Giter Site home page Giter Site logo

gecsbernat / cordova-plugin-stripeui Goto Github PK

View Code? Open in Web Editor NEW
0.0 2.0 3.0 2.1 MB

Cordova plugin for Stripe prebuilt native UI

License: MIT License

JavaScript 1.62% Java 64.74% Swift 33.64%
cordova cordova-plugin cordova-android cordova-ios ionic ionic-framework stripe stripe-checkout stripe-payment stripe-sdk

cordova-plugin-stripeui's Introduction

cordova-plugin-stripeui

Cordova plugin for Stripe Prebuilt UI on Android and iOS

Demo project

--> Ionic5/Cordova <--

Sample backend

--> NodeJs/Cloud-function <--

Features

Installation

ionic cordova plugin add https://github.com/gecsbernat/cordova-plugin-stripeui.git

Requirements

  • Stripe account.
  • Secret key and Publishable key (See Sample backend).
  • Apple Merchant ID and Apple Merchant Country Code Apple Pay.
  • Google Pay setup Google Pay.

Backend

  • You should host the backend code on your server or in a firebase cloud function (See Sample backend).

Usage

  • Sample typescript service code:
import { HttpClient } from '@angular/common/http';
import { Injectable } from '@angular/core';
import { Platform } from '@ionic/angular';

declare const StripeUIPlugin: any;

export interface PaymentResult {
    customerId?: string;
    code?: string;
    message?: string;
    error?: string;
}

export interface PaymentConfig {
    publishableKey?: string;
    companyName?: string;
    customerId?: string;
    paymentIntent?: string;
    ephemeralKey?: string;
    appleMerchantId?: string;
    appleMerchantCountryCode?: string;
    mobilePayEnabled?: boolean;
}

export interface BillingConfig {
    billingEmail?: string;
    billingName?: string;
    billingPhone?: string;
    billingCity?: string;
    billingCountry?: string;
    billingLine1?: string;
    billingLine2?: string;
    billingPostalCode?: string;
    billingState?: string;
}

@Injectable({ providedIn: 'root' })
export class StripePaymentService {

    private isCordova: boolean;
    private SERVER_URL = 'YOUR_BACKEND_URL/payment';

    constructor(
        private platform: Platform,
        private http: HttpClient
    ) {
        this.platform.ready().then(async () => {
            this.isCordova = this.platform.is('cordova');
        });
    }

    makePayment(amount: number, currency: string, customerId: string = null, customerEmail: string = null, customerName: string = null, billingConfig: BillingConfig): Promise<PaymentResult> {
        return new Promise((resolve, reject) => {
            if (this.isCordova) {
                const body = {
                    amount: amount,
                    currency: currency,
                    customerId: customerId,
                    customerEmail: customerEmail,
                    customerName: customerName
                };
                const subscribe = this.http.post(this.SERVER_URL, body).subscribe((result: any) => {
                    const paymentConfig: PaymentConfig = {
                        publishableKey: result.publishableKey,
                        companyName: result.companyName,
                        paymentIntent: result.paymentIntent,
                        customerId: result.customerId,
                        ephemeralKey: result.ephemeralKey,
                        appleMerchantId: result.appleMerchantId,
                        appleMerchantCountryCode: result.appleMerchantCountryCode,
                        mobilePayEnabled: true
                    }
                    this.presentPaymentSheet(paymentConfig, billingConfig).then((result) => {
                        result.customerId = paymentConfig.customerId;
                        resolve(result);
                    }).catch((error) => {
                        reject(error);
                    });
                    subscribe.unsubscribe(); return;
                }, (error) => {
                    reject(error);
                    subscribe.unsubscribe(); return;
                });
            } else {
                reject('NOT_CORDOVA'); return;
            }
        });
    }

    private presentPaymentSheet(paymentConfig: PaymentConfig, billingConfig: BillingConfig): Promise<PaymentResult> {
        return new Promise((resolve, reject) => {
            if (this.isCordova) {
                StripeUIPlugin.presentPaymentSheet(paymentConfig, billingConfig, (success: any) => {
                    try {
                        const result = JSON.parse(success) as PaymentResult;
                        resolve(result);
                    } catch (unused) {
                        resolve(success);
                    }
                    return;
                }, (error: any) => {
                    reject(error); return;
                });
            } else {
                reject('NOT_CORDOVA'); return;
            }
        });
    }

}
  • In your payment page:
....
  async payment() {
    try {
      this.loading = true;
      // customerId, customerEmail, customerName, billingConfig can be null.
      // customerId should be your saved customer from prevoius payment.
      const paymentResult = await this.stripeService.makePayment(this.amount, this.currency, this.customerId, this.customerEmail, this.customerName, this.billingConfig);
      const code = paymentResult.code ? Number(paymentResult.code) : -1;
      this.loading = false;
      if (code === 0) {
        // PAYMENT_COMPLETED
        this.savePayment(paymentResult);
      } else if (code === 1) {
        // PAYMENT_CANCELED
      } else if (code === 2) {
        // PAYMENT_FAILED
      }
    } catch (error) {
      this.loading = false;
      console.log(error);
    }
  }

  savePayment(paymentResult: PaymentResult) {
    // TODO: save the payment and customer in your database for later use...
    // customerId?: string; code?: string; message?: string; error?: string;
    console.log({ paymentResult });
    this.dismiss(true);
  }
  ....

cordova-plugin-stripeui's People

Contributors

gecsbernat avatar

Watchers

 avatar  avatar

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.