简体   繁体   English

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

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

REFERENCE: 参考:

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


QUESTION: 题:

I am trying to integrate Bitcoin payments to my code using Stripe. 我正在尝试使用Stripe将比特币付款集成到我的代码中。

From my understanding of the docs, I should create a Source object by creating a new form and on submit feed the data (in this case email and amount) to : 根据我对文档的理解,我应该通过创建新表单来创建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
});

Then pass the result to the server to charge the Source object. 然后将结果传递给服务器以对Source对象收费。 Only issue: I don't see how to pass the result to the server. 唯一的问题:我看不到如何将结果传递给服务器。 Should I create a new handler ? 我应该创建一个新的处理程序吗?

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();
      }                                    
    });

I am lost. 我搞不清楚了。

Here is the code I currently have that works perfectly for USD payments. 这是我目前拥有的代码,非常适合美元付款。


MY CODE: 我的密码:

clientside (payment.ejs) 客户端(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 %>

serverside (payment.js) 服务器端(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})
    );
});

Thank you to "pksk321" from the freenode IRC #stripe for helping me with this ! 感谢freenode IRC #stripe的“ pksk321”为我提供的帮助!

Apparently, all that was needed was to add bitcoin: true to the handler, like so : 显然,所需要做的就是在处理程序中添加bitcoin: true ,就像这样:

clientside 客户端

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