Skip to content

Complete Browser and Server client integration of Stripe in Scala and Scala.js

License

Notifications You must be signed in to change notification settings

harana-oss/scala-stripe

 
 

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

scala-stripe

Codacy Badge Build Status Stories in Ready Gitter Maven Central Latest version

Provides both client (Stripe.js using Scala.js) and server (NIO Scala) functionality for dealing with Stripe.

For more information on the Stripe API see https://stripe.com/docs/api

For more information on Stripe.js see https://stripe.com/docs/stripe.js

SBT Configuration

scala-stripe is published to Sonatype OSS and Maven Central and supports JVM and Scala.js with 2.11 and 2.12:

libraryDependencies += "com.outr" %% "scala-stripe" % "1.1.11"   // Scala
libraryDependencies += "com.outr" %%% "scala-stripe" % "1.1.11"  // Scala.js / Cross-Build

Dependencies

It's important to know how much your biting off when you are adding another dependency to your project. As such, we've endeavored to keep the dependencies to a minimum:

Getting Started

Though the JVM supports creating credit card tokens, the ideal path is to avoid the server ever having any such knowledge. To that end we will use Scala.js to first create the card token, and then the server (JVM) will use that token to make a purchase.

In the Browser

Setting up Stripe.js

We must first set our publishable key. Make sure you are using the public key (starts with "pk_"):

Stripe.setPublishableKey(stripePublicKey)

Validating the credit card

val validationResult: Boolean = Stripe.card.validateCardNumber(creditCardNumber)

Creating a Stripe Token for the credit card

The following example is a basic use-case, and assumes you'll provide implementations of sending the token to the server as well as showing token failure messages as that information is specific to your implementation.

Stripe.card.createToken(new StripeCardInfo {
  number = creditCardNumber
  exp_month = cardExpirationMonth
  exp_year = cardExpirationYear
}, (status: Int, info: CardTokenInfo) => {
  if (status == 200) {
    sendCardTokenInfoToServer(info.id, ...other useful information the server might need...)
  } else {
    showCardTokenFailure(info.error)
  }
})

On the Server

Creating a Stripe instance

As discussed in the Browser section, we have two keys: public ("pk_") and secret ("sk_"). For the server to work with Stripe we should use our private key to create an instance of Stripe:

val stripe = new Stripe(stripePrivateKey)

Charging the Credit Card

Now that we have received the card token on the server, we can use that to make a purchase with Stripe:

stripe.charges.create(Money(5.0), "USD", source = creditCardTokenId, customer = customerId).map {
  case Left(failure) => // Handle error from Stripe server
  case Right(charge) => // Success! Handle the Charge instance returned
}

Next Steps

Hopefully that is enough to get you started with stripe-scala. There is much more available and we attempt to be a very close wrapper around the Stripe API. If you have any problems refer to the Stripe API documentation as we try to remain very close to them.

Features for 2.0.0 (In-Progress)

  • Stripe API Relay
    • Orders
    • Order Items
    • Returns
    • Products
    • SKUs
  • Stripe API Radar
    • Reviews
  • Stripe API Core Resources
    • File Uploads
  • Stripe API Payments
    • Alipay Accounts
    • Sources
  • Stripe.js
    • Apple Pay

Features for 1.1.0 (Released 2017.08.08)

  • Migration to youi-client away from Gigahorse
  • Better error handling support

Features for 1.0.0 (Released 2016.01.05)

  • Stripe.js functionality in Scala.js (Excludes Apple Pay)
    • Card
    • Bank Account
    • Personally Identifiable Information (PII)
  • Stripe API Core Resources
    • Balance
    • Charges
    • Customers
    • Disputes
    • Events
    • Refunds
    • Tokens
    • Transfers
    • Transfer Reversals
  • Stripe API Connect
    • Account
    • Application Fees
    • Application Fee Refunds
    • Country Specs
    • External Accounts
  • Stripe API Payments
    • Bank Accounts
    • Cards
  • Stripe API Subscriptions
    • Coupons
    • Discounts
    • Invoices
    • Invoice Items
    • Plans
    • Subscriptions

About

Complete Browser and Server client integration of Stripe in Scala and Scala.js

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages

  • Scala 99.6%
  • Other 0.4%