Giter VIP home page Giter VIP logo

scala-stripe's Introduction

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

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.