react-ravepayment
Version:
This is a reactJS library for implementing rave payment gateway
192 lines (178 loc) • 9.41 kB
JavaScript
import React, { useState, useEffect, forwardRef, useContext } from 'react';
/*! *****************************************************************************
Copyright (c) Microsoft Corporation. All rights reserved.
Licensed under the Apache License, Version 2.0 (the "License"); you may not use
this file except in compliance with the License. You may obtain a copy of the
License at http://www.apache.org/licenses/LICENSE-2.0
THIS CODE IS PROVIDED ON AN *AS IS* BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
KIND, EITHER EXPRESS OR IMPLIED, INCLUDING WITHOUT LIMITATION ANY IMPLIED
WARRANTIES OR CONDITIONS OF TITLE, FITNESS FOR A PARTICULAR PURPOSE,
MERCHANTABLITY OR NON-INFRINGEMENT.
See the Apache Version 2.0 License for specific language governing permissions
and limitations under the License.
***************************************************************************** */
var __assign = function() {
__assign = Object.assign || function __assign(t) {
for (var s, i = 1, n = arguments.length; i < n; i++) {
s = arguments[i];
for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p)) t[p] = s[p];
}
return t;
};
return __assign.apply(this, arguments);
};
function __rest(s, e) {
var t = {};
for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p) && e.indexOf(p) < 0)
t[p] = s[p];
if (s != null && typeof Object.getOwnPropertySymbols === "function")
for (var i = 0, p = Object.getOwnPropertySymbols(s); i < p.length; i++) {
if (e.indexOf(p[i]) < 0 && Object.prototype.propertyIsEnumerable.call(s, p[i]))
t[p[i]] = s[p[i]];
}
return t;
}
var RavePaymentContext = React.createContext({
initializePayment: function () { },
onClose: function () { },
onSuccess: function () { },
});
function RavePaymentProvider(props) {
var children = props.children, onClose = props.onClose, onSuccess = props.onSuccess, others = __rest(props, ["children", "onClose", "onSuccess"]);
var initializePayment = useRavePayment(others).initializePayment;
return (React.createElement(RavePaymentContext.Provider, { value: { initializePayment: initializePayment, onClose: onClose, onSuccess: onSuccess } }, children));
}
// TODO: Provide an error if the user is trying to initialize the two mode concurrently
// For example when a user has already loaded in development and then trying to load in production there should be an error to warn the user
var cachedScripts = [];
function useRaveScript(production) {
var src = !production
? 'https://ravesandboxapi.flutterwave.com/flwv3-pug/getpaidx/api/flwpbf-inline.js'
: 'https://api.ravepay.co/flwv3-pug/getpaidx/api/flwpbf-inline.js';
var _a = useState({
loaded: false,
error: false,
}), state = _a[0], setState = _a[1];
useEffect(function () {
// If cachedScripts array already includes src that means another instance ...
// ... of this hook already loaded this script, so no need to load again.
function loadScript() {
if (cachedScripts.includes(src)) {
setState({
loaded: true,
error: false,
});
return function () { };
}
else {
cachedScripts.push(src);
// Create script
var script_1 = document.createElement('script');
script_1.src = src;
script_1.async = true;
var onScriptLoad_1 = function () {
setState({
loaded: true,
error: false,
});
};
var onScriptError_1 = function () {
var index = cachedScripts.indexOf(src);
if (index >= 0)
cachedScripts.splice(index, 1);
script_1.remove();
setState({
loaded: true,
error: true,
});
};
script_1.addEventListener('load', onScriptLoad_1);
script_1.addEventListener('complete', onScriptLoad_1);
script_1.addEventListener('error', onScriptError_1);
document.body.appendChild(script_1);
return function () {
script_1.removeEventListener('load', onScriptLoad_1);
script_1.removeEventListener('error', onScriptError_1);
};
}
}
loadScript();
}, []);
return [state.loaded, state.error];
}
function constructPaymentOption(option) {
if (!option)
return 'both';
if (Array.isArray(option))
return option.join(',');
return option;
}
function useRavePayment(options) {
var customer_email = options.customer_email, customer_phone = options.customer_phone, amount = options.amount, txref = options.txref, PBFPubKey = options.PBFPubKey, _a = options.meta, meta = _a === void 0 ? [{}] : _a, _b = options.pay_button_text, pay_button_text = _b === void 0 ? '' : _b, _c = options.integrity_hash, integrity_hash = _c === void 0 ? '' : _c, _d = options.currency, currency = _d === void 0 ? 'NGN' : _d, _e = options.country, country = _e === void 0 ? 'NG' : _e, _f = options.customer_firstname, customer_firstname = _f === void 0 ? '' : _f, _g = options.customer_lastname, customer_lastname = _g === void 0 ? '' : _g, _h = options.production, production = _h === void 0 ? false : _h, _j = options.custom_title, custom_title = _j === void 0 ? '' : _j, _k = options.custom_description, custom_description = _k === void 0 ? '' : _k, _l = options.custom_logo, custom_logo = _l === void 0 ? '' : _l, _m = options.redirect_url, redirect_url = _m === void 0 ? '' : _m, payment_options = options.payment_options, _o = options.subaccounts, subaccounts = _o === void 0 ? [{}] : _o, _p = options.payment_plan, payment_plan = _p === void 0 ? '' : _p, _q = options.hosted_payment, hosted_payment = _q === void 0 ? '' : _q, _r = options.campaign_id, campaign_id = _r === void 0 ? '' : _r;
var _s = useRaveScript(production), scriptLoaded = _s[0], scriptError = _s[1];
function initializePayment(onSuccess, onClose) {
if (scriptError) {
throw new Error('Unable to load flutterwave script');
}
if (scriptLoaded) {
var raveOption = {
customer_email: customer_email,
customer_phone: customer_phone,
amount: amount,
txref: txref,
PBFPubKey: PBFPubKey,
meta: meta,
pay_button_text: pay_button_text,
integrity_hash: integrity_hash,
currency: currency,
country: country,
customer_firstname: customer_firstname,
customer_lastname: customer_lastname,
custom_title: custom_title,
custom_description: custom_description,
custom_logo: custom_logo,
redirect_url: redirect_url,
payment_options: constructPaymentOption(payment_options),
subaccounts: subaccounts,
payment_plan: payment_plan,
hosted_payment: hosted_payment,
campaign_id: campaign_id,
onclose: onClose ? onClose : function () { },
callback: onSuccess ? onSuccess : function () { },
};
// @ts-ignore
window.getpaidSetup && window.getpaidSetup(raveOption);
}
}
useEffect(function () {
if (scriptError) {
throw new Error('Unable to load flutterwave script');
}
}, [scriptError]);
return { initializePayment: initializePayment };
}
function RavePayment(_a) {
var children = _a.children, ref = _a.ref;
var _b = useContext(RavePaymentContext), initializePayment = _b.initializePayment, onClose = _b.onClose, onSuccess = _b.onSuccess;
return children({
initializePayment: initializePayment,
onClose: onClose,
onSuccess: onSuccess,
ref: ref,
});
}
var ravePayment = forwardRef(function (_a, ref) {
var children = _a.children, props = __rest(_a, ["children"]);
return (React.createElement(RavePaymentProvider, __assign({}, props),
React.createElement(RavePayment, { ref: ref }, children)));
});
function RavePaymentButton(_a) {
var className = _a.className, text = _a.text, children = _a.children, rest = __rest(_a, ["className", "text", "children"]);
var _b = useContext(RavePaymentContext), initializePayment = _b.initializePayment, onClose = _b.onClose, onSuccess = _b.onSuccess;
function handleClick() {
return initializePayment(onSuccess, onClose);
}
return (React.createElement("button", __assign({ onClick: handleClick, className: className }, rest), text || children));
}
export { ravePayment as RavePayment, RavePaymentButton, RavePaymentContext as RavePaymentContent, RavePaymentProvider as RaveProvider, useRavePayment };
//# sourceMappingURL=index.es.js.map