【问题标题】:Stripe API Error - Missing required param: amount - PHP libraryStripe API 错误 - 缺少必需的参数:数量 - PHP 库
【发布时间】:2018-02-09 02:09:26
【问题描述】:

我已经实施了 Stripe 支付,但问题是我不断收到此错误:

{status: 500, error: "Missing required param: amount."}

从代码中可以看出,我有所有的参数,但我仍然不知道为什么会出现这个错误。这是我正在使用的代码:

public function process(){
    try {
        Stripe::setApiKey('sk_text_key');
        $charge = Stripe_Charge::create(array(
                     "amount" => 2000,
                    "currency" => "USD",
                    "card" => $this->input->post('access_token'),
                    "description" => "Stripe Payment"
        ));
    } catch (Stripe_InvalidRequestError $e) {
        // Invalid parameters were supplied to Stripe's API
        echo json_encode(array('status' => 500, 'error' => $e->getMessage()));
        exit();
    }
}   

这是我用来提交表单的 javascript:

$(function() {
    var $form = $('#payment-form');
    $form.submit(function(event) {
        $form.find('.submit').prop('disabled', true);
        $form.find('.submit').val('Please wait...');

        Stripe.card.createToken($form, stripeResponseHandler);
        return false;
    });
});

function stripeResponseHandler(status, response) 
{
    if (response.error) {
        alert(response.error.message);
    } 
    else {
        $.ajax({
            url: '<?php echo base_url('payment/process');?>',
            data: {access_token: response.id},
            type: 'POST',
            dataType: 'JSON',
            success: function(response){
                console.log(response);
                if(response.success)
                window.location.href="<?php echo base_url('booking/thankyou'); ?>";
            },
            error: function(error){
                console.log(error);
            }
        });
    }
}

【问题讨论】:

  • 您使用的是什么版本的 Stripe?
  • 我所看到的 - 您正在尝试将令牌设置为 CARD....在我看来它是“来源”。还有....尝试检查使用的条带版本。就像我这边的例子: $charge = \Stripe\Charge::create(array( "amount" => 20000, // 以美分为单位的金额 "currency" => "usd", "source" => $token, "description " => '额外付款' ));

标签: javascript php codeigniter stripe-payments stripe.js


【解决方案1】:

我看到您使用的是旧代码,在较新版本的 Stripe 中,您可以像这样制作Charge:

// Token is created using Stripe.js or Checkout!
// Get the payment token ID submitted by the form:
$token = $_POST['stripeToken'];

$charge = \Stripe\Charge::create(array(
  "amount" => 2000,
  "currency" => "usd",
  "description" => "Stripe Payment",
  "source" => $token,
));

查看更多示例here。

【讨论】:

  • 找不到类“Stripe\Charge”
  • 这就是我在控制器付款上调用它的方式 require_once(APPPATH . 'libraries/Stripe/lib/Stripe.php');
  • 为什么不使用命名空间?无论如何,尝试包括这个:require_once(APPPATH . 'libraries/Stripe/lib/Charge.php');
  • 好吧,我相信我使用的库不正确,请您参考一下我可以下载的地方
  • 你可以从here获取最新的Stripe.js,从here获取PHP库
猜你喜欢
  • 1970-01-01
  • 2022-01-01
  • 2021-05-09
  • 2023-01-23
  • 2012-06-27
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多