bond james
bond james

Reputation: 23

PHP Stripe API error on add payment source from token

I am getting the below errorStripe api error:-

I Think I am not getting the $payment_source_id, rather am unable to generate the $payment_source_id,

from this

$payment_source_id = $this->add_payment_source_from_token( $customer_id, $token_id, 
$invoice ); 

Its corresponding function is :-

private function add_payment_source_from_token( $customer_id, $token_id, SI_Invoice 
$invoice ) {
$customer = self::get_customer_object( $customer_id );
if ( ! $customer ) {
do_action( 'si_error', __CLASS__ . '::' . __FUNCTION__ . ' response: ', $customer );
return;
}
self::setup_stripe();
try {
$card = $customer->sources->create( array( 'source' => $token_id ) );
} catch (Exception $e) {
self::set_error_messages( $e->getMessage() );
return false;
}
$source_id = $card->id;
if ( ! isset( $_POST['sa_credit_store_payment_profile'] ) && ! isset( $checkout- 
>cache['sa_credit_store_payment_profile'] ) ) 
{Sprout_Billings_Profiles::hide_payment_profile( $source_id, $invoice->get_id() );}
return $source_id;
}

Any one could you please point out where am going wrong and what it should be, I think that will resolve the issue here, it will be a great help. Early response will be highly appreciated, Thanks in advance. By the way My stripe library is stripe-php-6.31.0.

Upvotes: 1

Views: 668

Answers (1)

Nolan H
Nolan H

Reputation: 7419

Based on the error Call to a member function create() on null, I am assuming the error originates on this line:

$card = $customer->sources->create( array( 'source' => $token_id ) );

This is likely because the Customer sources are expandable, meaning they are not returned by default. You must explicitly request expanded properties during retrieval. For Customer sources in stripe-php prior to the changes in 7.33.0 this would be:

\Stripe\Customer::retrieve([
  'id' => 'cus_123',
  'expand' => ['sources']
]);

For 7.33.0+ it would be:

$stripe->customers->retrieve(
  'cus_123',
  ['expand' => ['sources']]
);

I'm not sure how you can pass the expand parameter with get_customer_object but you'll need to investigate that separately.

Upvotes: 2

Related Questions