The old way of doing this was as follows
$customer = \Stripe\Customer::create(array(
"description" => $domain,
"email" => $email,
"source" => $token,
"metadata" => array(
"name" => $name,
"phone" => $phone
),
));
$cus = $customer->id;
\Stripe\Subscription::create(array(
"customer" => $cus,
"plan" => "1",
));
But now I do not see the "PLAN" option on the subscription create. Here is what I have so far...
$customer = $stripe->customers->create([
'description' => 'Description text here nomadweb.design',
'name' => 'Sammy Malones',
'email' => 'name#email.com',
'phone' => '5124592222'
]);
$cus = $customer->id;
$stripe->subscriptions->create([
'customer' => $cus,
'plan' => '1'
]);
In the API Docs is says that it's required to use the items parameter.
My question is how to I add a subscription to a customer with the newer api?
This is their code but I don't understand
$stripe->subscriptions->create([
'customer' => 'cus_J34i3JonNQQXdO',
'items' => [
['price' => 'price_0IQyZLH7HxDXZRHqJfpwwqBB'],
],
]);
https://stripe.com/docs/api/subscriptions/create
In my stripe dashboard I have a product created which is a monthly subscription, it has an ID like prod_BlMuxdEQJSxfKJ So I'm guessing I need to pass that ID in somehow as an item?
I would encourage you to read about Prices, the successor to Plans, but you can also provide an existing Plan like plan_123 to the subscription creation request, and it will be converted to a Price for you:
$stripe->subscriptions->create([
'customer' => 'cus_123',
'items' => [
['price' => 'plan_123'],
],
]);
You can't provide a Product here directly, as Products are not directly tied to any amount or interval. You need to create Prices for those Products, either using the API or your Dashboard.
When creating a subscription, you can optionally define the recurring pricing ad-hoc, using price_data (API doc) and referencing the Product to be used:
$subscription = $stripe->subscriptions->create([
'customer' => 'cus_123',
'items' => [[
'price_data' => [
'unit_amount' => 5000,
'currency' => 'usd',
'product' => 'prod_456',
'recurring' => [
'interval' => 'month',
],
],
]],
]);
Thank you to Nolan, it looks like you need to grab the product pricing API ID which is provided in the dashboard.
Here is the updated code
$stripe->subscriptions->create([
'customer' => $cid,
'items' => [['price' => 'price_0IR0OGH7HxDXZRHq3sIg9biB'],],
]);
Here the price ID is attaching the product which is a subscription to the customer.
if you are using laravel and stripe php sdk with it then you can do it like this below:
\Stripe\Stripe::setApiKey(env('STRIPE_PRIVATE_KEY'));
// Use an existing Customer ID if this is a returning customer.
// $customer = \Stripe\Customer::create([
// "description" => $domain,
// "email" => $email,
// "source" => $token,
// "metadata" => [
// "name" => $name,
// "phone" => $phone
// ],
// ]);
$customer = \Stripe\Customer::create();
// $customer_id = $customer->id;
$Subscription = \Stripe\Subscription::create([
'customer' => $customer->id,
'items' => [[
'price' => $price_id,
]],
'payment_behavior' => 'default_incomplete',
'expand' => ['latest_invoice.payment_intent'],
]);
$ephemeralKey = \Stripe\EphemeralKey::create(
['customer' => $customer->id],
['stripe_version' => '2020-08-27']
);
// $paymentIntent = \Stripe\PaymentIntent::create([
// 'amount' => $amount,
// 'currency' => $currency,
// 'customer' => $customer->id
// ]);
$response = [
'request' => $data,
'paymentIntent' => $subscription->latest_invoice->payment_intent->client_secret,
'ephemeralKey' => $ephemeralKey->secret,
'customer' => $customer->id,
'subscriptionId' => $subscription->id,
];
return response($response, 200);
And then in your front end you can process the payment with the help of your paymentIntent secret.
Related
I have a function a stripe function that slashes the price of a product, and I am trying to charge the newly slashed price after 5 minutes.
in my checkoutController I have
public function secondHalf($id){
// fetch the price
$productprice = Product::where('id', $id)
->pluck('price')
->firstOrFail();
// slash price into half
$slashPrice = intdiv($productprice,2); // divide by 2
// commence payment
\Stripe\Stripe::setApiKey(config('stripe.sk'));
$session = \Stripe\Checkout\Session::create([
'line_items' => [
[
'price_data' => [
'currency' => 'gbp',
'product_data' => [
'name' => 'tax online payment',
],
'unit_amount' => $slashPrice*100,
],
'quantity' => 1,
],
],
'mode' => 'payment',
'success_url' => route('success'),
'cancel_url' => url('/single-product/'.$id),
]);
$details = ['email' => 'thomsontochi#gmail.com'];
$emailJob = (new AfterPayment($details))
->delay(Carbon::now()
->addMinutes(5));
dispatch($emailJob);
return redirect()->away($session->url);
}
it basically find the product, slash the price and I then charge the slashed price.
I have tried making a job , called LatePaymentCharge and I took the function that handles the charge to the job like so
public function handle()
{
$slashPrice = $this->slashPrice;
//dd('stipe checkout');
\Stripe\Stripe::setApiKey(config('stripe.sk'));
$session = \Stripe\Checkout\Session::create([
'line_items' => [
[
'price_data' => [
'currency' => 'gbp',
'product_data' => [
'name' => 'staxo online payment',
],
'unit_amount' => $slashPrice*100,
],
'quantity' => 1,
],
],
'mode' => 'payment',
'success_url' => route('success'),
// 'cancel_url' => url('/single-product/'.$id),
]);
return redirect()->away($session->url);
}
I am currently having issues on how to pass the slash price value from my checkoutController to my job and im not sure if this is the right way to do it . please help
For some reason, when you create a "Subscription Schedules" stripe, it has very odd behavior where instead of trying to charge the customer, it keeps the invoice in draft for 1 hour and then closes the invoice and charges the customer.
I already have the card in the customer. I wonder if there is any way I can force the first phase of the subscription to be charged immediately.
My code:
$phases = [
[
'items' => [
[
'price_data' => [
'currency' => 'usd',
'product' => $product->stripe_product_id,
'recurring' => [
'interval' => $payment_plan['frequency'],
],
'unit_amount' => $payment_plan['stripe_amount']
],
'quantity' => 1,
],
],
'iterations' => (int) $payment_plan['total_payments']
],
];
$subscription = $stripe->subscriptionSchedules->create([
'customer' => $customer->stripe_customer_id,
'start_date' => 'now',
'end_behavior' => 'cancel',
'phases' => $phases,
]);
If you want to create a subscription immediately, you can do that without a Subscription Schedule and then set the schedule for that existing subscription:
https://stripe.com/docs/billing/subscriptions/subscription-schedules/use-cases#existing-subscription
Alternatively, if creating with the schedule like you're doing, once the subscription/invoice is created (as a draft) you can use the API to finalize it manually to proceed with the payment:
https://stripe.com/docs/api/invoices/finalize
Here is the solution:
$subscription = $stripe->subscriptionSchedules->create([
'customer' => $customer->stripe_customer_id,
'start_date' => 'now',
'end_behavior' => 'cancel',
'phases' => $phases,
]);
// now the invoice is a draft so we go get this invoice
$invoice = $stripe->invoices->all([
'limit' => 3,
'status' => 'draft',
'subscription' => $subscription->subscription,
'customer' => $customer->stripe_customer_id,
]);
//get the most recent draft
$invoice = $invoice->data[0];
//finalize the invoice (but this don't generate the payment for some reason)
$stripe->invoices->finalizeInvoice(
$invoice->id, [
'auto_advance' => true
]
);
//finaly pay the invoice
$invoice = $stripe->invoices->pay($invoice->id,[
'payment_method' => $token
]);
I am trying to customise stripe checkout to take a dynamic price from my cart. To test that I am simply trying to create a product and price within the strip config and then pass that price into the line item.
The code that I am trying keeps telling me that is no such product. I don't know if I am making a syntax error or if what I am trying is just wrong. can anyone advise?
include './stripe/init.php';
\Stripe\Stripe::setApiKey('API KEY GOES HERE');
header('Content-Type: application/json');
$YOUR_DOMAIN = 'https://www.XXXXX.XX.XX';
$product = \Stripe\Product::create([
'name' => 'Elearn Product',
]);
$price = \Stripe\Price::create([
'product' => '{{$product}}',
'unit_amount' => 1100,
'currency' => 'usd',
'recurring' => [
'interval' => 'month',
],
]);
$checkout_session = \Stripe\Checkout\Session::create([
'line_items' => [[
'price' => '{{$price}}',
'quantity' => 1,
]],
'payment_method_types' => [
'card',
],
'mode' => 'payment',
'success_url' => $YOUR_DOMAIN . '/enrolment-success',
'cancel_url' => $YOUR_DOMAIN . '/enrolment-failure',
]);
header("HTTP/1.1 303 See Other");
header("Location: " . $checkout_session->url);
You need to specify the Product and Price IDs like this:
'product' => $product->id,
And this:
'price' => $price->id,
Also, you can create multiple Prices for one Product, so if you're selling the same thing at different price points you can create the Product once and create many Prices for it instead of creating a new Product each time.
I am using Stripe Checkout API to direct a website user to make payment.
Is there a way to pass a shipping address to the hosted checkout page so it's gathered from the referrer rather then Stripe themselves?
function createSession()
{
require 'vendor/autoload.php';
\Stripe\Stripe::setApiKey('[API_KEY_REDACTED]');
$YOUR_DOMAIN = '[SITE_URL_REDACTED]';
// Calculate price
$price = 50;
$checkout_session = \Stripe\Checkout\Session::create([
'billing_address_collection' => 'required',
'payment_method_types' => ['card'],
'line_items' => [[
'price_data' => [
'currency' => 'gbp',
'unit_amount' => $price,
'product_data' => [
'name' => 'Product'
],
],
'quantity' => 1,
]],
'mode' => 'payment',
'success_url' => $YOUR_DOMAIN . '/success',
'cancel_url' => $YOUR_DOMAIN . '/cancel',
]);
return json_encode(['id' => $checkout_session->id]);
}
You can add the following line to make Stripe ask for a shipping address, but I want to pass this from the referrer instead.
'shipping_address_collection' => [
'allowed_countries' => ['US', 'CA'],
],
To do this you would create a Customer and provide their shipping address, then provide that existing Customer when creating the Checkout session:
$checkout_session = \Stripe\Checkout\Session::create([
'customer' => 'cus_123',
...
]);
I clone braintree project from https://github.com/braintree/braintree_php_example. Than I created account https://www.braintreepayments.com/sandbox. I must to return client_token. I debug this code
$result = Braintree\Transaction::sale([
'amount' => $amount,
'paymentMethodNonce' => $nonce,
'options' => [
'submitForSettlement' => true
]
]);
var_dump($result->transaction);
But token = null. Maybe my steps are incorrect?
////////////////////////////////
I did it!
I create user
$result = Braintree_Customer::create([
'firstName' => 'Mike',
'lastName' => 'Jones',
'company' => 'Jones Co.',
'email' => 'mike.jones#example.com',
'phone' => '281.330.8004',
'fax' => '419.555.1235',
'website' => 'http://example.com']);
Than I get customer_id
$result->customer->id;
Than I get token
$clientToken = Braintree_ClientToken::generate([
"customerId" => $result->customer->id
]);
Maybe problem with custom register in https://www.braintreepayments.com/sandbox.
Maybe I didn't put all information