bolt githublinkedinPage 1Created with Sketch. spots swoosh target triangle-icon twilio twitterweave

Stripe Payments

RichardJune 13, 2017

Stripe Payments

We have recently set up a couple of projects which implemented Stripe for taking customer payments. Given our discoveries along the way I decided to write an article that highlights some of the advantages of using Stripe and also outlines how to take payments.

Advantages of Stripe

So what are some of the advantages of using Stripe in an application?

Simple to setup

Getting started with Stripe is as easy as registering on their website, activating the account by supplying some basic information and noting down the publishable and secret keys. We're then ready to start putting code into our app - Stripe has libraries for Ruby, Python, PHP, Java, Go and Node.js and also offers SDKs for iOS and Android.

PCI Compliance

Another winner for Stripe is the way that card details are handled. At no point do we need to pass card details to the back-end or store them, removing the headache of having to deal with PCI compliance.

Facilitates Marketplace apps

With Stripe we have the ability to add marketplace functionality to our apps. Using the concept of Connected Accounts we can setup a means for suppliers to receive payment from customers without them having to actively setup an account on Stripe themselves.

The Process

So how do we receive payments from customers and send funds to our suppliers? Stripe uses various object types to represent payments, customers and withdrawals, and these are explained below with an outline of how to use them in an app.

Charges

These are the basis on which your customers will make a payment to your app. There are two parts to the process of creating a charge, (1) creating a tokenised version of the payment details on the front-end and (2) using that token on the back-end to create the actual charge.

On the front-end, by using Stripe.js we can plug in this piece of code to collect a user's card details and retrieve a tokenised version from the Stripe API:

var stripe = Stripe(publishable_key);
var elements = stripe.elements();
var card = elements.create('card');
stripe.createToken(card).then(function(result) {
  // handle result.error or result.token
});

This sets up a new Stripe instance using our publishable key, pulls in the required pieces from Elements to allow us to collect card information and then sends the card details to Stripe. We then handle the response (which may be an error due to invalid CVC, card number, etc...) or the token, which we can then pass to the back-end and use the following code to create a charge:

Stripe::Charge.create(
  amount: 2000,
  captured: true,
  currency: 'eur',
  description: 'Charge for [email protected]',
  source: credit_card_token,
)

Here we are sending:

  • amount - the amount to be charged
  • captured - which informs Stripe whether the card is to be charged straight away or just have the amount authorised. This allows us to charge the card at a later time - handy in some situations e.g. waiting for a rental owner to confirm a booking, with the knowledge that the customer will be able to pay at that point
  • currency - the currency to be charged in
  • description - a description of the charge to make it easier to see what payments are for in the Stripe console
  • source - in this case the token that we retrieved in the front-end

And that's it, with this we have been able to charge a user's card.

Customers

What if we don't want to just allow one-off purchases? Maybe we want to allow a customer to easily reuse previously entered card details or enable subscriptions. By using the concept of customers we are able to connect payments to a specific user.

Stripe::Customer.create(
  description: 'Customer for [email protected]',
  email: '[email protected]',
  source: credit_card_token
)

Here we are sending the following:

  • description - A description of the customer to more easily identify them in the console
  • email - Sending additional info for the user makes searching in the console easier
  • source - Sending a source means we can set up the customer with an initial payment source.

Payment Source

Payment sources are card details that have been previously saved on Stripe by a user. Using the same process as the previous charge example we can pass through a token and, instead of using the source immediately, store it against the customer for later payment.

customer = Stripe::Customer.retrieve(customer_id)
customer.sources.create(source: credit_card_token)

We are now able to create a charge against a created customer using their existing payment source:

Stripe::Charge.create(
  amount: 2000,
  captured: true,
  currency: 'eur',
  customer: 'cus_id',
  description: 'Charge for [email protected]',
  source: payment_source_id,
)

This is very similar to the previous charge example, but in this case we have the following additional fields:

  • customer - the Stripe id of the customer to be charged
  • source - the Stripe id of an existing payment method for the specified customer.

By using this, the customer will be charged on the specified payment source. If the payment source doesn't exist or does not belong to the specified customer then an error will be returned.

Connected Accounts

These are means by which we can add marketplace functionality to an app. We can set up an account for each supplier selling on our app and use them as the means for transferring money from a charge.

It is important to note that each connected account goes through a verification process which ensures that the person is, in fact, able to withdraw the funds. This is to prevent fraudulent activity via your app. Until verification has been completed the supplier will be restricted to a maximum amount of fund withdrawal.

To create a connected account for a supplier you can use the following

Stripe::Account.create(
  country: 'GB',
  email: '[email protected]',
  type: 'custom',
  payout_schedule: 'manual',
  legal_entity: {
    various_attributes: []
  }
)
  • country - the country in which the supplier is located, which in turn dictates the verification rules that the account will be governed by - move info here
  • type - The type of account we wish to set up. Custom means we have control over the account for the supplier, as we require in our marketplace.
  • payout_schedule - a handy feature that allows us to set different manual or automated payout rules per supplier. When manual the supplier is able to withdraw the funds when they please

Charge creation with connected account

Stripe::Charge.create(
  amount: 2000,
  captured: true,
  currency: 'eur',
  customer: 'cus_id',
  description: 'Charge for [email protected]',
  destination: {
    account: connected_account_id,
    amount: 1900
  }
  source: payment_source_id
)

In this variation of creating a charge we have the following additional attributes:

  • destination[:account] - this specifies which connected account is owned by the supplier of the purchased item
  • destination[:amount] - the amount of the charge that is to be routed to the supplier. The difference between the charge amount and this amount will be routed to the app owner's Stripe account and used for Stripe fees as well.

Payouts

The final object type I will mention in this article is a payout. These are the means by which the owner of a connected account can withdraw the charges that have been paid into their account. These only need to be used if the account isn't set up with an automated payment schedule.

Payout creation

Stripe::Payout.create(
  {
    amount: 1000,
    currency: 'eur',
    destination: external_account_id
  },
  {
    stripe_account: connected_account_id
  }
)

The attributes here are:

  • amount - the amount to be withdrawn to the connected account holder's external account
  • currency - the currency that the withdrawal should be in
  • destination - this specifies which external account to withdraw to, if the money is not to be withdrawn to the account's default external account
  • stripe_account - the id of the stripe connected account that the payout is to be against

And There's More

This article has given a quick outline of how Stripe can help you rapidly start taking payments from your customers. Of course there is much more to Stripe than this including performing refunds, handling callbacks from Stripe, subscriptions, coupons and discounts. Watch this space for future updates.

THINK

out loud

We think out loud on our blog. We share our views on coding, remote work and post regular insights into the freshest tech industry news.

Want more? Join our mailing list.

    DVELP

    WORK WITH US.

    Client Engagement

    +44 (0) 20 31 37 63 39

    [email protected]

    So, how can we help you?

    Live chat