Stripe Session with Shipping fee - stripe-payments

How can I add shipping fee to stripe session create.
Here is what the have so far:
`
$line_items = [
'price_data' => [
'product_data' => [
'name' => $product->title
],
'unit_amount' => $product->price * 100,
'currency' => 'cad',
],
'quantity' => $product->qty
];
$checkout_session = \Stripe\Checkout\Session::create([
'success_url' => $domain . '/success?session_id={CHECKOUT_SESSION_ID}',
'cancel_url' => $domain . '/cart',
'payment_method_types' => ['card'],
'mode' => 'payment',
'line_items' => [$line_items]
]);
`

There is no way to have Stripe calculate shipping. In order to include a shipping amount, you can either increase the unit_amount on your current line item, or add another line item for the shipping total.
I would recommend creating the full product catalog inside of Stripe and consider using a standard Product for Shipping, then use a derived amount for the unit amount on that shipping price data with the standard Shipping product.

Related

stripe subscription from registeration date of the month with initial fee

I want to do stripe monthly subscription with these conditions
monthly charge 29.99 dollars
when registration is done in the middle of the month installation price of 50.00 dollars will be added with subscription price.
For example, user registered October 29th
79.99 dollars should be paid at that timing. Installation Fees + Subscription Fees
29.99 us dollar subscription is charge at the same date of next month
how can I configure this? I want to know the configuration for stripe in codeigniter php
$price = \Stripe\Price::create([
'unit_amount' => 29.99*100,
'currency' => 'usd',
'recurring' => ['interval' => 'month'],
'product' => 'Monthly Plan Home Internet',
]);
$customer = \Stripe\Customer::create([
'email' => $email,
'source' => $token,
]);
$subscription = \Stripe\Subscription::create(array(
"customer" => $customer->id,
"currency" => $currency,
"add_invoice_items"=>array(
array(
"price"=>$price->id
),
),
"items" => array(
array(
"price" => "29.99"
),
),
));
Please Follow The Following Code
$customer = \Stripe\Customer::create([
'email' => $email,
'source' => $token,
]);
$subscription = \Stripe\Subscription::create(array(
"customer" => $customer->id,
"currency" => $currency,
"add_invoice_items"=>array(
array(
"price"=>"price_1LyBtEIhiBmLLX1CMotP6b2z" //first month payment with installation fees
),
),
"items" => array(
array(
"price" => "price_1LyC2vIhiBmLLX1ChID4GBZu" //monthly payment
),
),
));
When you create the Subscription you can use add_invoice_items to add a one-time Price to the first Invoice of the Subscription only.
That will be charged in addition to the recurring price you set in items, and the one-time Price will not show up on subsequent Invoices.

Stripe how to retrieve a Checkout Session's line items quantity?

Create payment Sessions :
$session = \Stripe\Checkout\Session::create([
'payment_method_types' => ['card'], //, 'fpx','alipay'
'line_items' => [[
'price_data' => [
'product_data' => [
'name' => "Topup USDT Wallet",
'images' => ["https://abc-uaha.co/uploads/site_logo/site_logo_20210321130054.png"],
'metadata' => [
'pro_id' => "USD".$_GET['price']/100
]
],
'unit_amount' => $_GET['price'],
'currency' => 'usd',
],
'quantity' => 1,
'description' => "Spartan Capital",
]],
'mode' => 'payment',
'success_url' => STRIPE_SUCCESS_URL.'?session_id={CHECKOUT_SESSION_ID}',
'cancel_url' => STRIPE_CANCEL_URL,
]);
Refer to this docs : https://stripe.com/docs/api/checkout/sessions/line_items
I tried retrieve quantity from session:
try {
$checkout_session = \Stripe\Checkout\Session::retrieve([
'id' =>$session_id,
'expand' => ['line_items'],
]);
}catch(Exception $e) {
$api_error = $e->getMessage();
}
$line_items = $session->line_items[0].quantity;
echo $line_items; //it shows nothing, how to make it output "1"?
line_items are no longer included by default when retrieving Checkout Sessions. To get them in your retrieve call, you need to expand the line_item property.
You have two syntax errors :
You're missing a layer and using dot notation instead of PHP arrow syntax. The 2nd error is using $session instead of $checkout_session. So it should be :
$quantity = $checkout_session->line_items->data[0]->quantity;

Stripe plan dynamic plan with sca

My issue:
I am working on a subscription or installment based application so the user gets whatever amount they had to for 3 or 6 months. I used stripe plan and schedule method to accomplish this:
When creating plan if the price is provided manually it works. Subscription is created
But for me sometimes there is calculation and amount varies like dynamic subscription and I want price to be dynamic.
But when I make amount" => $price, dynamic I get error
"This payment requires additional user action before it can be completed successfully. Payment can be completed using the PaymentIntent associated with the invoice. For more details see: https://stripe.com/docs/billing/subscriptions/overview#requires-action"
//Fetching intent
$intent = \Stripe\PaymentIntent::retrieve($paymentMethodParams->paymentIntent['id']);
//Create stripe user
$customer = \Stripe\Customer::create(([
'email' => 'evansd#ed.com',
'description' => 'Valued Customer',
'name' => 'Ed Ward',
'payment_method' => $paymentMethodParams->paymentMethod['id'],
'invoice_settings' =>
[
'default_payment_method' => $paymentMethodParams->paymentMethod['id']
]
]));
//Attaching paymentIntent with Customer
\Stripe\PaymentIntent::update($paymentMethodParams->paymentIntent['id'],
[
'customer' => $customer->id,
]
);
$intent = \Stripe\PaymentIntent::retrieve($paymentMethodParams->paymentIntent['id']);
$product = \Stripe\Product::create([
'name' => 'Payment plan for user',
'type' => 'service'
]);
//Creating plan for the customer
$plan = \Stripe\Plan::create(
array(
"product" => [
"name" => '#1 monthly Subscription'
],
"amount" => 200,
'nickname' => 'Payment Plan for order - ',
"currency" => 'GBP',
"interval" => 'month',
"interval_count" => 1,
'metadata' => [
]
)
);
$schedule = \Stripe\SubscriptionSchedule::create([
'customer' => $customer->id,
'start_date' => 'now',
'end_behavior' => 'cancel',
'metadata' => [
'local_id' => 'ABC 12345',
],
'phases' => [
[
'items' => [
[
'plan' => $plan->id,
],
],
'iterations'=>2
],
],
]);
$subscription_id = $schedule->subscription;
$result = \Stripe\Subscription::update($subscription_id,
[
'expand' => ['latest_invoice.payment_intent'],
]
);
$invoice = \Stripe\Invoice::retrieve($result->latest_invoice->id);
$invoice->pay();
What I did so far:
1: I even tried to set the $price = 1 and in phases 'quantity' => $price * $qty so it charges my price.
But then its same again.
This issue is only for certain test cards like : 4000 0027 6000 3184.(issue started when SCA was implemented). The other cards without sca rules works perfectly.
(These card that requires authentication on all transactions, regardless of how the card is set up.)
2:Created plan first and then created customer with the plan just like in mentioned in
Stripe Subscription Plans of Varying Amounts
I think its not user action, but its dynamic plan price issue.
If the price is static, it works and with sca card.
Thanks in advance.

Stripe create a new customer each payement

SOLUTION : Store in db the object Charge with the user id and check each time if we know this user or not :
$customerAlreadyKnown = OrdersModel::checkIfUserKnown($email);
If we know it then we have to update customer source (don't know why) :
if(gettype($customerAlreadyKnown)=='array'){
\Stripe\Customer::update(
$customerAlreadyKnown[0]['user_id'],
['source' => $token]
);
$charge = \Stripe\Charge::create([
'customer' => $customerAlreadyKnown[0]['user_id'],
'amount' => $itemPrice,
'currency' => 'eur',
'description' => $_POST['description'],
'metadata' => array(
'order_id' => $orderID,
),
]);
Else we create a new customer
}else{
// Add customer to stripe
$customer = \Stripe\Customer::create(array(
'email' => $email,
'card' => $token,
));
$charge = \Stripe\Charge::create([
'customer' => $customer->id,
'amount' => $itemPrice,
'currency' => 'eur',
'description' => $_POST['description'],
'metadata' => array(
'order_id' => $orderID,
),
]);
}
You are creating a new Token and Customer for each payment. You should instead identify your existing Customers (store a relation table locally on your application) in some way (e.g. by their email_address or the Token's Card fingerprint). Then instead of using the new Token and a new Customer, retrieve the existing customer as $customer before you charge (their existing Card will be used automatically).

Stripe - Adding user friendly description to Payment intent

I'm trying to work out how to add user friendly description to payment intent ( for example order number instead ... pi_xxxxxxxxxxxx)
$intent = \Stripe\PaymentIntent::create([
'payment_method' => $json_obj->payment_method_id,
'amount' => $json_obj->amount,
'statement_descriptor' => $json_obj->desription,
'receipt_email' => $json_obj->email,
'description' => $json_obj->$description,
'payment_method_types' => ["card"],
'currency' => 'gbp',
'confirmation_method' => 'manual',
'confirm' => true,
]);
it was very easy with Charge Api, any suggestions? Thx in advance
coding error
'description' => $json_obj->$description,
should be
'description' => $json_obj->description,

Resources