Stripe: Create a Charge with Coupon - stripe-payments

Could you guy please show me how to create a Stripe Charge which can apply the discount automatically.
I have a valid coupon code (expired in far future, forever use, discount $2).
I create a new Stripe user and assign that Coupon Code for him.
I make a Charge with that customer and some money says: amount = $10.
All the thing works. When I login to Stripe Dashboard, I still see the new user in the list and he is using that Coupon Code. However in the payment, he still pay me $10 instead of $8.
I would like to make a Charge with amount = $10, however Stripe will do discounting so the true Charge will pay $8 only.
$myCard = array(
'number' => '4242424242424242',
'exp_month' => 12,
'exp_year' => 16
);
$coupon = Coupon::retrieve('6868');
//Valid coupon
$stripe_customer = Customer::create(array(
'card' => $myCard,
'email' => 'cus#info.com',
'coupon' => $coupon
));
$charge = Charge::create(array(
'customer' => $stripe_customer->id,
'amount' => 1000,
'currency' => 'usd'
));

Stripe does not allow coupons/discounts for one time payments because you don't really need Stripe to handle your coupons for one-time payments.
You need coupons for subscriptions because Stripe generates a subscription's charges automatically, by itself, on its own schedule. That means your code has no opportunity to modify the amount of the charge itself. Instead, you have to modify the subscription, which then modifies the charges it generates in the future. Stripe offers three simple ways to do this: changing the plan, setting the account balance, and adding invoice items. However, none of them allow you to specify a percentage discount or apply a modification for more than just the next invoice but less than forever. Coupons fix that issue.
A one-time charge, on the other hand, is always generated by you, so you always have an opportunity to modify the amount of the charge yourself. The problem that coupons are trying to solve doesn't exist here, so they aren't offered as an option.
reference : click here

Related

stripe first time the user pays for a subscription

If this is the first time this specific customer pays for this specific subscription.
I must set the cancel_at field of the subscription based on the chosen plan ID. (I don't want to set it upon the creation of the subscription). To tackle this first time , I have captured the invoice.payment_succeeded event and I did the following:
$invoices = \Stripe\Invoice::all([
"limit" => 3, //no need to get all of the invoices just checking if their number is = 1.
"status" => 'paid',
"customer" => $customer_id,
"subscription" => $subscription_id,
"expand" => ["data.charge"],
]);
if ( count($invoices->data) == 1 ) {
//First time this user pays for this specific subscription;
Can You just tell me if it is right or if I missed anything as I am new to php as well as stripe. should I be doing count($invoices) instead of what i did ?
Your code looks fine, but the only way to tell if it'll work or not is for you to run it and see if it does what you expect it to do. You can use your test mode API keys and the Stripe CLI to test the flow:
stripe trigger customer.subscription.created
This will create a test customer, a plan and a subscription. The latter will immediately create and pay an invoice, so you should get a invoice.payment_succeeded webhook event to test with.

Stripe Upcoming Invoice API while in trial period

I'd like to show a customer a preview (or a quote) of what their subscription charges will be if they make changes to their subscription. For this, I'm using the "upcoming invoice" API endpoint:
https://stripe.com/docs/api/invoices/upcoming
From that link, they state that "You can preview the effects of updating a subscription, including a preview of what proration will take place.".
When changing plans, you have to first fetch the current subscription item ID (https://stripe.com/docs/api/subscription_items), and specify "deleted" => true. Then you add the new plan as a subscription_item.
Here's an example call:
[
"subscription" => "sub_GUw5iYoBYiSAEl"
"subscription_items" => [
[
"id" => "si_GUw53rbtqOpZYB"
"deleted" => true
],
[
"plan" => "plan_GAf8EkL1VIPYZ9"
"quantity" => "1"
]
]
]
This works great when the user is not in their trial period.
However, if the user is in their trial period, this only works if the billing interval of the plan you are changing to is the same. E.g. if you are on a monthly plan, changing to another monthly plan works.
If you are in your trial period on a monthly plan, and you are changing to a yearly plan (or vice versa), then it shows the upcoming invoice as $0.00 due today.
This also happens via the Stripe Dashboard:
Is there a way to show the actual amount, not $0.00?
You can pass subscription_trial_end to see what it would look like if the trial ended immediately.

Stripe immediate payment on each quantity update

We are using Stripe as the payment gateway, and we have a yearly plan i.e .billing cycle of the plan is 1 year.
Within the billing cycle, the user can update opt for more seats which will result in increasing the quantity for the subscribed plan.
Subscription subscription = Subscription.retrieve(paymentDetails.getSubscriptionId());
int currentQuentity = subscription.getQuantity();
Map<String, Object> updateParams = new HashMap<String, Object>();
updateParams.put("quantity", (currentQuentity + changeInQuantity));
subscription.update(updateParams);
So for any update stripe refunds(if quantity reduces) and charges(if quantity increases) at the end of billing cycle prorated.
In our business logic, we need immediate payment (charges or refunds) on each quantity update rather then on end of billing cycle. Is there any way in the stripe to achieve so.
I spent a lot of time trying to figure this one out, I hope I can save someone else some time. To take payment for a change of quantity or subscription plan, you need to do the following:
Update subscription with the new changes.
Create an invoice.
The thing that confused me by this is that Stripe magically knows to only invoice for the updated subscription items, and not for upcoming billing cycle items.
Retrieve the newly created invoice
Finalise the invoice
Take payment for the invoice This assumes that your customer has a payment method stored.
Only when all of the steps have been completed, you've successfully charged for the subscription change (and charged for the change only). You can do it all of it in one go. The customer should get an invoice email from Stripe at the end of it.
What makes matters complicated for me, is that reading from the documentation, I got the impression that Stripe will automatically do all of this for me as long as the subscription is set to "billing": "charge_automatically". This is not the case, and in Stripe's defence, they mention it at the bottom of the documentation page related to upgrading and downgrading plans:
If you want the customer to immediately pay the price difference when switching to a more expensive plan on the same billing cycle, you need to generate an invoice after making the switch.
To charge immediately after subscription update, you need to create an invoice just after the subscription update. This will result in an immediate charge
To create invoice you can simply call:
Stripe.apiKey = "sk_test_BQokikJOvBiI2HlWgH4UWQfQ2";
Map<String, Object> invoiceParams = new HashMap<String, Object>();
invoiceParams.put("customer", "cus_D2XUIsncG7YUX");
Invoice.create(invoiceParams);
Ref (https://stripe.com/docs/api#update_subscription):
The accepted answer is the solution if you want to maintain the same billing dates for the subscription.
A simpler solution if you need to push the billing cycle forward to that of the date of the change is to simply set the 'billing_cycle_anchor' to 'now' when updating the subscription. Stripe will immediately charge the user and apply and prorations.
example in python
stripe.Subscription.modify(
subscription_id,
billing_cycle_anchor = 'now',
......
)
You can also set proration_behavior to always_invoice and Stripe will apply both a proration and immediately invoice your customer (source).
Here's an example in curl:
curl https://api.stripe.com/v1/subscriptions/sub_HE... \
-u "sk_test_MI...:" \
-d "items[0][id]=si_Il3Z..." \
-d "items[0][quantity]"=2 \
-d "proration_behavior=always_invoice"

Displaying a fake price in Stripe

I'm using Stripe's Checkout.
There is nothing that prevent me (apart my honesty) to display a low price to the customer, and then charge a higher price without his consent.
Did I miss something ? Is that an industry standard ? I believe it's not.
Why the token did not contain the user validated amount ?
Is it only in test mode ?
Thank you.
For exemple, this works (in test mode at least). I can see the high amount in my dashboard.
<script
src="https://checkout.stripe.com/checkout.js" class="stripe-button"
data-key="my_key"
data-amount="10"
data-currency="eur">
</script>
Then in the server side :
$charge = \Stripe\Charge::create(array(
"amount" => 10000,
"currency" => "eur",
"source" => $_REQUEST['stripeToken'],
));
You didn't miss anything -- it's up to each merchant to correctly display the amount and currency that will be used when actually charging their customers.
Failing to do would very likely result in disputes and chargebacks from your customers, which in turn could lead to your Stripe account being closed.

Stripe subscription

I wanted to know if it is possible to not take credit card info when one of the drop down options for a subscriptions is a free plan.
So basically I have two plans. First is the basic plan and is free. The second is the premium plan and cost $20. When someone chooses the premium plan we capture their credit card info.
Now if someone chooses the free plan, is it possible to hide the credit card input fields when someone chooses the basic? And will stripe execute on it? Is there such an option on stripe?
thanks,
Yes you can hide the credit card option if this is a free subacription.
If you are using stripe.js then you can simply dissable the form based on your subscription plan.
Free subscriptions in Stripe don't require a card. Your application would need to recognize its a free plan and not require the credit card
yes You can do it based on your subscription plan.
you can create Free and Pro plan(paid) and take those to your drop down list.
when user select free plan hide your payment information capturing part and go further.
yeah that is possible,
try to use this example:
1:first create plan (In plan we can pass amount 0 for free subscription )
2:create customer
3:create subscription
public function createPlan($data)
{
$plan=\Stripe\Plan::create(array(
"amount" => $data['Amount'],
"interval" => $data['Interval'],
"name" => $data['planName'],
"currency" => "usd",
"id" => $data['planName'])
);
}
/*
* createcustomer function used to create customer for implementing recurring transactions rather asking again and again card details
*/
public function createCustomer($data,$token=null)
{
$customer=\Stripe\Customer::create(array(
"email"=>$data['email'],
"description" => $data['name'],
"source" => $token // obtained with Stripe.js
));
$this->subscribe($customer,$data);
}
public function subscribe($customer,$data)
{
$subsc=\Stripe\Subscription::create(array(
"customer" => $customer->id,
"plan" => $data['planName']
));
\Stripe\Subscription::create(array(
"customer" => customer['id'],
"plan" => "home-delivery",
));
}

Resources