react-native-moyasar-sdk
Version:
Official React Native Moyasar SDK - Integrate Credit Cards, Apple Pay, Samsung Pay, and STC Pay with simple interfaces for a seamless payment experience in your React Native app
108 lines (106 loc) • 4.79 kB
JavaScript
;
// @ts-ignore
import { ApplePayButton, PaymentRequest } from "../react_native_apple_pay/index.js";
import { debugLog, errorLog } from "../helpers/debug_log.js";
import { Platform, useColorScheme, View } from 'react-native';
import { toMajor } from "../helpers/currency_util.js";
import { assert } from "../helpers/assert.js";
import { ApplePayRequestSource } from "../models/api/sources/apple_pay/apple_pay_request_source.js";
import { PaymentRequest as MoyasarPaymentRequest } from "../models/api/api_requests/payment_request.js";
import { createPayment } from "../services/payment_service.js";
import { isMoyasarError, GeneralError, NetworkError } from "../models/errors/moyasar_errors.js";
import { jsx as _jsx } from "react/jsx-runtime";
// TODO: Move to service module
export async function onApplePayResponse(token, paymentConfig, onPaymentResult) {
const source = new ApplePayRequestSource({
applePayToken: token,
manualPayment: paymentConfig.applePay?.manual,
saveCard: paymentConfig.applePay?.saveCard
});
const paymentRequest = new MoyasarPaymentRequest({
givenId: paymentConfig.givenId,
amount: paymentConfig.amount,
currency: paymentConfig.currency,
description: paymentConfig.description,
metadata: paymentConfig.metadata,
source: source,
applyCoupon: paymentConfig.applyCoupon
});
debugLog('Moyasar SDK: Paying with Apple Pay...');
try {
const paymentResponse = await createPayment(paymentRequest, paymentConfig.publishableApiKey);
onPaymentResult(paymentResponse);
} catch (error) {
errorLog(`Moyasar SDK: Failed to pay with Apple Pay, ${error}`);
if (isMoyasarError(error)) {
onPaymentResult(error);
} else {
onPaymentResult(new NetworkError('Moyasar SDK: An error occured error while processing an Apple Pay payment'));
}
}
}
export function ApplePay({
paymentConfig,
onPaymentResult,
style
}) {
assert(!!paymentConfig.applePay, 'Apple Pay config is required to use Apple Pay, you have to configure the `applePay` property in the `PaymentConfig` object');
const isLightTheme = useColorScheme() === 'light';
if (Platform.OS !== 'ios' || !paymentConfig.applePay) {
debugLog('Moyasar SDK: Apple Pay is not supported on this device or the `applePay` property is not set, showing empty view');
return /*#__PURE__*/_jsx(View, {});
}
assert(paymentConfig.applePay !== undefined, 'Apple Pay config is required');
return /*#__PURE__*/_jsx(View, {
style: {
alignItems: 'center'
},
children: /*#__PURE__*/_jsx(ApplePayButton, {
type: style?.buttonType ?? 'inStore',
height: style?.height ?? 50,
width: style?.width ?? '90%',
cornerRadius: style?.cornerRadius ?? 11,
style: style?.buttonStyle ?? (isLightTheme ? 'black' : 'white'),
onPress: () => {
debugLog('Moyasar SDK: Apple Pay button pressed');
const methodData = {
supportedMethods: ['apple-pay'],
// TODO: Test what happens if the merchantId was not set
data: {
merchantIdentifier: paymentConfig.applePay?.merchantId,
supportedNetworks: paymentConfig.supportedNetworks,
countryCode: paymentConfig.merchantCountryCode,
currencyCode: paymentConfig.currency
}
};
const details = {
total: {
label: paymentConfig.applePay?.label,
amount: {
currency: paymentConfig.currency,
value: toMajor(paymentConfig.amount, paymentConfig.currency)
}
}
};
const applePayPaymentRequest = new PaymentRequest([methodData], details);
debugLog(`Moyasar SDK: Apple Pay request: ${JSON.stringify(applePayPaymentRequest)}`);
applePayPaymentRequest.show().then(async paymentResponse => {
debugLog('Moyasar SDK: Got Apple Pay response');
if (paymentResponse.details.paymentData) {
await onApplePayResponse(paymentResponse.details.paymentData, paymentConfig, onPaymentResult);
// TODO: Better handle completion based on `onApplePayResponse` response
paymentResponse.complete('success');
} else {
errorLog('Moyasar SDK: Apple Pay token is null, please use a physical device in order to test Apple Pay');
paymentResponse.complete('failure');
onPaymentResult(new GeneralError('Moyasar SDK: Apple Pay token is null, are you using a Simulator? Please file a bug report if you are not'));
}
}).catch(error => {
// TODO: Should we call the `onPaymentResult` callback here?
errorLog(`Moyasar SDK: Apple Pay payment error: ${error}`);
});
}
})
});
}
//# sourceMappingURL=apple_pay.js.map