enupal / stripe

Allows customers sign up for recurring and one-time payments with Stripe, perfect for orders, donations, subscriptions, and events. Create simple payment forms in seconds easily without coding. For Craft CMS
https://enupal.com/craft-plugins/stripe-payments/
Other
33 stars 19 forks source link

Apply percentage discount to options #373

Closed mark-chief closed 11 months ago

mark-chief commented 1 year ago

Description

How can I apply a flat 20% discount to the cart? I have tried to add it as a negative line item but stripe doesn't allow negatives.

I have also tried adjusting the total in the options but it still calculates the total base don the lineitems.

{% if loop.first and discountPercentage > 0 %}

{% set lineItems = lineItems|merge([{
name : 'Volume Discount -'~ discountPercentage ~'%',
amount :  discountAmount|number(decimals=2)|replace('.',''),
currency : 'gbp',
quantity : 1,
}]) %}

{% set lineItems = lineItems|merge([{
name : entry.title,
description : 'Price Inc. VAT',
amount : lineItemPrice|number(decimals=2)|replace('.',''),
currency : 'gbp',
quantity : 1,
}]) %}

{% else %}

{% set lineItems = lineItems|merge([{
name : entry.title,
description : 'Price Inc. VAT',
amount : lineItemPrice|number(decimals=2)|replace('.',''),
currency : 'gbp',
quantity : 1,
}]) %}

{% endif %}

{% endfor %}                                 

{% set options = {
amount: discountedPriceIncVat, 
quantity: completedEntriesCounter, 
itemName : currentUser.firstName ~ ' ' ~ currentUser.lastName,
itemDescription : 'Test Description',
checkoutSuccessUrl: '/account/manage-entries?msg=order-success',
checkoutCancelUrl: '/account/manage-entries',
removeDefaultItem: true,   
allowPromotionCodes : true, 
lineItems: lineItems, 
calculateFinalAmount: false,
} %}

calculateFinalAmount: false, doesnt appear to work, if I think I know what its supposed to do.. but no info in docs on that

Steps to reproduce

1. 2.

Additional info

andrelopez commented 11 months ago

Hi @mark-chief you can use the beforeCreateSession Event If you want to create a session with an applied discount (docs), in your custom module/plugin you may need something like:

use enupal\stripe\services\Checkout;
use enupal\stripe\events\CheckoutEvent;
use enupal\stripe\Stripe;
use craft\base\Plugin;
use Craft;

class YourPlugin extends Plugin
{
    public function init()
    {
        ....
        ....

         Event::on(Checkout::class, Checkout::EVENT_BEFORE_CREATE_SESSION, function(CheckoutEvent $e) {
              $isCart = $e->isCart;
              $sessionParams = $e->sessionParams;
              //overwrite $sessionParams
              $sessionParams['discounts'] = [['coupon' => 'COUPON_ID']];
              $e->sessionParams = $sessionParams;
         });
        ...
        ...        
     }
}   

You have full control here about your checkout session, here the stripe docs with all the properties you can overwrite