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

scala-stripe's People

Contributors

codacy-badger avatar darkfrog26 avatar harana-bot avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar

scala-stripe's Issues

currencies with different fractions than 2

Hello, I was testing this lib adn I noticed that is converting any currency on a currency with 2 decimals, in this case if you try to pay in i.e. JPY the amount is multiplied by 100.
I am quite noob on github and I am not mainly a developer, I did some change to the code but I don t know how to send it to help in case it is useful to solve this issue :-) feel free to contact me to per mit me to learn how I can help (I tried to fork and push but I don t know if affects in some way your job or the licence so I removed my public fork atm)

event/data/previous_attributes is optional in API but mandatory in the case class

For example stripe.events.byId(id) fails with io.circe.DecodingFailure$$anon$2: Attempt to decode value on failed cursor: DownField(previousAttributes),DownField(data)

And when parsing an incoming webhook event with Pickler the same failure happens. I can workaround the webhook (by adding and empty object, for example), but not the API call.

image

Error on decoding json while creating account

I've ran stripeClient.accounts.create(custom = false) and got this error Exception in thread "main" com.outr.stripe.PicklerException: Unable to decode Caused by: DecodingFailure(String, List(DownField(businessName))) , as in the payload the bussiness_name was null. can this be a problem ?

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.