【问题标题】:PHP - Issue with Stripe API Create CardPHP - Stripe API 创建卡的问题
【发布时间】:2015-04-01 14:22:32
【问题描述】:

由于某种原因,我在实现 Stripe API 方面做得不是很好。我一直在努力创建客户并收取费用,但是当我尝试向现有客户添加新卡时,我无法让它发挥作用。

这是 API 的基本实现(足够简单),但它不断提供错误:

$customer = Stripe_Customer::retrieve($stripe_customer_id);
$customer->sources->create(array("card" => $token));

https://stripe.com/docs/api#create_card

错误日志:

PHP Fatal error:  Uncaught exception 'Stripe_InvalidRequestError' with message 'Missing required param: source' in / ... /scripts/stripe/stripe_library/lib/Stripe/ApiRequestor.php:142
Stack trace: 
0 / ... /scripts/stripe/stripe_library/lib/Stripe/ApiRequestor.php(254): Stripe_ApiRequestor->handleApiError('{\n  "error": {\n...', 400, Array) 
1 / ... /scripts/stripe/stripe_library/lib/Stripe/ApiRequestor.php(104): Stripe_ApiRequestor->_interpretResponse('{\n  "error": {\n...', 400)
2 / ... /scripts/stripe/stripe_library/lib/Stripe/List.php(19): Stripe_ApiRequestor->request('post', '/v1/customers/c...', Array)
3 / ... /join/update-card-stripe.php(34): Stripe_List->create(Array)
4 {main} thrown in / ... /scripts/stripe/stripe_library/lib/Stripe/ApiRequestor.php on line 142

任何帮助将不胜感激。谢谢!

【问题讨论】:

  • Missing required param: source,可能会给你一些继续的东西..

标签: php stripe-payments


【解决方案1】:

我认为条纹示例文档已过时或不正确。查看“定义”下的文档应该是:

$customer->sources->create(array("source" => $token));

在“示例请求”下,它确实与您所做的完全一样,但是查看左侧记录的参数,它实际上应该是“源”而不是“卡”

https://stripe.com/docs/api/php#create_card

【讨论】:

  • 谢谢。但我只是尝试换掉它,我仍然得到同样的错误。 $customer = Stripe_Customer::retrieve($user_data["stripe_customer_id"]);$customer->sources->create(array("source" => $token));
  • 您打算如何获取卡数据。它是用 stripe.js 创建的吗?还是您将所有卡片数据从您自己的表单传递到后端?我怀疑问题不在于 API 调用,而在于您传递的内容
  • 你是对的。我正在使用自定义表单来尝试它。当我使用基本的嵌入式表单时,它工作正常。我将对此进行调查,看看我是否可以纠正这个问题。谢谢!
【解决方案2】:

我使用以下代码向现有客户添加了一张新卡:

$customer_stripe_data = \Stripe\Customer::retrieve('customer stripe id');

$createdCard = $customer_stripe_data->sources->create(array("source" => $this->input->post('stripe_token')));

$customer_stripe_data->save();

通过在我的 html 文件中包含条带 js 文件,我得到了 $this->input->post('stripe_token')

【讨论】:

    【解决方案3】:

    我已由客户创建并尝试使用源令牌注册卡

    // create a new customer if our current user doesn't have one
    $stripe_customer = \Stripe\Customer::create(array(
            'source' => $token,
            'description' => $displayname,
            'metadata' => array('BHAA_ID'=>$bhaa_id),
            'email' => $email
        )
    );
    $stripe_customer_id=$stripe_customer->id;
    update_user_meta( $bhaa_id, 'stripe_customer_id', $stripe_customer_id );
    error_log('$stripe_customer_id '.$stripe_customer_id);
    
    // http://stackoverflow.com/questions/29393338/php-issue-with-stripe-api-create-card
    $customer = \Stripe\Customer::retrieve($stripe_customer_id);
    $card = $customer->sources->create(array("source" => $token));
    error_log('card '.$card->id);
    

    但我得到同样的错误

    [16-Dec-2015 18:54:12 UTC] PHP Fatal error:  Uncaught exception 'Stripe\Error\InvalidRequest' with message 'Missing required param: source.' in /var/www/wp-content/plugins/events-manager-pro-stripe-token-gateway-master/vendor/stripe/stripe-php/lib/ApiRequestor.php:103 from API request 'req_7Xq54M2AlKDijE'
    Stack trace:
    #0 /var/www/wp-content/plugins/events-manager-pro-stripe-token-gateway-master/vendor/stripe/stripe-php/lib/ApiRequestor.php(217): Stripe\ApiRequestor->handleApiError('{?  "error": {?...', 400, Array, Array)
    #1 /var/www/wp-content/plugins/events-manager-pro-stripe-token-gateway-master/vendor/stripe/stripe-php/lib/ApiRequestor.php(60): Stripe\ApiRequestor->_interpretResponse('{?  "error": {?...', 400, Array)
    #2 /var/www/wp-content/plugins/events-manager-pro-stripe-token-gateway-master/vendor/stripe/stripe-php/lib/ApiResource.php(108): Stripe\ApiRequestor->request('post', '/v1/customers/c...', Array, Array)
    #3 /var/www/wp-content/plugins/events-manager-pro-stripe-token-gateway-master/vendor/stripe/stripe-php/lib/ApiResource.php(99): Stripe\Ap in /var/www/wp-content/plugins/events-manager-pro-stripe-token-gateway-master/vendor/stripe/stripe-php/lib/ApiRequestor.php on line 103
    

    我怀疑问题可能出在 Stripe PHP API 中

    【讨论】:

    • 说你有同样的问题不应该在答案部分发布,请发布一个新问题或使用评论部分进行类似的事情。
    猜你喜欢
    • 2019-11-26
    • 2022-01-26
    • 2019-08-03
    • 2019-10-07
    • 2019-01-12
    • 1970-01-01
    • 1970-01-01
    • 2021-02-17
    • 1970-01-01
    相关资源
    最近更新 更多