-1

REFERENCE:

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


QUESTION:

I am trying to integrate Bitcoin payments to my code using 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 :

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. 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)

<% 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)

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})
    );
});
TheProgrammer
  • 1,409
  • 4
  • 24
  • 53
  • 1
    your question section doesn't contain a question! define "struggling". This is so vague as to be a pointless sentence. We cannot infer anything about your situation from that. What's going wrong with your code above? See https://stackoverflow.com/help/how-to-ask and clarify your question. – ADyson Dec 19 '17 at 16:27
  • 1
    We don't care about your HTML, that is your part in building the front-end; if you are struggling with the API or whatever, that is where you have to focus in your question, don't include irrelevant information –  Dec 19 '17 at 16:33
  • What is what you are not clear about? –  Dec 19 '17 at 16:34
  • I reccommend you bother reading the docs. I know it is very annoying and tiring, but it is the actual way of developing something at all. If you then find a specific problem or do not know how to do something, you can post a question with it. But not just say 'I can't do this, please do it for me' –  Dec 19 '17 at 16:36
  • @J.C.Rocamonde Apologies if my original question gave this impression. Question edited. – TheProgrammer Dec 19 '17 at 16:38
  • @ADyson Question edited. – TheProgrammer Dec 19 '17 at 16:38
  • sorry, I saw your // this happens regardless of the success of the charge comment and I thought you wanted it to go depending on its success. –  Dec 19 '17 at 16:54
  • @J.C.Rocamonde It's my fault, I forgot to remove that comment. – TheProgrammer Dec 19 '17 at 16:57
  • @TheProgrammer its fine –  Dec 19 '17 at 16:58
  • @J.C.Rocamonde :) – TheProgrammer Dec 19 '17 at 16:58

1 Answers1

0

Thank you to "pksk321" from the freenode IRC #stripe for helping me with this !

Apparently, all that was needed was to add bitcoin: true to the handler, like so :

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();
      }                                    
    });
TheProgrammer
  • 1,409
  • 4
  • 24
  • 53