@reevit/react 0.3.5 → 0.3.7

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/index.mjs CHANGED
@@ -1,5 +1,6 @@
1
1
  import { createContext, useReducer, useRef, useEffect, useCallback, useState, useContext } from 'react';
2
2
  import { jsxs, jsx, Fragment } from 'react/jsx-runtime';
3
+ import CheckoutSdk from '@hubteljs/checkout';
3
4
 
4
5
  // src/components/ReevitCheckout.tsx
5
6
 
@@ -804,74 +805,69 @@ function PaystackBridge({
804
805
  /* @__PURE__ */ jsx("p", { children: "Connecting to Paystack..." })
805
806
  ] }) });
806
807
  }
807
- function loadHubtelScript() {
808
- return new Promise((resolve, reject) => {
809
- if (window.HubtelCheckout) {
810
- resolve();
811
- return;
812
- }
813
- const script = document.createElement("script");
814
- script.src = "https://checkout-v3.hubtel.com/js/checkout.js";
815
- script.async = true;
816
- script.onload = () => resolve();
817
- script.onerror = () => reject(new Error("Failed to load Hubtel script"));
818
- document.head.appendChild(script);
819
- });
820
- }
821
808
  function HubtelBridge({
822
809
  merchantAccount,
823
810
  amount,
824
- currency = "GHS",
825
811
  reference,
826
- email,
827
812
  phone,
828
813
  description = "Payment",
814
+ callbackUrl,
815
+ basicAuth,
829
816
  onSuccess,
830
817
  onError,
831
818
  onClose,
832
819
  autoStart = true
833
820
  }) {
834
821
  const initialized = useRef(false);
822
+ const checkoutRef = useRef(null);
835
823
  const startPayment = useCallback(async () => {
836
824
  try {
837
- await loadHubtelScript();
838
- if (!window.HubtelCheckout) {
839
- throw new Error("Hubtel checkout not available");
840
- }
841
- window.HubtelCheckout.initPayment({
842
- merchantAccount,
843
- basicDescription: description,
844
- totalAmount: amount / 100,
845
- // Hubtel expects amount in major units (GHS, not pesewas)
846
- currency,
847
- clientReference: reference || `hubtel_${Date.now()}`,
848
- customerEmail: email,
849
- customerMsisdn: phone,
850
- onComplete: (response) => {
851
- if (response.status === "Success") {
825
+ const checkout = new CheckoutSdk();
826
+ checkoutRef.current = checkout;
827
+ const purchaseInfo = {
828
+ amount: amount / 100,
829
+ // Convert from minor to major units
830
+ purchaseDescription: description,
831
+ customerPhoneNumber: phone || "",
832
+ clientReference: reference || `hubtel_${Date.now()}`
833
+ };
834
+ const config = {
835
+ branding: "enabled",
836
+ callbackUrl: callbackUrl || window.location.href,
837
+ merchantAccount: typeof merchantAccount === "string" ? parseInt(merchantAccount, 10) : merchantAccount,
838
+ basicAuth: basicAuth || ""
839
+ };
840
+ checkout.openModal({
841
+ purchaseInfo,
842
+ config,
843
+ callBacks: {
844
+ onInit: () => console.log("Hubtel checkout initialized"),
845
+ onPaymentSuccess: (data) => {
852
846
  const result = {
853
- paymentId: response.transactionId,
854
- reference: response.clientReference,
855
- amount: Math.round(response.amount * 100),
856
- // Convert back to pesewas
857
- currency: response.currency,
847
+ paymentId: data.transactionId || reference || "",
848
+ reference: data.clientReference || reference || "",
849
+ amount,
850
+ currency: "GHS",
858
851
  paymentMethod: "mobile_money",
859
852
  psp: "hubtel",
860
- pspReference: response.transactionId,
853
+ pspReference: data.transactionId || "",
861
854
  status: "success"
862
855
  };
863
856
  onSuccess(result);
864
- } else {
857
+ checkout.closePopUp();
858
+ },
859
+ onPaymentFailure: (data) => {
865
860
  const error = {
866
861
  code: "PAYMENT_FAILED",
867
- message: response.message || "Payment failed",
862
+ message: data.message || "Payment failed",
868
863
  recoverable: true
869
864
  };
870
865
  onError(error);
866
+ },
867
+ onLoad: () => console.log("Hubtel checkout loaded"),
868
+ onClose: () => {
869
+ onClose();
871
870
  }
872
- },
873
- onCancel: () => {
874
- onClose();
875
871
  }
876
872
  });
877
873
  } catch (err) {
@@ -883,7 +879,7 @@ function HubtelBridge({
883
879
  };
884
880
  onError(error);
885
881
  }
886
- }, [merchantAccount, amount, currency, reference, email, phone, description, onSuccess, onError, onClose]);
882
+ }, [merchantAccount, amount, reference, phone, description, callbackUrl, basicAuth, onSuccess, onError, onClose]);
887
883
  useEffect(() => {
888
884
  if (autoStart && !initialized.current) {
889
885
  initialized.current = true;
@@ -895,6 +891,37 @@ function HubtelBridge({
895
891
  /* @__PURE__ */ jsx("p", { children: "Connecting to Hubtel..." })
896
892
  ] }) });
897
893
  }
894
+ function openHubtelPopup(config) {
895
+ const checkout = new CheckoutSdk();
896
+ const purchaseInfo = {
897
+ amount: config.amount,
898
+ purchaseDescription: config.description,
899
+ customerPhoneNumber: config.customerPhoneNumber || "",
900
+ clientReference: config.clientReference || `hubtel_${Date.now()}`
901
+ };
902
+ const checkoutConfig = {
903
+ branding: "enabled",
904
+ callbackUrl: config.callbackUrl || window.location.href,
905
+ merchantAccount: typeof config.merchantAccount === "string" ? parseInt(config.merchantAccount, 10) : config.merchantAccount,
906
+ basicAuth: config.basicAuth || ""
907
+ };
908
+ checkout.openModal({
909
+ purchaseInfo,
910
+ config: checkoutConfig,
911
+ callBacks: {
912
+ onPaymentSuccess: (data) => {
913
+ config.onSuccess?.(data);
914
+ checkout.closePopUp();
915
+ },
916
+ onPaymentFailure: (data) => {
917
+ config.onError?.(data);
918
+ },
919
+ onClose: () => {
920
+ config.onClose?.();
921
+ }
922
+ }
923
+ });
924
+ }
898
925
  function loadFlutterwaveScript() {
899
926
  return new Promise((resolve, reject) => {
900
927
  if (window.FlutterwaveCheckout) {
@@ -1688,13 +1715,14 @@ function ReevitCheckout({
1688
1715
  return /* @__PURE__ */ jsx(
1689
1716
  HubtelBridge,
1690
1717
  {
1691
- merchantAccount: pspKey,
1718
+ merchantAccount: paymentIntent?.pspCredentials?.merchantAccount || pspKey,
1692
1719
  amount: paymentIntent?.amount ?? amount,
1693
1720
  currency: paymentIntent?.currency ?? currency,
1694
1721
  reference: paymentIntent?.reference || reference,
1695
1722
  email,
1696
1723
  phone: momoData?.phone || phone,
1697
1724
  description: `Payment ${paymentIntent?.reference || reference || ""}`,
1725
+ basicAuth: paymentIntent?.pspCredentials?.basicAuth,
1698
1726
  onSuccess: handlePSPSuccess,
1699
1727
  onError: (err) => handlePSPError(err),
1700
1728
  onClose: handlePSPClose
@@ -1888,6 +1916,6 @@ function ReevitCheckout({
1888
1916
  ] });
1889
1917
  }
1890
1918
 
1891
- export { FlutterwaveBridge, HubtelBridge, MPesaBridge, MobileMoneyForm, MonnifyBridge, PaymentMethodSelector, PaystackBridge, ReevitAPIClient, ReevitCheckout, StripeBridge, createReevitClient, detectNetwork, formatAmount, formatPhone, loadFlutterwaveScript, loadHubtelScript, loadMonnifyScript, loadPaystackScript, loadStripeScript, useMPesaStatusPolling, useReevit, useReevitContext, validatePhone };
1919
+ export { FlutterwaveBridge, HubtelBridge, MPesaBridge, MobileMoneyForm, MonnifyBridge, PaymentMethodSelector, PaystackBridge, ReevitAPIClient, ReevitCheckout, StripeBridge, createReevitClient, detectNetwork, formatAmount, formatPhone, loadFlutterwaveScript, loadMonnifyScript, loadPaystackScript, loadStripeScript, openHubtelPopup, useMPesaStatusPolling, useReevit, useReevitContext, validatePhone };
1892
1920
  //# sourceMappingURL=index.mjs.map
1893
1921
  //# sourceMappingURL=index.mjs.map