Stripe API
Stripe is a payment API. You can use it on websites, or mobiles devices, and you can use your own interface, or theirs.
It's worth noting that their documentation can be adapted according to the technologies you are using (ex: Node.js, HTML...), so you may be able to download code samples in your language π₯³.
Some useful pages
- Prebuilt Checkout page (client web + server)
- Accept a payment (client web/mobile + server)
- Custom payment flow (client web/mobile + server)
β‘οΈYou need an account at some point, but you can use the public (secret) test key provided in the samples to test the API
Basics
To use the Stripe API, you will have to use two tokens.
-
sk_SECRET_KEY
: a secret key used server-side -
pk_PUBLIC_KEY
: a public key used client-side
You can get them from your Stripe Dashboard.
Testing
-
4242 4242 4242 4242
(any CVC/...): OK -
4000 0025 0000 3155
(any CVC/...): Authentication required -
4000 0000 0000 9995
(any CVC/...): Refused
Node.js (server)
Generate a payment intent
await stripe.paymentIntents.create({
amount: 1400, // calculate the price
currency: "usd",
payment_method_types: ['card'],
// optional; you can store custom data
// metadata: {
// key: value
// }
}).then(r => res.json({ id: r.id, clientSecret: r.client_secret }) )
Retrieve a payment intent
const paymentIntent = await stripe.paymentIntents.retrieve('pi_id')
// see status
if (paymentIntent.status === 'succeeded') { /* ... */ }
Delete a payment intent
const paymentIntent = await stripe.paymentIntents.cancel('pi_id')
Android (client)
Reference: Accept a payment.
Edit your build.gradle
+ implementation 'com.stripe:stripe-android:20.16.1'
Edit your AndroidManifest.xml
+ <uses-permission android:name="android.permission.INTERNET" />
<application
+ android:name=".MainApplication"
Create/Edit MainApplication.kt
import android.app.Application
import com.stripe.android.PaymentConfiguration
class MainApplication : Application() {
override fun onCreate() {
super.onCreate()
PaymentConfiguration.init(
applicationContext,
"pk_PUBLIC_KEY_HERE"
)
}
}
Create a payment intent (server-side)
You need to create a PaymentIntent server-side. In the Stripe response, you will get a client_secret that must be sent to the client.
Finally, add this code to the activity with the payment button. You will typically call init at the start, show when the pay button is pressed.
β οΈ You need to fetch the payment intent from your API. See Internet.
private lateinit var paymentSheet: PaymentSheet
private fun init() {
paymentSheet = PaymentSheet(this, ::onPaymentSheetResult)
}
private fun show() {
paymentSheet.presentWithPaymentIntent(
"PAYMENT_INTENT.CLIENT_SECRET",
PaymentSheet.Configuration(
merchantDisplayName = "MERCHANT NAME HERE",
allowsDelayedPaymentMethods = false
)
)
}
private fun onPaymentSheetResult(paymentSheetResult: PaymentSheetResult) {
when(paymentSheetResult) {
is PaymentSheetResult.Canceled -> {
// do something
}
is PaymentSheetResult.Failed -> {
// do something | see paymentSheetResult.error
}
is PaymentSheetResult.Completed -> {
// do something
}
}
}
π» To-do π»
Stuff that I found, but never read/used yet.