2015-04-01 70 views
1

由於某些原因,我沒有很好地實現Stripe API。我一直致力於創建客戶和收費標準,但是當我嘗試向現有客戶添加新卡時,我無法使其工作。PHP - 使用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 

任何幫助,將不勝感激。謝謝!

+0

'缺少必需的參數:源',可能會給你一些東西.. – JonasCz 2015-04-01 14:28:49

回答

1

我認爲條紋示例文檔已過時或不正確。放眼下「定義」的文檔應該是:

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

在「實例的請求」,它確實有它完全按照你正在做的,但看着記錄到左邊的參數,它實際上應該是「源」而不是「卡」

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

+0

謝謝。但我只是試着將它換掉,我仍然得到相同的錯誤。 '$ customer = Stripe_Customer :: retrieve($ user_data [「stripe_customer_id」]);' ''$ customer-> sources-> create(array(「source」=> $ token));' – libradog 2015-04-01 15:10:25

+0

你打算怎麼樣關於獲取卡數據。它是用stripe.js創建的嗎?或者你是否將所有卡數據從你自己的表單傳遞到後端?我懷疑這個問題不在API調用中,而是你正在傳遞的內容 – 2015-04-01 15:38:54

+0

你是對的。我正在使用自定義表單來嘗試它。當我使用基本的嵌入式表單時,它工作正常。我會調查,看看我能否改正這個問題。謝謝! – libradog 2015-04-01 20:36:13

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 

我懷疑問題可能是條紋PHP API

+0

說你有同樣的問題不應該張貼在答案部分,請發佈一個新的問題或使用評論部分這樣的事情。 – rapid3642 2017-04-28 08:02:54

1

我用下面的代碼添加了一個新的卡到現有的客戶在

$ customer_stripe_data = \ Stripe \ Customer :: retrieve('customer stripe id'); (array)(「source」=> $ this-> input-> post('stripe_token')));}創建一個數組,

$ customer_stripe_data-> save();

我得到了$ this-> input-> post('stripe_token')通過在我的html文件中包含條紋js文件。