PHP - Stripe API创建卡问题

时间:2015-04-01 14:22:33

标签: php stripe-payments

由于某种原因,我没有很好地实现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

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

3 个答案:

答案 0 :(得分:1)

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

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

在"示例请求"确实它确实如你所做的那样,但是看看左边记录的参数,它实际上应该是" source"不是"卡"

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

答案 1 :(得分:1)

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

$ 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->保存();

我通过在我的html文件中包含条带js文件获得了$ this-> input->帖子(' stripe_token')。

答案 2 :(得分:0)

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

// 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