ReferenceError:在条带中未定义clientSecret

时间:2019-10-23 09:38:51

标签: node.js stripe-payments

我正在尝试将付款请求按钮集成到条纹中。为此,我遵循文档(https://stripe.com/docs/stripe-js/elements/payment-request-button#complete-payment-intents)。我在Chrome中找到了按钮。付款完成时

paymentRequest.on('paymentmethod', function(ev) {
  stripe.confirmPaymentIntent(clientSecret, {
    payment_method: ev.paymentMethod.id,
  }).then(function(confirmResult) {
    if (confirmResult.error) {
      console.log("error")
      ev.complete('fail');
    } else {
      ev.complete('success');
      stripe.handleCardPayment(clientSecret).then(function(result) {
        if (result.error) {
            console.log(result.error)
        } else {
            console.log("Success")
        }
      });
    }
  });
});

我收到一个错误,未定义clientSecret。我不知道该从哪里获得客户机密

付款方式 API未提供clientSecret

当我调用 source API时,我获得了clientSecret

但是不能同时运行 source paymentMethod API

请指导我完成集成的正确方法。 预先感谢

2 个答案:

答案 0 :(得分:1)

来自Stripe Docs

  

clientSecret 此PaymentIntent的客户密码。用于使用可发布密钥的客户端检索。   客户机密可用于从您的前端完成付款。不应将其存储,记录,嵌入URL或暴露给客户以外的任何人。确保在包含客户端密钥的任何页面上启用了TLS。

因此clientSecret定义为 paymentintent 对象的属性。

付款意向对象样本):

{
 "id": "pi_1FpUmEKZaRsxp2y4c9OPoTjM",
 "object": "payment_intent",
 "amount": 3000,
 "amount_capturable": 0,
   amount_received": 0,
 "application": null,
 "application_fee_amount": null,
 "canceled_at": null,
 "cancellation_reason": null,
 "capture_method": "automatic",
 "charges": {
     "object": "list",
     "data": [],
     "has_more": false,
     "url": "/v1/charges?payment_intent=pi_1FpUmEKZaRsxp2y4c9OPoTjM"
 },
 "client_secret": "pi_1FpUmEKZaRsxp2y4c9OPoTjM_secret_tv9tsgAQbAlNRYqm8MAzmYPuE",
 "confirmation_method": "automatic",
 "created": 1576307458,
 "currency": "eur",
 "customer": null,
 "description": null,
 "invoice": null,
 "last_payment_error": null,
 "livemode": false,
 "metadata": {},
 "next_action": null,
 "on_behalf_of": null,
 "payment_method": null,
 "payment_method_options": {
      "card": {
      "installments": null,
      "request_three_d_secure": "automatic"
    }
 },
 "payment_method_types": [
      "card"
 ],
 "receipt_email": null,
 "review": null,
 "setup_future_usage": null,
 "shipping": null,
 "statement_descriptor": null,
 "statement_descriptor_suffix": null,
 "status": "requires_payment_method",
 "transfer_data": null,
 "transfer_group": null
 }

作为documentation中的第2步,您可以将您的clientSecret作为 paymentintent.client_secret 进行检索。要在您的代码中使用:

 const clientSecret  = paymentRequest.client_secret

如果其他人正在使用php,请不要忘记将其用作字符串而不是变量:

submitButton.addEventListener('click', function(ev) {
    stripe.confirmCardPayment('<?php  echo ($intent->client_secret);?>', {
        payment_method: {card: card}
    }).then(function(result) {
        if (result.error) {
            // Show error to your customer (e.g., insufficient funds)
            console.log(result.error.message);
        } else {
            // The payment has been processed!
            if (result.paymentIntent.status === 'succeeded') {
            // Show a success message to your customer
            // There's a risk of the customer closing the window before callback
            // execution. Set up a webhook or plugin to listen for the
            // payment_intent.succeeded event that handles any business critical
            // post-payment actions.
            }
        }
    });
});

答案 1 :(得分:0)

获取您的客户端密钥,并将其用作参数。 这是how to get the client secret