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.
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.
I got an error saying :
Customer cus_EmVoBWWrqnWw4W does not have a linked source with ID tok_1EIw1gKIAV9zC39qQnFXKzi6.
here is my code :
\Stripe\Stripe::setApiKey("sk_test_rUFQ9fpJoK9TlRVJs1nSYd6C");
$user = new User();
$email = $user->getEmail();
//creation du client
$customer = \Stripe\Customer::create(array(
"email" => $email,
));
$charge = \Stripe\Charge::create(array(
"amount" => 2000,
"currency" => "eur",
"source" => $request ->request->get('stripeToken'),
"description" => "Stripe connect - Order 36",
"customer" => $customer->id
));
\Stripe\Stripe::setApiKey("sk_test_rUFQ9fpJoK9TlRVJs1nSYd6C");
$refund = \Stripe\Refund::create([
'charge' => $charge,
'amount' => 1000,
]);
You need to save the card to the customer and then charge the customer. After that, unless the customer has multiple sources and you want to use a specific one, you won't need to pass source to Charge::create.
I have two functions namely charge and subscription, when a successful charge
happens I insert some metadata there look below:
Same for the subscription function:
Then I have a separate file which I will not post here because it only calls to the Charge list API to get all the successful charges to check all the metadatas in there, however I noticed that once I created a subscription via API it's only available there the metadata1 and metadata2.
When I view on the charge created by the subscription nothing was attached to its metadata. How can I attach the metadatas from the subscription to the charge that corresponds to it?
<?php
require_once('stripe-php-master/init.php');
require 'apiKeys.php';
$data = $_REQUEST['data'];
if(isset($data['stripeToken'])){
create_customer($data);
}else{
printResponse($data,"No Token");
}
//Create Customer
function create_customer($data){
try{
\Stripe\Stripe::setApiKey(SK_TEST_KEY);
$customer = \Stripe\Customer::create(array(
"description" => 'Widget Create Customer',
"source" => $data['stripeToken'],
"email" => $data['stripeEmail']
));
$data['customerID'] = $customer->id;
//create_charge($data);
create_subscription($data);
}catch(Exception $e){
echo $e->getMessage();
exit;
}
}//create_customer
//Create charge
function create_charge($data){
try{
\Stripe\Stripe::setApiKey(SK_TEST_KEY);
$settings = [
'currency' => 'aud',
'amount'=>5500,
'description' => 'Widget Payment for ' .$data['widget_name'],
'customer' => $data['customerID'],
'metadata'=>["metadata1" => $data['metadata1'],"metadata2" => $data['metadata2']]
];
// Get the payment token ID submitted by the form:
$charge = \Stripe\Charge::create($settings);
printResponse($data, $charge->status);
}catch(Exception $e){
printResponse($data,$e->getMessage());
exit;
}
}//create_charge
//Create subscription
function create_subscription($data){
try{
\Stripe\Stripe::setApiKey(SK_TEST_KEY);
$settings = [
'customer' => $data['customerID'],
'metadata'=>["metadata1" => $data['metadata1'],"metadata2" => $data['metadata2']],
'items' => [
[
'plan' => $data['widget_class']
]
]
];
$subscription = \Stripe\Subscription::create($settings);
printResponse($data,$subscription->status);
}catch(Exception $e){
printResponse($data,$e->getMessage());
exit;
}
}
function printResponse($data,$status){
//CREATE Response JSON
print_r(json_encode([
'customer_name'=> $data['customer_name'],
'metadata1' => $data['metadata1'],
'metadata2'=> $data['metadata2'],
'payment_status'=>$status
]));
}
?>
I just want the when someone subscribes the charge from the subscription event has the metadata1 and metadata2 attached to it.
my stripe account is configured with test. and using test keys into my laravel application.
While posting, i am creating new stripe user if not exist like this,
Stripe::setApiKey(Config::get('constants.features.stripe.secret'));
$token = $input['_token'];
$customer = Stripe_Customer::create(array(
"description" => "#{$user->id} / {$user->username} / {$user->email}",
"source" => $token,
"email" => $user->email
));
And creating charge like this,
$charge = Stripe_Charge::create(array(
"amount" => '5000',
"currency" => "gbp",
"customer" => $customer->id,
"description" => 'description',
"statement_descriptor" => strtoupper("test desc")
));
But it return error Stripe_InvalidRequestError No such token:
The _token input from Laravel is the CSRF token. This is not the token you should be passing to stripe in the source field. The token in the source field should be the token generated by the stripe.js library which allows you to identify a customers credit card without actually seeing the card details.
The stripe documentation explains how you should be handling this https://stripe.com/docs/stripe.js
I am trying to implement what mentioned here but getting error:
Array ([type] => invalid_request_error [message] => No such customer: 92 [param] => customer)
I am using Laravel Cashier:
$user = User::find($current_user_id);
if ($user->charge(100, [
'customer' => 92,
'source' => $token,
'description' => "Testing Payment Deduction for Brief"
])
) {
print "Awesome";
} else {
print "it failed";
}
Using this code to create customer fails as it trie to create a record in users table which does not need as I already have a user created:
$customer = $user->create(array(
'email' => 'customer#example.com',
'card' => $token
));
How can I send my existing customer ID and email to Stripe?
This issues erupts only due to one reason and that is token generated by following function.
Stripe.card.createToken($form, stripeResponseHandler)
in stripeResponseHandler(status, response)
response.id is the token value. so please use this token to create subscription or whatever... Thanks