R4ttlesnake
R4ttlesnake

Reputation: 1771

(Rails) PayPal payment with Braintree v.zero SDK

I’m working on a Rails app that will have a modifiable shopping cart, a selectable shipping fee, finished with a simple PayPal payment. According to the PayPal developer docs, the Express Checkout seems to be appropriate and a simple method for Rails might be Braintree's v.zero SDK.

According to the Braintree Docs for the PayPal client-side implementation (JS v2 SDK), I will have a container for the PayPal button and a javascript with all options and a provided Client-Token:

<div id="paypal-container"></div>

<script type="text/javascript">
  braintree.setup("CLIENT-TOKEN-FROM-SERVER", "custom", {
    paypal: {
      container: "paypal-container",
    },
    onPaymentMethodReceived: function (obj) {
      doSomethingWithTheNonce(obj.nonce);
    }
  });
</script>

Submitting this button should get me a payment_method_nonce from braintree, invoke a server-side method to fill in the necessary payment informations (payment_method_nonce, amount, etc.) and finally complete the transaction. Such as in the server-side implementation docs for ruby:

def checkout
    result = Braintree::Transaction.sale(
      :amount => "10.00",
      :payment_method_nonce => params[:payment_method_nonce],
      :order_id => "Mapped to PayPal Invoice Number",
      :options => {
        :paypal => {
          :custom_field => "PayPal custom field",
          :description => "Description for PayPal email receipt",
        },
      }
    )
    if result.success?
      "Success ID: #{result.transaction.id}"
    else
      result.message
    end
  end

I think, I understand this procedure basically, but I don't get how the javascript would route to my server-side method. Having a credit card form, I could define a target to post to such as /checkout and have the /checkout route to my checkout method. But only using PayPal, how do I route from my PayPal button to my checkout method?

Thanks

Upvotes: 1

Views: 253

Answers (1)

cooljake
cooljake

Reputation: 696

Full disclosure: I work at Braintree. If you have any further questions, feel free to contact support.

Braintree's javascript SDK does generate a new payment_method_nonce, but it does not automatically send it to your server. That part of the process is left up to you. You can access and work with the new nonce by implementing the onPaymentMethodReceived callback`:

<script type="text/javascript">
  braintree.setup("CLIENT-TOKEN-FROM-SERVER", "custom", {
    paypal: {
      container: "paypal-container",
    },
    onPaymentMethodReceived: function (obj) {
      // you may submit the nonce to your server here
      doSomethingWithTheNonce(obj.nonce);
    }
  });
</script>

That function, doSomethingWithTheNonce is a placeholder to indicate that this is where you would handle the nonce. You may, as you were thinking, set up a post to an endpoint like /checkout. You could also set the nonce in a form that the user will submit later, then pick up the nonce from the form when it's submitted.

Upvotes: 3

Related Questions