This article will describe how to complete an one time payment of digital download with firebase and stripe, using jekyll template.
Unzip the project to your local disk.
Notice that:
If you are not using Jekyll to build your website, you can directly use the files inside _site folder.
Otherwise, you can config the jekyll environment as follow.
$ ruby -v
ruby 2.2.4p230 (2015-12-16 revision 53155) [i386-mingw32]
$ gem install jekyll
$ cd projectfolder
$ jekyll serve
In order to send email to your customer, you can use following firebase Extension:
This exenstion will auto send email when you write the collection. In the demo project, two collections are used.
Use this extenstion, you also need a SMTP connenct URL. I am using Mailgun to do this.
Modify webconfig.js
// Your web app's Firebase configuration
const firebaseConfig = {
apiKey: "***********************************",
authDomain: "easycomplex-website.firebaseapp.com",
databaseURL: "https://easycomplex-website-default-rtdb.firebaseio.com",
projectId: "easycomplex-website",
storageBucket: "easycomplex-website.appspot.com",
messagingSenderId: "********************",
appId: ""***********************************","
};
// Replace with your cloud functions location
const functionLocation = 'us-east1';
// Initialize Firebase
const firebaseApp = firebase.initializeApp(firebaseConfig);
const db = firebaseApp.firestore();
// https://dashboard.stripe.com/apikeys
const STRIPE_PUBLISHABLE_KEY = '************************';
const stripe = Stripe(STRIPE_PUBLISHABLE_KEY);
Add a product in the stripe dashboard
Enable the stripe client-only intergration setting.
Goto Firebase Console, add collection “onetime_customers” and “products_index” in Firestore. Add the stripe product (using above stripe proudct API ID ) into “products_index” collection as follow.
Using following security rules for your firestore.
Customer can only read their own record in the the “onetime_customers” collection.
Only product owner can read product detail in the “products_index” collection.
rules_version = '2';
service cloud.firestore {
match /databases/{database}/documents {
match /onetime_customers/{email} {
allow read: if request.auth.token.email == email;
match /ownproducts/{id} {
allow read: if request.auth.token.email == email;
}
}
match /products_index/{id} {
allow read: if request.auth != null && exists(/databases/$(database)/documents/onetime_customers/$(request.auth.token.email)/ownproducts/$(id));
}
match /mail/{uid} {
allow read, write: if request.auth.uid == uid;
}
}
}
While stripe checkout completed, customer data needs to be wrote back to firestore which will be completed by Cloud Function.
Run CLI tool and goto your project folder.
Using following commands:
> cd projectfolder
> firebase init functions
> cd functions
> npm install
> firebase functions:config:set stripe.secret="stripekey" stripe.hooksecret="webhookkey"
> firebase deploy --only functions
Fetch above “stripekey” in the stripe dashboard.
Fetch above “webhookkey” in the stripe dashboard as well.
Notice you should update the above webhook endpoint url after cloud function deploy success in firebase. Using below link as your webhook endpoint url.
> firebase deploy --only hosting
Create a html file as “checkout_onetimepayment.html” in the project folder. Replace following price id with your own product price id in in the stripe dashboard.
---
layout: checkout
---
<script type="text/javascript">
firebase.auth().onAuthStateChanged((firebaseUser) => {
initCheckout(
firebaseUser, //user
'price_1IZqARDrT2ErSlwXTMS2K88z', //product price id
'https://easycomplex-website.web.app/updateresource.html?id=price_1IZqARDrT2ErSlwXTMS2K88z', //pay successful jump to, fetch firebase data in this page
'https://easycomplex-website.web.app/' //cancel payment
);
});
</script>
Let’s begin to reveive your payment on your own!
[1] Host your own blog with Jekyll on Firebase and Travis CI