React Stripe付款创建客户和订单/送货地址等

时间:2018-08-25 19:29:54

标签: javascript node.js reactjs aws-lambda stripe-payments

我使用gatsby react和aws lambda创建了条形支付页面。但是此代码不会创建客户数据(如送货地址,电子邮件等)

Lamdba代码

const stripe = require("stripe")(process.env.STRIPE_SECRET_KEY);

module.exports.handler = (event, context, callback) => {
  console.log("creating charge...");

  // Pull out the amount and id for the charge from the POST
  console.log(event);
  const requestData = JSON.parse(event.body);
  console.log(requestData);
  const amount = requestData.amount;
  const token = requestData.token.id;

  // Headers to prevent CORS issues
  const headers = {
    "Access-Control-Allow-Origin": "*",
    "Access-Control-Allow-Headers": "Content-Type"
  };

  return stripe.charges
    .create({
      // Create Stripe charge with token
      amount,
      source: token,
      currency: "usd",
      description: "Tshirt"
    })
    .then(charge => {
      // Success response
      console.log(charge);
      const response = {
        headers,
        statusCode: 200,
        body: JSON.stringify({
          message: `Charge processed!`,
          charge
        })
      };
      callback(null, response);
    })
    .catch(err => {
      // Error response
      console.log(err);
      const response = {
        headers,
        statusCode: 500,
        body: JSON.stringify({
          error: err.message
        })
      };
      callback(null, response);
    });
};

盖茨比付款代码

代码有效,付款有效。但是运送详细信息不起作用。

openStripeCheckout(event) {
    event.preventDefault();
    this.setState({ disabled: true, buttonText: "WAITING..." });
    this.stripeHandler.open({
      name: "Demo Product",
      amount: amount,
      shippingAddress: true,
      billingAddress: true,
      description: "",
       token: (token, args) => {
        fetch(`AWS_LAMBDA_URL`, {
          method: "POST",
          body: JSON.stringify({
            token,
            args,
            amount,
          }),
          headers: new Headers({
            "Content-Type": "application/json",
          }),
        })
          .then(res => {
            console.log("Transaction processed successfully");
            this.resetButton();
            this.setState({ paymentMessage: "Payment Successful!" });
            return res.json();
          })
          .catch(error => {
            console.error("Error:", error);
            this.setState({ paymentMessage: "Payment Failed" });
          });
      },
    });
  } 

我想查看客户数据,送货地址等。

感谢您的帮助。

1 个答案:

答案 0 :(得分:1)

您要收集的令牌回调的args参数中提供帐单和送货地址。

https://jsfiddle.net/qh7g9f8w/

var handler = StripeCheckout.configure({
  key: 'pk_test_xxx',
  locale: 'auto',
  token: function(token, args) {

    // Print the token response
    $('#tokenResponse').html(JSON.stringify(token, null, '\t'));

    // There will only be args returned if you include shipping address in your config
    $('#argsResponse').html(JSON.stringify(args, null, '\t'));

  }
});