How to tell if stripe charge is success or not - stripe-payments

I have php code to charge credit card via stripe
I am beginner in php classes, so i don't know how to tell if the payment is succeed or not
<?
require_once('lib/Stripe.php');
Stripe::setApiKey("sk_test_123ABC");
if(isset($_POST)) {
$payment = Stripe_Charge::create(array(
'amount' => '50',
'currency' => 'usd',
'card' => array(
'number'=> '4242424242424242',
'exp_month' => '12',
'exp_year'=> '14',
'cvc'=> '123'),
'description' => 'New payment'
)); }
print_r($payment);
?>
Results
Stripe_Charge Object
(
[_apiKey:protected] => sk_test_123ABC
[_values:protected] => Array
(
[id] => ch_157h99GwRVG4EfR7dCxslaAs
[object] => charge
[created] => 1418105445
[livemode] =>
[paid] => 1
[amount] => 50
[currency] => usd
[refunded] =>
[captured] => 1
[refunds] => Stripe_List Object
(

Your question has its answer.If an object is returned of stripecharge object that means the payment was successfull.

Sorry but is there a way to focus on an error too? The above only confirms if charge was successful or no. It doesn't state the reason for rejection.
error:
message: "Your card has insufficient funds."
type: "card_error"
code: "card_declined"
charge: ch_12asdasweAAFdgooXDUd2tvKJgW
Sorry could not post a comment, due to less reputation. :(

Related

How do I update product price in Stripe using PHP API?

Now I'm working on Stripe where I could set price for the product using PHP API call, but unable to update price amount. How can I do that?
My used code:
$price = $this->stripe->prices->update('price_1LRwQ6HEtJIPaXSgzQFrZtv7', [
'unit_amount' => $request->unit_amount * 100,
]);
I already solved the issue. In Stripe, a price update is not possible directly, just do the following to make it happen:
Delete the existing price.
Create new price for the product.
$this->stripe->plans->delete(
$price_data['stripe_price_id'],
[]
);
$price = $this->stripe->prices->create([
'unit_amount' => $request->price * 100,
'currency' => 'usd',
'recurring' => ['interval' => 'month'],
'product' => $data['stripe_product_id'],
]);

How to capture funds later when using Stripe's Checkout feature?

I am using Stripe's Checkout feature. My product takes a few minutes to generate, so I want to place a hold on the funds, only charging the customer once their product is ready. Is it possible to do so without refactoring away from using this feature?
This code works for me:
<?php
$amount = 50;
$stripe = new \Stripe\StripeClient(env('STRIPE_SECRET'));
$session = $stripe->checkout->sessions->create([
'payment_method_types' => ['card'],
'success_url' => 'reservation.success'.'?id={CHECKOUT_SESSION_ID}',
'cancel_url' => 'reservation.cancel',
'payment_intent_data' => [
'capture_method' => 'manual',
],
'line_items' => [[
'price_data' => [
'currency' => "eur",
'product_data'=> [
'name'=> "My awesome product",
],
'unit_amount'=> $amount * 100,
],
'quantity' => 1
]],
'mode' => 'payment',
]);
return redirect()->to($session->url);
This was done in laravel. You might need to adapt it a little to your needs.
Of course you need to
adapt $amount to the amount you want to collect in €.
change the currency if you need to
set the route for success_url
set the route for cancel_url
After calling this code the user is redirected to the session url, to make the payment, which is then reserved on his card.
You can then capture the payment later via:
<?php
$stripe = new \Stripe\StripeClient(env('STRIPE_SECRET'));
$session_id = 'THE_CHECKOUT_SESSION_ID';
$payment_intent = $stripe->checkout->sessions->retrieve($session_id)-payment_intent;
$intent = \Stripe\PaymentIntent::retrieve($payment_intent);
// Capture all
$intent->capture();
// Alternative: Capture only a part of the reserved amount
// $intent->capture(['amount_to_capture' => 750]);
See also https://stripe.com/docs/payments/capture-later

Stripe - Cannot charge a customer that has no active card

I have the follow block of PHP code which generates a new Stripe customer using a token, since i don't want to handle the card details on my server
$stripe_customer = \Stripe\Customer::create(array(
'source' => $token,
'description' => $displayname,
'metadata' => array('BHAA_ID'=>$bhaa_id),
'email' => $email
)
);
$stripe_customer_id=$stripe_customer->id;
I then attempt to register the card with these calls
$customer = \Stripe\Customer::retrieve($stripe_customer_id);
$card = $customer->sources->create(array("source" => $token));
The final step is to then charge the created customer like this using the customer id
// https://stripe.com/docs/api?lang=php#create_charge
$charge = \Stripe\Charge::create(array(
'amount' => $amount*100,
'currency' => 'eur',
'customer' => $stripe_customer->id,
'description' => $booking_description,
'metadata' => array("booking_id" => $booking_id, "event_name" => $booking_description),
'statement_descriptor' => "abc",
'receipt_email' => get_userdata($user_id)->user_email
));
This was working, but i recently updated to the 3.5.0 version of the stripe PHP API. I'm now getting this error
Booking could not be created:
Connection error:: "Cannot charge a customer that has no active card"
From what i can make out, it now seems that I have to register a card with the customer but I can't find any examples of this.
Any advise would be appreciated.
EDIT - Add stripe dashboard details
From the stripe dashboard, I can see that my customer is registered. This is in the response log
id: cus_7Xq5jNrrLfv73U
object: "customer"
account_balance: 0
created: 1450292050
currency: null
default_source: null
delinquent: false
description: "Web Master"
This is a screen shot of the 'Register Card' request. I can see that the 'customer id' is present in the URL, but the source parameter is not present as a Query Parameter or in the post body.

How do I refund a customer using Stripe Library

I am using Stripe for handling the payment process,and I come to the point when I have to refund the customers.
So far I am using this code:
$customer = \Stripe\Customer::create(array(
"source" => $token,
"description" => $email)
);
$charge = \Stripe\Charge::create(array(
'amount' => $amount, // Amount in cents!
'currency' => $this->currency,
"description" => $email,
"customer" => $customer->id
));
Once this is done, I store customer id to my users table customers. I have the customer stored id and later I wanna refund him, How do I do that?
I know you can refund using this code:
$charge = \Stripe\Charge::retrieve($charge_id);
$refund = $charge->refunds->create();
My case is a bit different that how do I find the charge_id using customer id, somewhat?
Thanks
$charge = \Stripe\Charge::create(array(
'amount' => $amount, // Amount in cents!
'currency' => $this->currency,
"description" => $email,
"customer" => $customer->id
));
Then add this lines :
$details = json_decode($charge);
$result = get_object_vars($details);
Now you get all the result in $result variable then you find:
$charge_id = $result['id'];

How can i get tax codes from NetSuite?

Now i have 2 ways to get tax code from NetSuite and these are advantage/disadvantage of each
1/ The first way:
I get all tax code from saleTaxItem list and save in database, with this way, it's easy and fast.
But we must to check, employees/vendors has permission to use it. in result, SalesTaxItem object don't have any property refer to employees/vendors and Employee/Vendor object don't have refer key to SalesTaxItem too.
So, how can i know employee/vendor has permission to use taxcode with this way?
This is structure of SalesTaxItem Object:
SalesTaxItem Object
(
[itemId] => Item Name
[displayName] =>
[description] =>
[rate] => 7.25%
[taxType] =>
[taxAgency] => RecordRef Object
(
[internalId] => -100
[externalId] =>
[type] =>
[name] => New Name
)
[purchaseAccount] =>
[saleAccount] =>
[isInactive] =>
[effectiveFrom] =>
[validUntil] =>
[eccode] =>
[reverseCharge] =>
[parent] =>
[exempt] =>
[isDefault] =>
[excludeFromTaxReports] =>
[available] =>
[export] =>
[taxAccount] => RecordRef Object
(
[internalId] => 37
[externalId] =>
[type] =>
[name] => New Name
)
[county] => Country Name
[city] =>
[state] => CA
[zip] => ,95646,96120
[nexusCountry] =>
[internalId] => -111
[externalId] =>
[nullFieldList] =>
)
2/ The second way:
I get employees list, vendors list. And foreach those lists to get taxcodes with function getSelectValueResult of NetsuiteService object.
With this way, with each employee/vendor we need call function getSelectValueResult to get taxcodes list of that employee/vendor. Althought we have 10 tax codes, but we need call function 1000 times (if we have 1000 employee/vendor).
Advantage of this way , we can save reference keys [taxcodes, employee], [taxcodes, vendor], it help check employee/vendor has permission to use tax code.
Disadvantage : slow and waste our time, and get duplicate tax code records.
This is structure of GetSelectValueResult Object when call function getSelectValueResult for each employee/vendor
[getSelectValueResult] => GetSelectValueResult Object
(
[status] => Status Object
(
[statusDetail] =>
[isSuccess] => 1
)
[totalRecords] => 2
[totalPages] => 1
[baseRefList] => BaseRefList Object
(
[baseRef] => Array
(
[0] => RecordRef Object
(
[internalId] => 25821
[externalId] =>
[type] => platformCore:RecordRef
[name] => My tax code name 1
)
[1] => RecordRef Object
(
[internalId] => 27812
[externalId] =>
[type] => platformCore:RecordRef
[name] => My tax code name 2
)
)
)
)
Which one i should to use ?
I think first way is good, but how can i check permission of employee/vendor when use taxcode?
Thank you very much.
Since Individual Tax codes does not have permission, you can get the roles of the employees separately and store it in a separate table. So you can join the roles whenever you need.

Resources