簡體   English   中英

如何使用Stripe接受比特幣和美元付款?

[英]How to accept Bitcoin and USD payments using Stripe?

參考:

https://stripe.com/docs/sources/bitcoin


題:

我正在嘗試使用Stripe將比特幣付款集成到我的代碼中。

根據我對文檔的理解,我應該通過創建新表單來創建Source對象,並在提交Feed時將數據(在這種情況下為電子郵件和金額)發送至:

stripe.createSource({
  type: 'bitcoin',
  amount: 1000,
  currency: 'usd',
  owner: {
    email: 'jenny.rosen@example.com',
  },
}).then(function(result) {
  // handle result.error or result.source
});

然后將結果傳遞給服務器以對Source對象收費。 唯一的問題:我看不到如何將結果傳遞給服務器。 我應該創建一個新的處理程序嗎?

var handler = StripeCheckout.configure({
      key: 'key',
      locale: 'auto',
      name: 'website',
      description: 'Secure Payment',
      token: function(token) {
        $('#stripeToken').val(token.id);
        $("#stripeEmail").val(token.email);
        $('form').submit();
      }                                    
    });

我搞不清楚了。

這是我目前擁有的代碼,非常適合美元付款。


我的密碼:

客戶端(payment.ejs)

<% include partials/header %>
<div class="background">
    <div class="message">
        <div class="paymentBlock">
            <h1 class="title">TITLE</h1>

            <form class="paymentForm" action="/payment/charge" method="POST">  
                <input id="inputAmount" class="amountInput" name="amount" type="number/>
                <input type="hidden" id="stripeToken" name="stripeToken" />
                <input type="hidden" id="stripeEmail" name="stripeEmail"/>
                <button type="submit" class="btn btn-success" id="paymentButton" >Submit Payment</button>
            </form>
        </div>
    </div>
</div>

<script src="https://checkout.stripe.com/checkout.js"></script>

<script>

    var handler = StripeCheckout.configure({
      key: 'key',
      locale: 'auto',
      name: 'website',
      description: 'Secure Payment',
      token: function(token) {
        $('#stripeToken').val(token.id);
        $("#stripeEmail").val(token.email);
        $('form').submit();
      }                                    
    });

    $('#paymentButton').on('click', function(e) {
      e.preventDefault();

      $('#error_explanation').html('');

      var amount = $('#inputAmount').val();
      amount = amount.replace(/\$/g, '').replace(/\,/g, '');

      amount = parseFloat(amount);

      if (isNaN(amount)) {
        $('#error_explanation').html('<p>Please enter a valid amount in USD ($).</p>');
      }
      else if (amount < 1.00) {
        $('#error_explanation').html('<p>Payment amount must be at least $1.</p>');
      }
      else {
        amount = amount * 100;
        handler.open({
          amount: Math.round(amount)
        })
      }
    });
    // Close Checkout on page navigation
    $(window).on('popstate', function() {
      handler.close();
    });
</script>

<% include partials/indexScripts %>

服務器端(payment.js)

router.post("/", (req, res) => {

    var amount = req.body.amount;
    var object;
    var ARef = admin.database().ref("ref");
    var ARefList;
    amount = amount * 100; 

    var object = {
        amount: amount,
        email: email,
        inversedTimeStamp: now
    }

    stripe.customers.create({
        email: req.body.stripeEmail,
        source: req.body.stripeToken
    })
    .then(customer =>
        stripe.charges.create({
            amount: amount,
            description: "desc",
            currency: "usd",
            customer: customer.id
        })
    )
    .then(charge => 
        ARef.transaction(function(dbAmount){  
            if (!dbAmount) { 
              dbAmount = 0; 
            } 
            dbAmount = dbAmount + amount/100; 
            return dbAmount; 
        })
    )
    .then( () =>
        ARef.push(object)
    )
    .then ( () =>
        ARefList.push(object)
    )
    .then( () =>
        res.render("received",{amount:amount/100})
    );
});

感謝freenode IRC #stripe的“ pksk321”為我提供的幫助!

顯然,所需要做的就是在處理程序中添加bitcoin: true ,就像這樣:

客戶端

var handler = StripeCheckout.configure({
      key: 'key',
      locale: 'auto',
      name: 'website',
      description: 'Secure Payment',
      bitcoin: true,
      token: function(token) {
        $('#stripeToken').val(token.id);
        $("#stripeEmail").val(token.email);
        $('form').submit();
      }                                    
    });

暫無
暫無

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

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