我尝试使用条带在我的应用程序中使用Laravel制作小额付款表单。我甚至跟着拉克拉斯特的教程。我收到此错误
Stripe_InvalidRequestError
You must supply either a card or a customer id
//billing.js
(function(){
var StripeBilling = {
init: function(){
this.form=$('#billing-form');
this.submitButton = this.form.find('input[type=submit]');
this.submitButtonValue = this.submitButton.val();
var stripeKey=$('meta[name="publishable-key"]').attr('content');
Stripe.setPublishableKey(stripeKey);
this.bindEvents();
},
bindEvents: function(){
this.form.on('submit', $.proxy(this.sendToken, this));
},
sendToken: function(event){
this.submitButton.val('One Moment').prop('disabled', true);
Stripe.createToken(this.form, $.proxy(this.stripeResponseHandler, this) );
event.preventDefault();
},
stripeResponseHandler: function(status, response){
if(response.error){
this.form.find('.payment-errors').show().text(response.error.message);
return this.submitButton.prop('disabled', false).val(this.submitButtonValue);
}
$('<div>', {
type: 'hidden',
name: 'stripe-token',
value: response.id
}).appendTo(this.form);
this.form[0].submit();
}
};
StripeBilling.init();
})();
//StripeBilling.php
<?php
namespace Acme\Billing;
use Stripe;
use Stripe_Charge;
use Config;
class StripeBilling implements BillingInterface {
public function __construct()
{
Stripe::setApiKey(Config::get('stripe.secrete_key'));
}
public function charge(array $data)
{
try
{
return Stripe_Charge::create([
'amount' => 1000, // $10
'currency' => 'usd',
'description' => $data['email'],
'card'=>$data['token']
]);
}
catch(Stripe_CardError $e)
{
dd('Card was declined');
}
}
}
可能是什么问题?我甚至从github采取了相同的代码,但同样的错误。一切都与拉克拉斯特的相同。有什么想法吗?
答案 0 :(得分:1)
修改2:您在secrete_key
中使用了Stripe::setApiKey(Config::get('stripe.secrete_key'))
- 它应该是secret_key
吗?
修改1:您的billing.js和laracasts之间的唯一区别&#39;是Stripe.createToken
末尾的两个右括号之间的空格:
Stripe.createToken(this.form, $.proxy(this.stripeResponseHandler, this) );
假设这没有解决问题,您是否尝试在处理费用之前创建了Stripe客户?我有一个类似的系统(来自同一个Laracast),它首先创建了一个客户:
public function createStripeCustomer($email, $token)
{
$key = Config::get('stripe.secret');
Stripe::setApiKey($key);
$customer = Stripe::customers()->create([
'card' => $token,
'email' => $email,
'description' => 'desc'
]);
// error checking
return $customer['id'];
您希望返回客户ID,然后在Stripe_Charge
数组中使用
return Stripe_Charge::create(
[
'amount' => 1000, // $10
'currency' => 'usd',
'customer' => $customer['id'],
'description' => $data['email'],
'card'=>$data['token']
]);