【问题标题】:PHP Stripe API error on add payment source from token从令牌添加支付源时出现 PHP Stripe API 错误
【发布时间】:2021-02-08 19:00:34
【问题描述】:

我收到以下错误:-

我认为我没有获得 $payment_source_id,而是无法生成 $payment_source_id,

从这里

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

它对应的函数是:-

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;
}

任何人都可以请您指出哪里出了问题以及应该是什么,我认为这将解决这里的问题,这将是一个很大的帮助。早日回复将不胜感激,在此先感谢。顺便说一下我的条带库是 stripe-php-6.31.0

【问题讨论】:

    标签: php stripe-payments payment-gateway


    【解决方案1】:

    基于错误Call to a member function create() on null,我假设错误源于此行:

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

    这可能是因为客户sources are expandable,这意味着默认情况下不会返回它们。您必须在检索期间显式地request expanded properties。对于stripe-phpchanges in 7.33.0 之前的客户来源,这将是:

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

    对于 7.33.0+,它将是:

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

    我不确定如何将expand 参数与get_customer_object 一起传递,但您需要单独调查。

    【讨论】:

    • 我试图从这里生成源,但不幸的是,我不能:- public static function get_payment_profiles( $profile_id = 0 ) { $sources = $customer->sources->all(); if (empty($sources)) {return array();} $cards = array(); foreach ( $sources->data as $key => $card ) { $desc = ( isset( $card->brand ) ) ? $card->brand : $card->bank_name ; $cards[ $card->id ] = $desc 。 ':'。 $card->last4; }return $cards;}
    • 您能否编辑您的问题以添加有关任何错误或意外结果的一些详细信息?这里的代码很难阅读,而且似乎没有包含任何错误参考。
    • 谢谢,它现在可以工作了。通过 expand => ['sources'] 这一直像冠军一样工作:)
    猜你喜欢
    • 2017-06-18
    • 2019-04-04
    • 1970-01-01
    • 2021-03-22
    • 2019-02-01
    • 1970-01-01
    • 2018-12-27
    • 1970-01-01
    • 2016-02-19
    相关资源
    最近更新 更多