laravel / cashier-mollie

MIT License
375 stars 63 forks source link

VariableDiscountHandler #155

Open Paulsky opened 4 years ago

Paulsky commented 4 years ago

Does someone already written a VariableDiscountHandler? I mean, instead of coupons with fixed discount, coupons with percentages as discount.

Would you accept a PR if someone writes a VariableDiscountHandler @sandervanhooft ? I think this is a common use case. If someone uses coupons, the options will most of the time be there; fixed or variable discount.

sandervanhooft commented 4 years ago

Would you accept a PR if someone writes a VariableDiscountHandler @sandervanhooft ?

Sure! Don't forget to include tests. FYI I'm in survival mode this week, hope to address most issues next week.

Paulsky commented 4 years ago

Thanks @sandervanhooft ! I will try to create a PR for this! I wish you all the best with your housing unit!! 🙏

GertTimmerman commented 4 years ago

@Paulsky Any progress on this? I need a percentage discount handler for myself, but I don't want to reinvent the wheel ;)

Paulsky commented 4 years ago

@GertTimmerman I haven't started to work on this to be honest. But I'm still interested and willing to develop this. However, I don't know when I can start developing.... Do you have time and resources to work on this? Please let me know if you get started

GertTimmerman commented 4 years ago

@Paulsky I am able to take a look at it, but I can't promise anything. I will let you know (in this thread) if I have made some progress. It's not my top prio to have this.

Paulsky commented 4 years ago

Alright! Let's keep each other posted. I will get in touch if I am able to start working on this. Maybe we can help each other out.

sandervanhooft commented 4 years ago

Hi @GertTimmerman and @Paulsky , how are you doing on this? Need any help?

GertTimmerman commented 4 years ago

Hi @GertTimmerman and @Paulsky , how are you doing on this? Need any help?

I have created a PercentageDiscountHandler, But I have not been able to test everything because you have to do tests with specific mollie orders, etc.

Otto-munch commented 4 years ago

Any status update on a variable discount handler?

Paulsky commented 4 years ago

I'm sorry @Otto-munch , I still haven't got the change to work on this. Maybe you can help @GertTimmerman out? I do remember that writing test for this was a bit difficult.

salmanhijazi commented 3 years ago

Here's how I created a PercentageDiscountHandler for myself. Just extended the FixedDiscountHandler and changed the calculation logic.

<?php

namespace App\Libraries\Cashier\Coupon;

use Laravel\Cashier\Order\OrderItem;
use Laravel\Cashier\Order\OrderItemCollection;
use Laravel\Cashier\Coupon\FixedDiscountHandler as CoreFixedDiscountHandler;
use Money\Money;

class PercentageDiscountHandler extends CoreFixedDiscountHandler
{
    /**
     * @param \Money\Money $base The amount the discount is applied to.
     * @return \Money\Money
     */
    protected function unitPrice(Money $base)
    {
        $discount = $base->multiply( $this->context('discount') * 0.01 );

        if($this->context('allow_surplus', false) && $discount->greaterThan($base)) {
            return $base->negative();
        }

        return $discount->negative();
    }
}

I'm loading coupons from the database. So I added this to my Coupon model to make it work.

/**
     * Builds a Cashier coupon from the current model.
     *
     * @returns \Laravel\Cashier\Coupon\Coupon
     */
    public function buildCashierCoupon(): CashierCoupon
    {
        $context = [
            'description' => $this->campaign->title,
            'discount' => $this->campaign->discount,
        ];

        return (new CashierCoupon(
            $this->name,
            ($this->campaign->discount_type == 'percentage' ? new PercentageDiscountHandler : new FixedDiscountHandler),
            $context))
        ->withTimes($this->campaign->times);
    }

I'm grouping coupons by campaigns and have a discount_type assigned to the campaign.

jasperf commented 3 years ago

Also see https://github.com/laravel/cashier-mollie/pull/291 added by @GertTimmerman