I have successfully integrated stripe payment. However, when I pass hardcoded amount and currency it works fine, but as I use variable in the charge array it gives me HTTP ERROR 500
My code:
<?php
require_once "config.php";
session_start();
$depoamount=$_SESSION['stripeamount'];
$curr=$_SESSION['curr'];
$token = $_POST['stripeToken'];
$email = $_POST['stripeEmail'];
$custid=$_SESSION['reg_no'];
//$custid='123';
$id=$_POST['ID'];
/* $depoamount=1200;
$curr='inr';*/
$customer = \Stripe\Customer::create([
'email' => $email,
'source' => $token,
'customer'=> $custid,
]);
$charge = \Stripe\Charge::create([
'customer' => $customer->id,
'amount' => $depoamount,// If I use '5000' it works
'currency' => $curr,// same if I put 'usd' it works
]);
echo '<h1>Successfully charged</h1>';
echo $charge->email;
echo $charge->id;
echo $custid;
?>
I think syntax is ok , however its not working.
Related
If the payment is successful, it redirects to the "success" page, but if the payment fails, it does not redirect to the "cancel" page, it looks like the photo below. I will be glad if you can help, thank you.
/* stripe code */ else if ($payment_method == 'stripe') {
$stripe = array(
"secret_key" => $stripe_secret_key,
"publishable_key" => $stripe_publish_key
);
\Stripe\Stripe::setApiKey($stripe['secret_key']);
$customer = \Stripe\Customer::create(array(
'email' => $order_email,
'source' => $token
));
$item_name = $item_names_data;
$item_price = $amount * 100;
$currency = $site_currency;
$order_id = $purchase_token;
try {
$charge = \Stripe\Charge::create(array(
'customer' => $customer->id,
'amount' => $item_price,
'currency' => $currency,
'description' => $item_name,
'metadata' => array(
'order_id' => $order_id
)
));
$chargeResponse = $charge->jsonSerialize();
if ($chargeResponse['paid'] == 1 && $chargeResponse['captured'] == 1) {
return view('success')->with($data_record);
}
} catch (\Stripe\Exception\CardException $e) {
return view('cancel');
}
}
There are multiple things unclear and it's hard to help you with just this information.
You are using Charge API which is an old and legacy API. It's
better to go with Payment Intent API.
It looks like you haven't written a full error handling logic. You can debug by catching all possible Error types, including the ApiErrorException you are receiving. See example on Stripe API Reference.
My current Stripe code is as below: as I pass to stripe.
Had been using following Stripe Library:
https://github.com/stripe/stripe-php
Its working fine as of now. Now I need to change code according to SCA. But I am total lost with code update.
My controller
$this->stripe_->add_charge_array($stripe_user_id, $order_info, $price * 100, $currency, $metadata);
$this->stripe_->pay($currency_id, , $stripeToken, $card_id);
$data = $this->stripe_->get_charge_response($stripe_user_id, , $order_id);
$order->setStripeChargeId($data['charge_id']);//Here I save to db
And in stripe library:
public function pay($currency_id, $stripe_token = null, $card_id = null, $account_id = null){
$charge = $this->charge_card($cart['amount'], $cart['currency'], $stripe_token, $description, $cart['metadata'], $currency_id, $account_id);
}
And my charge_card method connecting to Stripe
public function charge_card(){
try {
\Stripe\Stripe::setApiKey($STRIPE_KEY);
$charge = \Stripe\Charge::create(array(
"amount" => intval($amount),
"currency" => $currency,
"source" => $stripe_token, // obtained with Stripe.js
"description" => $description,
"metadata" => $metadata
));
} catch (\Stripe\Error\Base $e) {
// Display a very generic error to the user, and maybe send
// yourself an email
$result = self::put_stripe_error($e);
} catch (Exception $e) {
// Something else happened, completely unrelated to Stripe
self::put_stripe_error($e);
}
return $charge;
}
Stripe library:
https://js.stripe.com/v3/
From what I did understand from documentation is that I need to change my charge_card method like below.
$intent = \Stripe\PaymentIntent::create([
'payment_method' => '{{PAYMENT_METHOD_ID}}',
'customer' => '{{CUSTOMER_ID}}',
'amount' => 1099,
'currency' => 'gbp',
'confirmation_method' => 'manual',
'confirm' => true,
'setup_future_usage' => 'off_session',
]);
But then I get payment method Id in $intent . How to proceed from here.
Documentation misses this.
I'm trying to make a monthly subscription plan using the stripe API, but I have an error message saying
Cannot use stripe token more than once
And when I try to remove 'source' => $token I have this error :
The customer must have an active payment source attached.
Here's my code :
require_once('stripe/config.php');
$token = $_POST['stripeToken'];
$email = $_POST['stripeEmail'];
$customer = \Stripe\Customer::create([
'email' => $email,
'source' => $token,
]);
\Stripe\Stripe::setApiKey("sk_test_xxxxxxxxxxxxxxxxxxxxxxxxx");
$product = \Stripe\Product::create([
'name' => 'Abonnement Simple',
'type' => 'service',
]);
$subscription = \Stripe\Subscription::create(array(
"customer" => $customer->id,
"plan" => "simpleNoEngagement"
));
token is required only for creating new customer. Once customer is created, you do not need to send token again. You need to grab customerId in that case.
I have a problem in my wordpress, i use stripe and i have this error message : "This customer has no attached payment source " but i don't understand why. In dev environment i had no problem and now in prod i have this issue. For information i use the card number 4242 4242 4242 4242 for testing.
Thanks a lot for your help
if(isset($_POST['action']) && $_POST['action'] == 'stripe' && wp_verify_nonce($_POST['stripe_nonce'], 'stripe-nonce')) {
global $stripe_options, $post;
// load the stripe libraries
require_once(STRIPE_BASE_DIR . '/init.php');
// retrieve the token generated by stripe.js
$token = $_POST['stripeToken'];
$amount = base64_decode($_POST['amount'])*100;
$email = $_POST['email'];
$plan_nickname = $_POST['plan_nickname'];
$plan_id = $_POST['plan_id'];
$nom = $_POST['name'];
$prenom = $_POST['prenom'];
$adresse = $_POST['address-line1'];
$ville = $_POST['address-city'];
$zip = $_POST['address-zip'];
// check if we are using test mode
if(isset($stripe_options['test_mode']) && $stripe_options['test_mode']) {
$secret_key = $stripe_options['test_secret_key'];
} else {
$secret_key = $stripe_options['live_secret_key'];
}
// attempt to charge the customer's card
try {
\Stripe\Stripe::setApiKey($secret_key);
$product = \Stripe\Product::create([
'name' => $stripe_options['product_name'],
'type' => 'service',
]);
$plan = \Stripe\Plan::create([
'product' => $stripe_options['product_key'],
'nickname' => $plan_nickname,
'interval' => 'month',
'currency' => 'eur',
'amount' => $amount,
]);
$customer = \Stripe\Customer::create([
'email' => $email,
'source' => $token,
'description' => $plan_nickname,
]);
$subscription = \Stripe\Subscription::create([
'customer' => $customer->id,
'items' => [['plan' => $plan_id]],
]);
// redirect on successful payment
$redirect = add_query_arg('payment', 'paid', $_POST['redirect']);
} catch (Exception $e) {
// redirect on failed payment
//$redirect = add_query_arg('payment', 'failed', $_POST['redirect_failed']);
var_dump($e);
}
// redirect back to our previous page with the added query variable
wp_redirect($redirect); exit;
}
}
Check the plan on Stripe. If the plan has a trial period you need to create the subscription with the trial period. I am using laravel cashier and react-stripe-checkout and this worked for me:
$user->newSubscription('main', 'basic')
->trialDays(30)
->create($request->token);
I've had a look at a few answer to this quesition on Stack Overflow, but there solutions didnt work for me. Im receiving the following error whilst testing the Stripe API
Class 'Stripe\StripeCharge' not found
This is the code that I am using:
require_once('app/init.php');
\Stripe\Stripe::setApiKey($stripe['private']);
if(isset($_POST['stripeToken'])){
$token = $_POST['stripeToken'];
try {
\Stripe\StripeCharge::create(array(
"amount" => 2000,
"currency" => "gbp",
"card" => "$token",
"description" => $Email
));
} catch(Stripe_CardError $e){
//Error Payment
}
}
echo $_POST['stripeToken'];
/* Stripe Vairables */
$stripe = [
'publishable' => 'hidden',
'private' => 'hidden'
];
I didn't use composer to pull this as it works with the include of the "init" file (supposedly). Any help would be great!
The correct name of the class is \Stripe\Charge, not \Stripe\StripeCharge. You can see an example of a charge creation request in the API documentation: https://stripe.com/docs/api/php#create_charge
Also, the card parameter was renamed to source in the 2015-02-18 API version.
Another problem is that you assign the $stripe array with your API keys at the end. You need to assign it before you can use it in the call to \Stripe\Stripe::setApiKey().
There were a few other minor mistakes. Here is a corrected version of your code:
require_once('app/init.php');
/* Stripe variables */
$stripe = [
'publishable' => 'hidden',
'private' => 'hidden'
];
\Stripe\Stripe::setApiKey($stripe['private']);
if(isset($_POST['stripeToken'])) {
$token = $_POST['stripeToken'];
$email = $_POST['stripeEmail'];
try {
$charge = \Stripe\Charge::create(array(
"amount" => 2000,
"currency" => "gbp",
"source" => $token,
"description" => $email
));
// echo $charge->id;
} catch(\Stripe\Error\Card $e) {
// Card error
}
}