【问题标题】:Stripe API Status 200 OK No Event CreatedStripe API 状态 200 OK 未创建事件
【发布时间】:2018-02-20 03:23:33
【问题描述】:

我正在使用带有测试密钥的 Stripe API。我已将付款表单成功发布到 Stripe API 端点,但是没有创建任何事件。当我检查 Stripe 事件/日志时,我可以在我提交的测试付款的日志选项卡下看到 200 - Ok 成功消息,但是事件选项卡仍然为空。似乎无法弄清楚这一点。我的代码如下。

这是我用于处理付款的 php:

function wpay_stripe_process_payment() {
    if(isset($_POST['action']) && $_POST['action'] == 'stripe' && wp_verify_nonce($_POST['stripe_nonce'], 'stripe-nonce')) {

        global $stripe_options;

        // load the stripe libraries
        require_once(STRIPE_BASE_DIR . '/lib/Stripe.php');

        // retrieve the token generated by stripe.js
        $token = $_POST['stripeToken'];

        // check if we are using test mode
        if(isset($stripe_options['test_mode']) && $stripe_options['test_mode']) {
            $secret_key = $stripe_options['test_secret_key'];
        } else {
            $secret_key = $stripe_options['live_secret_key'];
        }

        // attempt to charge the customer's card
        try {
            Stripe::setApiKey($secret_key);
            $charge = Stripe_Charge::create(array(
                    'amount' => 1000, // $10
                    'currency' => 'usd',
                    'card' => $token
                )
            );


            // redirect on successful payment
            $redirect = add_query_arg('payment', 'paid', $_POST['redirect']);

        } catch (Exception $e) {
            // redirect on failed payment
            $redirect = add_query_arg('payment', 'failed', $_POST['redirect']);
        }

        // redirect back to our previous page with the added query variable
        wp_redirect($redirect); exit;
    }
}
add_action('wpay_stripe_process_payment');

这是我发送付款信息的 js:

Stripe.setPublishableKey(stripe_vars.publishable_key);

function stripeResponseHandler(status, response) {
    if (response.error) {
        // show errors returned by Stripe
        jQuery(".payment-errors").html(response.error.message);
        // re-enable the submit button
        jQuery('#stripe-submit').attr("disabled", false);
    } else {
        var form$ = jQuery("#stripe-payment-form");
        // token contains id, last4, and card type
        var token = response['id'];
        // insert the token into the form so it gets submitted to the server
        form$.append("<input type='hidden' name='stripeToken' value='" + token + "'/>");
        // and submit
        form$.get(0).submit();
    }
}
jQuery(document).ready(function($) {
    $("#stripe-payment-form").submit(function(event) {
        // disable the submit button to prevent repeated clicks
        $('#stripe-submit').attr("disabled", "disabled");

        // send the card details to Stripe
        Stripe.createToken({
            number: $('.card-number').val(),
            cvc: $('.card-cvc').val(),
            exp_month: $('.card-expiry-month').val(),
            exp_year: $('.card-expiry-year').val()
        }, stripeResponseHandler);

        // prevent the form from submitting with the default action
        return false;
    });
});

JSON 响应:

{
  "id": "tok_1B12dQG6oQhg3oEDNlZLFORy",
  "object": "token",
  "card": {
    "id": "card_1B12dQG6oQhg3oEDIQa4fiCe",
    "object": "card",
    "address_city": null,
    "address_country": null,
    "address_line1": null,
    "address_line1_check": null,
    "address_line2": null,
    "address_state": null,
    "address_zip": null,
    "address_zip_check": null,
    "brand": "Visa",
    "country": "US",
    "cvc_check": "unchecked",
    "dynamic_last4": null,
    "exp_month": 4,
    "exp_year": 2018,
    "funding": "credit",
    "last4": "4242",
    "metadata": {
    },
    "name": null,
    "tokenization_method": null
  },
  "client_ip": "187.232.128.105",
  "created": 1505177536,
  "livemode": false,
  "type": "card",
  "used": false
}

【问题讨论】:

  • 能否在 200 - OK 日志中添加 JSON 响应?
  • @spartacus 我已经用 JSON 响应更新了帖子

标签: javascript php wordpress api stripe-payments


【解决方案1】:

根据 Stripe 文档:https://stripe.com/docs/charges

这个:

$charge = Stripe_Charge::create(array(
    'amount' => 1000, // $10
    'currency' => 'usd',
    'card' => $token // I think the key here needs to be "source"
));

应该是这样的:

$charge = Stripe_Charge::create(array(
    'amount' => 1000, // $10
    'currency' => 'usd',
    'source' => $token
));

对我来说,我使用的是他们提供的 Stripe PHP 库,而不是:

Stripe_Charge::create(...);

我正在使用:

\Stripe\Charge::create(...);

但我真的认为问题可能是您在收费时需要使用“来源”,而不是“卡”。

【讨论】:

    【解决方案2】:

    您的 Stripe 令牌位于 response.id,而不是 response['id']

    // token contains id, last4, and card type
    var token = response.id;
    

    响应是一个 JSON 对象,因此您必须像遍历任何其他 js 对象一样遍历该对象。

    【讨论】:

    • 实际上,response['id'] 工作得很好。我用这种方式没有问题。
    • 我的前端的 Stripe API 使用括号表示法失败。但是,我认为您的回答可能触及了真正的问题。它应该是(在 PHP 中)“源代码”,而不是“卡片”。支持你的答案。
    • 其实既然提到了,应该就是response.id。甚至 Stripe 文档也以这种方式显示。我也给你投票了。这是相关的,应该得到解决。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-04-09
    • 2016-03-20
    • 1970-01-01
    • 2016-02-08
    • 2016-02-10
    相关资源
    最近更新 更多