laravel / cashier-paddle

Cashier Paddle provides an expressive, fluent interface to Paddle's subscription billing services.
https://laravel.com/docs/cashier-paddle
MIT License
238 stars 57 forks source link

Get subscription payment method #240

Closed gkrteam closed 7 months ago

gkrteam commented 7 months ago

Hi,

I tried to get the payment method of a user for a subscription. I searched in the Paddle docs and it seems that payment methods are linked on transactions and not on subscriptions.

So I added the following code in the subscription class (based on what had been done for Cashier Paddle V1):

    /**
     * Get the payment method details from the subscription.
     *
     * @return array
     */
    public function paymentMethodDetails()
    {
        $paymentAttempts = $this->transactions()->orderByDesc('billed_at')->first()->asPaddleTransaction()['payments'];
        $paymentMethodDetails = collect($paymentAttempts)->whereIn('status', ['captured', 'authorized'])->first()['method_details'] ?? '';

        return $paymentMethodDetails;
    }

    /**
     * Get the payment method type from the subscription.
     *
     * @return string
     */
    public function paymentMethod()
    {
        return $this->paymentMethodDetails()['type'] ?? '';
    }

    /**
     * Get the card type from the subscription.
     *
     * @return string
     */
    public function cardType()
    {
        return $this->paymentMethodDetails()['card']['type'] ?? '';
    }

    /**
     * Get the last four digits from the subscription if it's a credit card.
     *
     * @return string
     */
    public function cardLastFour()
    {
        return $this->paymentMethodDetails()['card']['last4'] ?? '';
    }

    /**
     * Get the card expiry month.
     *
     * @return string
     */
    public function cardExpiryMonth()
    {
        return $this->paymentMethodDetails()['card']['expiry_month'] ?? '';
    }

    /**
     * Get the card expiry year.
     *
     * @return string
     */
    public function cardExpiryYear()
    {
        return $this->paymentMethodDetails()['card']['expiry_year'] ?? '';
    }

    /**
     * Get the cardholder's name.
     *
     * @return string
     */
    public function cardholderName()
    {
        return $this->paymentMethodDetails()['card']['cardholder_name'] ?? '';
    }

In the paymentMethodDetails function, I filtered by captured and authorized statuses because I noticed that the authorized status was returned when a transaction was made to update the payment method.

I don't know if this is the way to go, I'd like to get some feedback on this. Thanks!

driesvints commented 7 months ago

Unfortunately it's not possible to retrieve the payment method for subscriptions in Paddle Billing. If you'd like this feature, I suggest you contact Paddle to add this to their API's so we can retrieve it.