Stripe This customer has no attached payment source - php

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);

Related

Laravel Stripe Payment Error Redirect Not Working

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.

Stripe payment SCA

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.

how to pass php variable to charge create in stripe

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.

Error : cannot use stripe token more than once

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.

Wepay integration

I'm trying to create a simple checkout page with wepay and my checkout code (taken from the SDK sample) works great for the owner (me) when I'm signed in, but when logged in as a different user who hasn't created the account (under theirs?) it says the account is invalid or does not belong to the user.
So how are new logged in users supposed to pay to the account (mine), in other words make payments?
Here is the code for reference. The account_id doesn't work for new logged in users because they haven't created it.
$wepay = new WePay($_SESSION['wepay_access_token']);
$checkout = $wepay->request('checkout/create', array(
'account_id' => 501999810,
'amount' => 1.00,
'currency'=> 'USD',
'short_description'=> 'Selling 42 Pens',
'type'=> 'goods'
));
Maybe I have something completely off, but that account_id is where I want to receive payments?
Any help would be appreciated!
Once you register the user, you have to create an actual merchant account by calling /account/create.
Once you have the user's access token, you can call /account/create with the user's access token and the relevant info to actually attach an account to the user. The /account/create call will return an account_id. That account_id is the one you use in the /checkout/create call.
So until you create an account via /account/create the user cannot accept payments.
$productData['data']['seller_data']['wepay_access_token']="STAGE_ea6cd2dffa3dfa23bd4817f210936fadada9fa91e906f353e15813c6cf920fb8";
$productData['data']['seller_data'}['wepay_account_id']="287443494";
$wepay = new WePay($productData['data']['seller_data']['wepay_access_token']);
$checkOutData = array(
//"account_id" => 12345678,
"account_id" => $productData['data']['seller_data'}['wepay_account_id'],
"amount" => 500,
"type" => "goods",
"currency" => "USD",
"short_description" => "Purchase made at test site",
"long_description" => "The charges made in this payment are of the order placed in test",
"delivery_type" => "point_of_sale",
"fee" => array(
"app_fee" => 15,
//'fee_payer' => 'payer_from_app'
'fee_payer' => 'payee_from_app'
),
//"auto_release"=>false,
"payment_method" => array(
"type" => "credit_card",
"credit_card" => array(
"id" => 2178689241,
"auto_capture" => false
)
)
);
try {
$checkoutResult = $wepay->request('checkout/create', $checkOutData);
} catch (Exception $e) {
$data['status'] = '0';
$data['message'] = 'We could not complete checkout!';
return $this->SetOutput();
}
You can get seller access token and account id using
$user_id=20;
$access_token="STAGE_ea6cd2dffa3dfa23bd4817f210936fadada9fa91e906f353e15813c6cf920fb8":
$userName="test user";
$description="sell products";
try {
$wepay = new WePay($access_token);
if (empty($userName)) {
try {
$uresponse = $wepay->request('/user');
$userName = $uresponse->user_name;
} catch (WePayException $e) {
$uresponse['status'] = '0';
$uresponse['message'] = $e->getMessage();
return $uresponse;
}
}
$response = $wepay->request('account/create/', array(
'name' => $userName,
'description' => $description,
'reference_id' => '"' . $user_id . '"'
));
} catch (WePayException $e) {
$response['status'] = '0';
$response['message'] = $e->getMessage();
return $response;
}

Categories