here is my code to create a subscription:
$subscription = \Stripe\Subscription::create(array(
"customer" => $customer->id, //customer id from previous lines after creating customer
"plan" => 'premium-plan',
'metadata' => ['user_id' => $userId]
));
here is my code to update plans:
$subscriptionUpdate = \Stripe\Subscription::retrieve($subscriptionIdFromDatabase);
$subscriptionUpdate->plan = 'best-premium-plan';
$subscriptionUpdate->save();
How can I add metadata to an invoice if the user wants to update plans?
if the user wants to update their plan using the second block of code, it will generate an invoice. how can i assign metadata to that invoice when user changes plans?
Perform an update on the invoice.
Invoice::update($invoiceId, ['metadata' => [
'my_data' => $someVar
]]);
https://stripe.com/docs/api/metadata
Related
I want to add Add On to my subscription. Thus i follow this MultiPlan in Laravel doc. For every new subscription, new stripe product are create (means every plan have different product. including the addon). User need to subscribe to any subscription before subscript to the addon. There are no error, but the Subscription DB from stripe for the current subscription will return null quantity and null stripe_plan. Then create error from that database as i cant call the current subscription. Why does the stripe does that? or am I surpose to create new plans under the same product id in Stripe?
My code to create stripe product and plan
$stripe = new StripeClient(env('STRIPE_SECRET'));
$product_stripe = $stripe->products->create([
'name' => $request->service_name,
]);
$plan_stripe = $stripe->plans->create([
// 'id' => $stripe_plan,
'nickname' => $request->service_name,
'product' => $product_stripe['id'],
'amount' => $request->price*100,
'currency' => 'usd',
'interval' => $request->period,
'interval_count' => $request->period_num,
]);
This is my code to subscribe to addon
$user = auth()->user();
$user->subscription('default')->addPlanAndInvoice('plan_stripe_id', 'quantity');
Note that default is the user current subscription.
I use this code to make a payment and create a new customer on my stripe dashboard.
$customer = \Stripe\Customer::create([
"name" => $utente,
"source" => $_POST['stripeToken'],
"email" => $row['mail'],
]);
$charge = \Stripe\Charge::create([
"customer" => $customer->id,
"amount" => $amount_cents,
"currency" => "eur",
"description" => $description
]);
The information such as the name or email the use to create a new customer, are taken from the dashboard of my site when a user is registered and connected to it.
This concern the user's first payment .php file.
on the second user's payment, the site redirect to a second .php file, where I need to add a second payment to the customer previously created. the problem is that if I keep the same code, on the stripe dashboard I find myself two equal customers. I don't know how to get the customer id when, when I redirect to the second payment page, as user information I only have the mail and the name .. how can I do?
I'll try something like that by searching on stripe api reference:
$customer = \Stripe\Customer::all([
'email' => $row['mail']
]);
$charge = \Stripe\Charge::create([
"customer" => $customer['data']->id,
"amount" => $amount_cents,
"currency" => "eur",
"description" => $description
]);
but it give me the error
Must provide source or customer..
Listing customers will return an object with a data property. data is a list of customers so you'll need to index into that list and grab one of the customers before looking at their ID. It should look something like:
$customer = \Stripe\Customer::all([
'email' => $row['mail']
]);
$charge = \Stripe\Charge::create([
'customer' => $customer->data[0]->id,
'amount' => $amount_cents,
'currency' => 'eur',
'description' => $description
]);
Following code I'm using to upgrade a user's plan:
$subscription = \Stripe\Subscription::all(array('customer'=>$customerId,'limit'=>10));
$subscription_id = $subscription->data[0]->id;
$subscription = \Stripe\Subscription::retrieve($subscription_id);
$updatePlan = [
'cancel_at_period_end' => false,
'items' => [
[
'id' => $subscription->items->data[0]->id,
'plan' => planPrefix.$packageId,
],
],
'tax_percent' => $package_tax,
];
\Stripe\Subscription::update($subscription_id, $updatePlan);
//Create invoice now
$invoice = \Stripe\Invoice::create([
'customer' => $customerId,
]);
$invoiceId = $invoice->id;
//Pay invoice now
$invoice = \Stripe\Invoice::retrieve($invoiceId);
$invoice->pay();
And when stripe get paid to that newly created invoice, The event "invoice.payment_succeeded" is triggered to my webhook where I update my database accordingly.
The problem is that stripe sends me previous plan id with the accurate invoice.
E.g., If User A subscribes to plan id 1 then stripe sends me object for the newly subscribed plan details to my webhook with accurate data, but when User A
upgrades to plan whose id is 2, stripe sends me data with the event of "invoice.payment_succeeded" where I can find all the data related to subscription update but the issue is with the plan id. The plan id stripe sends me is old one i.e., 1 instead of 2, and when User A upgrade to plan id 3 then it sends me plan id 2 in the webhook notification.
Any help in this matter would be much appreciated.
For compliance reasons I am generating tokens on the client side and sending those details to stripe. I want to display the last four digits and the type of card on my confirmation page
I am creating a customer
// Create a Customer:
$customer = \Stripe\Customer::create([
'source' => $token,
'email' => $current_user->user_email,
]);
than adding them to a subscription
//create the subscription for the customer
$subscription = \Stripe\Subscription::create(array(
'customer' => $customer->id,
"items" => array(
array(
"plan" => "dpc-standard",
),
)
));
The subscription returns https://stripe.com/docs/api#subscription_object
a ton of data including the invoice_id that is generated for the subscription but doesn't return any CC details
When you create a Customer and pass the source parameter set to a token id, it will save that card on the new customer. The value returned by this call is a Customer object with the sources property which will contain the new card you just saved.
You can access the last 4 digits easily using:
$last4 = $customer->sources->data[0]->last4;
After completing transaction, there are two customer entry under vault.
Step I have followed:
1. Created customer.
//first customer vault entry is created at this point
$customerParams = Braintree_Customer::create(array(
'firstName' => $firstName,
'lastName' => $lastName,
));
2.Then generated clientToken
Braintree_ClientToken::generate(array(
"customerId" => $customerParams->customer->id
));
3.Then with help of api generated nonce in js successfully:
var client = new braintree.api.Client({clientToken: ctoken});
client.tokenizeCard({
...
...
});
4.Again at this point new customer is created
Braintree_Transaction::sale(array(
'amount' => $mapCidInvoiceID['amount'],
'orderId' => $redirectParams['invoiceID'],
'paymentMethodNonce' => $nonce,
'options' => array(
'storeInVaultOnSuccess' => true,
),
));
is there something wrong in my code? why for one transaction two customer record are created? For first record first and last name are recorded. But second case no such detail are stored.
First creation of customer is required for second and third step.
Full disclosure: I work at Braintree.
Your code is creating two separate customers because when you call Transaction::sale it does not have the customer from your Customer::create call tied to it, and you have the option set to store the payment method in the vault upon success. This creates a customer when saving the payment method from the transaction, because a payment method must be tied to a customer. To resolve your problem, pass the customer id returned from Customer::create as a customerId param when you call Transaction::sale.
Braintree_Transaction::sale(array(
'amount' => $mapCidInvoiceID['amount'],
'orderId' => $redirectParams['invoiceID'],
'customerId' => $customerParams->customer->id
'paymentMethodNonce' => $nonce,
'options' => array(
'storeInVaultOnSuccess' => true,
),
));