laravel / cashier-mollie

MIT License
376 stars 63 forks source link

Redirect url per plan in the database doesnt seem to be working #88

Closed mikelmao closed 4 years ago

mikelmao commented 4 years ago

I have implemented plans being loaded from the database per issue #69 which seems to be working fine. Though i have added the column firstPaymentRedirectUrl (varchar) and it does not seem to load this.

My DatabasePlanRepository looks like this:

<?php

namespace App\Http\Repositories;

use App\Plan;
use Laravel\Cashier\Plan\Contracts\PlanRepository;

class DatabasePlanRepository implements PlanRepository {
    /**
     * @param string $name
     * @return null|\Laravel\Cashier\Plan\Contracts\Plan
     */
    public static function find(string $name)
    {
        return Plan::where('name', $name)->first();
    }

    /**
     * @param string $name
     * @return \Laravel\Cashier\Plan\Contracts\Plan
     */
    public static function findOrFail(string $name)
    {
        return Plan::where('name', $name)->firstOrFail();
    }
}

And my plan model as followed:

<?php

namespace App;

use Illuminate\Database\Eloquent\Model;
use Laravel\Cashier\Plan\Contracts\Plan as PlanImplements;
use Laravel\Cashier\Order\OrderItemPreprocessorCollection;
use Money\Money;
use Money\Currency;

class Plan extends Model implements PlanImplements
{
    protected $fillable = [
        'name', 'amount', 'interval', 'description', 'currency','firstPaymentDescription','firstPaymentAmount','firstPaymentRedirectUrl'
    ];

    public function getCurrency()
    {
        return $this->attributes['currency'];
    }

    public function getCode()
    {
        return $this->attributes['currency'];

    }

    public function amount()
    {

        $currency = new Currency($this->getCode());
        return new Money($this->attributes['amount'] * 100, $currency);
    }

    /**
     * @param \Money\Money $amount
     * @return \Laravel\Cashier\Plan\Contracts\Plan
     */
    public function setAmount(Money $amount)
    {
        $this->amount = $amount;

        return $this;
    }

    /**
     * @return string
     */
    public function description()
    {
        return $this->attributes['description'];

    }

    /**
     * @return string
     */
    public function interval()
    {
        return $this->attributes['interval'];

    }

    /**
     * @return string
     */
    public function name()
    {
        return $this->attributes['name'];
    }

    /**
     * The amount the customer is charged for a mandate payment.
     *
     * @return \Money\Money
     */
    public function firstPaymentAmount()
    {
        $currency = new Currency($this->getCode());
        return new Money($this->attributes['firstPaymentAmount'] * 100, $currency);
    }

    /**
     * @param \Money\Money $firstPaymentAmount
     * @return Plan
     */
    public function setFirstPaymentAmount(Money $firstPaymentAmount)
    {
        $this->firstPaymentAmount = $firstPaymentAmount;

        return $this;
    }

    /**
     * @return string
     */
    public function firstPaymentMethod()
    {
        return $this->attributes['firstPaymentMethod'];
    }

    /**
     * @param string $firstPaymentMethod
     * @return Plan
     */
    public function setFirstPaymentMethod(?string $firstPaymentMethod)
    {
        $this->firstPaymentMethod = $firstPaymentMethod;

        return $this;
    }

    /**
     * The description for the mandate payment order item.
     *
     * @return string
     */
    public function firstPaymentDescription()
    {

        return $this->attributes['firstPaymentDescription'];
    }

    /**
     * @param string $firstPaymentDescription
     * @return Plan
     */
    public function setFirstPaymentDescription(string $firstPaymentDescription)
    {

        $this->firstPaymentDescription = $firstPaymentDescription;

        return $this;
    }

    /**
     * @return string
     */
    public function firstPaymentRedirectUrl()
    {
        return $this->firstPaymentRedirectUrl;
    }

    /**
     * @param string $redirectUrl
     * @return Plan
     */
    public function setFirstPaymentRedirectUrl(string $redirectUrl)
    {
        $this->firstPaymentRedirectUrl = $redirectUrl;

        return $this;
    }

    /**
     * @return string
     */
    public function firstPaymentWebhookUrl()
    {
        return config('cashier.first_payment');

    }

    /**
     * @param string $webhookUrl
     * @return Plan
     */
    public function setFirstPaymentWebhookUrl(string $webhookUrl)
    {

    }

    /**
     * @return \Laravel\Cashier\Order\OrderItemPreprocessorCollection
     */
    public function orderItemPreprocessors()
    {
        return $this->orderItemPreprocessors;
    }

    /**
     * @param \Laravel\Cashier\Order\OrderItemPreprocessorCollection $preprocessors
     * @return \Laravel\Cashier\Plan\Contracts\Plan
     */
    public function setOrderItemPreprocessors(OrderItemPreprocessorCollection $preprocessors)
    {

        $this->orderItemPreprocessors = $preprocessors;

        return $this;
    }
}

As you can see i have added the firstPaymentRedirectUrl and setFirstPaymentRedirectUrl methods. Though upon succesfully making a payment through the redirect URL, it will send me to my localhost, which seems to be loaded from config/cashier.php file

'redirect_url' => config('app.url'),

My app.url is http://localhost

Any clues on how I can achieve loading the redirect url based on the plan's firstPaymentRedirectUrl in the DB?

EDIT: Formatted code

sandervanhooft commented 4 years ago

Confirmed, it's a bug.

sandervanhooft commented 4 years ago

It seems The FirstPaymentSubscriptionBuilder is not using the firstPayment overrides from the Plan.

sandervanhooft commented 4 years ago

Will look into this.

mikelmao commented 4 years ago

I have created my first ever pull request with the fix. Im not sure if i did it right, i followed a youtube video how to make pull requests on github xD @sandervanhooft

sandervanhooft commented 4 years ago

Just pushed a fix to develop. Your PR was definitely heading in the right direction, but I needed additional tests and some more to fix the whole problem.