簡體   English   中英

React Stripe付款創建客戶和訂單/送貨地址等

[英]React Stripe Payment Create Customer and Order / Shipping Address etc

我使用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" });
          });
      },
    });
  } 

我想查看客戶數據,送貨地址等。

感謝您的幫助。

您正在收集的令牌回調的args- 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'));

  }
});

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM