Thursday, January 20, 2022

[FIXED] Passing Stripe Payment Methods to Laravel View

Issue

I am trying to load previously used cards using the Stripe API using Laravel using this link from Stripe https://stripe.com/docs/api/payment_methods/list?lang=php

Here is the snippet of code from my controller:

 $stripe = new \Stripe\StripeClient(
            'sk_test_51GueZuLq4MEy
          );


    $customer_id = "cus_HhnBT9fpjxW3hn";
    
    $paymentMethods = $stripe->paymentMethods->all([
            'customer' => $customer_id,
            'type' => 'card',
          ]);
    
    $pm = ($paymentMethods->data);
    
    return view('payment.details', $pm);

However when I am trying to pass the card data into my view I am unable to do so. The variable I am passing in views is:

{{ $pm }}

The error message I get is that my variable is not recognised. The data I am trying to access is $paymentmethods->-data->card->last4

Any help is always appreciated


Solution

$stripe->paymentMethods->all returns an object with a data attribute, where the data is an array of PaymentMethods, in this case, you'll likely want to access the first element of data.

Try updating to:

$pm = $paymentMethods->data[0];


Answered By - cjav_dev

No comments:

Post a Comment

Note: Only a member of this blog may post a comment.