UNPKG

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

97 lines (92 loc) 4.04 kB
"use strict"; import { Buffer } from 'buffer'; import { debugLog, errorLog } from "../helpers/debug_log.js"; import { ApiError } from "../models/errors/api_error.js"; import { isMoyasarError, NetworkEndpointError, NetworkError } from "../models/errors/moyasar_errors.js"; import { PaymentResponse } from "../models/api/api_responses/payment_response.js"; import { TokenResponse } from "../models/api/api_responses/token_response.js"; import { PaymentType } from "../models/payment_type.js"; const paymentsApiUrl = 'https://api.moyasar.com/v1/payments'; const tokenApiUrl = 'https://api.moyasar.com/v1/tokens'; /** * Makes a POST request. * @param url - The URL to send the request to. * @param jsonPayload - The encoded JSON payload to send. * @param {string} [publishableApiKey] - Moyasar's publishable API key. * * @throws */ async function makeRequest(url, jsonPayload, publishableApiKey) { debugLog('Moyasar SDK: Making backend request...'); const response = await fetch(url, { method: 'POST', headers: publishableApiKey ? buildRequestHeaders(publishableApiKey) : buildRequestHeaders(), body: jsonPayload }); debugLog('Moyasar SDK: Got backend response...'); const responseJson = await response.json(); if (!response.ok) { errorLog(`Moyasar SDK error: Backend request failed with status code: ${response.status} and message: ${JSON.stringify(responseJson)}`); throw new NetworkEndpointError(ApiError.fromJson(responseJson)); } debugLog(`Moyasar SDK: Backend request successful, ${JSON.stringify(responseJson)}`); return responseJson; } /** * Initiates a payment. * @param paymentRequest - The payment request object. * @param publishableApiKey - Moyasar's publishable API key. */ export async function createPayment(paymentRequest, publishableApiKey) { const jsonPayload = JSON.stringify(paymentRequest.toJson()); try { const paymentJson = await makeRequest(paymentsApiUrl, jsonPayload, publishableApiKey); return PaymentResponse.fromJson(paymentJson, paymentRequest.source.type); } catch (error) { return isMoyasarError(error) ? error : new NetworkError('Moyasar SDK: An error occured while processing a Credit Card payment'); } } /** * Creates a Credit Card token. * @param tokenRequest - The token request object. * @param publishableApiKey - Moyasar's publishable API key. */ export async function createToken(tokenRequest, publishableApiKey) { const jsonPayload = JSON.stringify(tokenRequest.toJson()); try { const paymentJson = await makeRequest(tokenApiUrl, jsonPayload, publishableApiKey); return TokenResponse.fromJson(paymentJson); } catch (error) { return isMoyasarError(error) ? error : new NetworkError('Moyasar SDK: An error occured while creating a token request'); } } /** * Sends an OTP (One-Time Password) to the specified URL for payment processing. * @param otp - The OTP value to be sent. * @param url - The URL to which the OTP should be sent. * @param paymentSource - The source of the payment, defaults to PaymentType.stcPay. */ export async function sendOtp(otp, url, paymentSource = PaymentType.stcPay) { const jsonPayload = JSON.stringify({ otp_value: otp }); try { const paymentJson = await makeRequest(url, jsonPayload); return PaymentResponse.fromJson(paymentJson, paymentSource); } catch (error) { return isMoyasarError(error) ? error : new NetworkError('Moyasar SDK: An error occured while processing STC payment'); } } function buildRequestHeaders(apiKey) { const headers = { 'Content-Type': 'application/json', 'X-MOYASAR-LIB': 'moyasar-react-native-sdk', // TODO: Find a better solution for getting the version number to avoid hardcoding it. Importing the package.json file had its own issues after migrating the SDK's tooling and configuration. 'X-REACT-NATIVE-SDK-VERSION': '0.9.0' }; if (apiKey) { headers['Authorization'] = `Basic ${Buffer.from(apiKey).toString('base64')}`; } return headers; } //# sourceMappingURL=payment_service.js.map