Apple pay

The SDK given is not designed for redux, we must wrap it in Promise as external service prior to saga-call

./app/service/apple_pay.js

import { validateMerchant } from './axios_payment_gateway';

export const checkApplyPayCompatibility = ({merchantIdentifier}) => {
    return new Promise((resolve, reject) => {
        if (window.ApplePaySession) {
            var promise = ApplePaySession.canMakePaymentsWithActiveCard(merchantIdentifier);
            promise.then( resolve ); 
        } else {
            resolve(false)
        }
    })
}

export const performPayment = ({paymentRequest}) => {
    return new Promise((resolve, reject) => {
        var session = new ApplePaySession(1, paymentRequest);
        session.onvalidatemerchant = (event) => {
            try{
                validateMerchant(event.validationURL).then((merchantSession) => {
                    session.completeMerchantValidation(merchantSession);
                })
            }catch (e) {
                reject();
            }
        }
        session.onpaymentauthorized = resolve;
        session.oncancel = reject;
        session.begin();
    })
}

./app/saga/CartCheckout.js

import { getMerchantIdentifier, getApplePayPaymentRequest } from '../selectors';
import { checkApplyPayCompatibility, performPayment, callCompleteTransaction } from '../services';

function* showInvoicePessimistic() {
    while (true) {
        yield take (INVOICE_SHOWN)
        const merchantIdentifier = yield select (getMerchantIdentifier);
        const { isApplePayAvailable, invoice } = yield all({
            isApplePayAvailable: call (checkApplyPayCompatibility, {merchantIdentifier}),
            invoice: call (prepareInvoice)
        })
        yield put (INVOICE_FETCHED, { isApplePayAvailable, invoice })
        yield call (browserHistory.push, '/cart/invoice');
    }
}

function* showInvoiceOptimistic() {
    while (true) {
        yield take (INVOICE_SHOWN)
        yield call (browserHistory.push, '/cart/invoice');
        const merchantIdentifier = yield select (getMerchantIdentifier);
        const { isApplePayAvailable, invoice } = yield all({
            isApplePayAvailable: call (checkApplyPayCompatibility, {merchantIdentifier}),
            invoice: call (prepareInvoice)
        })
        yield put (INVOICE_FETCHED, { isApplePayAvailable, invoice })
    }
}

function* makePaymentByApplePay() {
    while (true) {
        yield take (APPLE_PAY_START);
        const paymentRequest = yield select ( getApplePayPaymentRequest );
        const { payment } = yield call ( performPayment, paymentRequest );
        if (payment) {
            const result = yield call(callCompleteTransaction, payment.token);
            if (result.success) {
                yield call(browserHistory.push, '/purchase/success');
            }
        }
    }
}

results matching ""

    No results matching ""