react-native-payvessel 1.0.0
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/LICENSE +21 -0
- package/README.md +243 -0
- package/lib/PayvesselCheckout.d.ts +30 -0
- package/lib/PayvesselCheckout.d.ts.map +1 -0
- package/lib/PayvesselCheckout.js +387 -0
- package/lib/PayvesselCheckout.js.map +1 -0
- package/lib/index.d.ts +14 -0
- package/lib/index.d.ts.map +1 -0
- package/lib/index.js +29 -0
- package/lib/index.js.map +1 -0
- package/lib/types.d.ts +168 -0
- package/lib/types.d.ts.map +1 -0
- package/lib/types.js +22 -0
- package/lib/types.js.map +1 -0
- package/lib/usePayvessel.d.ts +74 -0
- package/lib/usePayvessel.d.ts.map +1 -0
- package/lib/usePayvessel.js +97 -0
- package/lib/usePayvessel.js.map +1 -0
- package/package.json +55 -0
- package/src/PayvesselCheckout.tsx +507 -0
- package/src/index.ts +50 -0
- package/src/types.ts +200 -0
- package/src/usePayvessel.ts +156 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 Nex Panther Technologies Ltd
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/README.md
ADDED
|
@@ -0,0 +1,243 @@
|
|
|
1
|
+
# React Native Payvessel
|
|
2
|
+
|
|
3
|
+
A React Native SDK for integrating Payvessel Payment Gateway into your mobile app.
|
|
4
|
+
Built with TypeScript for type safety and similar API to the [npm package](https://www.npmjs.com/package/payvessel-checkout).
|
|
5
|
+
|
|
6
|
+
## Features
|
|
7
|
+
|
|
8
|
+
- 🚀 **Simple API** - Similar to the npm package `payvessel-checkout`
|
|
9
|
+
- 💳 **Multiple Channels** - Bank Transfer and Card payments
|
|
10
|
+
- 📱 **Modal Checkout** - Full-screen WebView modal
|
|
11
|
+
- 🔄 **Callbacks** - onSuccess, onError, onClose support
|
|
12
|
+
- 🪝 **React Hook** - `usePayvessel` hook for easy state management
|
|
13
|
+
- 📝 **TypeScript** - Full TypeScript support with type definitions
|
|
14
|
+
|
|
15
|
+
## Installation
|
|
16
|
+
|
|
17
|
+
```bash
|
|
18
|
+
npm install react-native-payvessel react-native-webview
|
|
19
|
+
# or
|
|
20
|
+
yarn add react-native-payvessel react-native-webview
|
|
21
|
+
```
|
|
22
|
+
|
|
23
|
+
### iOS Setup
|
|
24
|
+
|
|
25
|
+
```bash
|
|
26
|
+
cd ios && pod install
|
|
27
|
+
```
|
|
28
|
+
|
|
29
|
+
### Android Setup
|
|
30
|
+
|
|
31
|
+
No additional setup required.
|
|
32
|
+
|
|
33
|
+
## Usage
|
|
34
|
+
|
|
35
|
+
### Basic Usage with Component
|
|
36
|
+
|
|
37
|
+
```tsx
|
|
38
|
+
import React, { useState } from 'react';
|
|
39
|
+
import { View, Button } from 'react-native';
|
|
40
|
+
import PayvesselCheckout from 'react-native-payvessel';
|
|
41
|
+
|
|
42
|
+
const App = () => {
|
|
43
|
+
const [showCheckout, setShowCheckout] = useState(false);
|
|
44
|
+
|
|
45
|
+
return (
|
|
46
|
+
<View style={{ flex: 1 }}>
|
|
47
|
+
<Button
|
|
48
|
+
title="Pay with Payvessel"
|
|
49
|
+
onPress={() => setShowCheckout(true)}
|
|
50
|
+
/>
|
|
51
|
+
|
|
52
|
+
<PayvesselCheckout
|
|
53
|
+
visible={showCheckout}
|
|
54
|
+
apiKey="YOUR_API_KEY"
|
|
55
|
+
customerEmail="customer@example.com"
|
|
56
|
+
customerPhoneNumber="08012345678"
|
|
57
|
+
amount={1000}
|
|
58
|
+
currency="NGN"
|
|
59
|
+
customerName="John Doe"
|
|
60
|
+
channels={['BANK_TRANSFER', 'CARD']}
|
|
61
|
+
metadata={{ order_id: '12345' }}
|
|
62
|
+
onSuccess={(result) => {
|
|
63
|
+
console.log('Payment successful:', result);
|
|
64
|
+
setShowCheckout(false);
|
|
65
|
+
}}
|
|
66
|
+
onError={(error) => {
|
|
67
|
+
console.log('Payment error:', error);
|
|
68
|
+
}}
|
|
69
|
+
onClose={() => {
|
|
70
|
+
console.log('Checkout closed');
|
|
71
|
+
setShowCheckout(false);
|
|
72
|
+
}}
|
|
73
|
+
/>
|
|
74
|
+
</View>
|
|
75
|
+
);
|
|
76
|
+
};
|
|
77
|
+
|
|
78
|
+
export default App;
|
|
79
|
+
```
|
|
80
|
+
|
|
81
|
+
### Using the Hook
|
|
82
|
+
|
|
83
|
+
```tsx
|
|
84
|
+
import React from 'react';
|
|
85
|
+
import { View, Button } from 'react-native';
|
|
86
|
+
import { PayvesselCheckout, usePayvessel } from 'react-native-payvessel';
|
|
87
|
+
|
|
88
|
+
const App = () => {
|
|
89
|
+
const { openCheckout, checkoutProps } = usePayvessel({
|
|
90
|
+
apiKey: 'YOUR_API_KEY',
|
|
91
|
+
onSuccess: (result) => {
|
|
92
|
+
console.log('Payment successful:', result);
|
|
93
|
+
},
|
|
94
|
+
onError: (error) => {
|
|
95
|
+
console.log('Payment error:', error);
|
|
96
|
+
},
|
|
97
|
+
onClose: () => {
|
|
98
|
+
console.log('Checkout closed');
|
|
99
|
+
},
|
|
100
|
+
});
|
|
101
|
+
|
|
102
|
+
const handlePayment = () => {
|
|
103
|
+
openCheckout({
|
|
104
|
+
email: 'customer@example.com',
|
|
105
|
+
phoneNumber: '08012345678',
|
|
106
|
+
amount: 1000,
|
|
107
|
+
currency: 'NGN',
|
|
108
|
+
name: 'John Doe',
|
|
109
|
+
channels: ['BANK_TRANSFER', 'CARD'],
|
|
110
|
+
metadata: { order_id: '12345' },
|
|
111
|
+
});
|
|
112
|
+
};
|
|
113
|
+
|
|
114
|
+
return (
|
|
115
|
+
<View style={{ flex: 1 }}>
|
|
116
|
+
<Button title="Pay with Payvessel" onPress={handlePayment} />
|
|
117
|
+
{checkoutProps && <PayvesselCheckout {...checkoutProps} />}
|
|
118
|
+
</View>
|
|
119
|
+
);
|
|
120
|
+
};
|
|
121
|
+
|
|
122
|
+
export default App;
|
|
123
|
+
```
|
|
124
|
+
|
|
125
|
+
## Props
|
|
126
|
+
|
|
127
|
+
### PayvesselCheckout
|
|
128
|
+
|
|
129
|
+
| Prop | Type | Required | Description |
|
|
130
|
+
|------|------|----------|-------------|
|
|
131
|
+
| `visible` | boolean | ✅ | Whether the checkout modal is visible |
|
|
132
|
+
| `apiKey` | string | ✅ | Your Payvessel API key |
|
|
133
|
+
| `customerEmail` | string | ✅ | Customer's email |
|
|
134
|
+
| `customerPhoneNumber` | string | ✅ | Customer's phone number |
|
|
135
|
+
| `amount` | string | ✅ | Amount to charge (e.g., "1000") |
|
|
136
|
+
| `currency` | string | ❌ | Currency code (default: "NGN") |
|
|
137
|
+
| `customerName` | string | ✅ | Customer's full name |
|
|
138
|
+
| `channels` | string[] | ❌ | Payment channels (default: ["BANK_TRANSFER"]) |
|
|
139
|
+
| `metadata` | object | ❌ | Custom metadata |
|
|
140
|
+
| `reference` | string | ❌ | Unique transaction reference |
|
|
141
|
+
| `redirectUrl` | string | ❌ | URL to redirect after payment |
|
|
142
|
+
| `onSuccess` | function | ❌ | Called on successful payment |
|
|
143
|
+
| `onError` | function | ❌ | Called on payment error |
|
|
144
|
+
| `onClose` | function | ❌ | Called when checkout is closed |
|
|
145
|
+
| `showHeader` | boolean | ❌ | Show header (default: true) |
|
|
146
|
+
| `headerTitle` | string | ❌ | Header title (default: "Checkout") |
|
|
147
|
+
|
|
148
|
+
### usePayvessel Hook
|
|
149
|
+
|
|
150
|
+
```typescript
|
|
151
|
+
const {
|
|
152
|
+
isVisible, // boolean - checkout visibility
|
|
153
|
+
isLoading, // boolean - loading state
|
|
154
|
+
error, // string | null - error message
|
|
155
|
+
transactionData, // TransactionData | null - transaction data
|
|
156
|
+
openCheckout, // (params: CheckoutParams) => void - open checkout
|
|
157
|
+
closeCheckout, // () => void - close checkout
|
|
158
|
+
resetError, // () => void - reset error state
|
|
159
|
+
checkoutProps, // CheckoutProps | null - props to spread on component
|
|
160
|
+
} = usePayvessel({
|
|
161
|
+
apiKey: 'YOUR_API_KEY',
|
|
162
|
+
onSuccess: (result) => {},
|
|
163
|
+
onError: (error) => {},
|
|
164
|
+
onClose: () => {},
|
|
165
|
+
});
|
|
166
|
+
```
|
|
167
|
+
|
|
168
|
+
## Types
|
|
169
|
+
|
|
170
|
+
```typescript
|
|
171
|
+
import {
|
|
172
|
+
PaymentStatus,
|
|
173
|
+
PaymentChannel,
|
|
174
|
+
CheckoutParams,
|
|
175
|
+
PayvesselSuccessResponse,
|
|
176
|
+
PayvesselErrorResponse,
|
|
177
|
+
} from 'react-native-payvessel';
|
|
178
|
+
|
|
179
|
+
// Payment Status Enum
|
|
180
|
+
enum PaymentStatus {
|
|
181
|
+
SUCCESS = 'success',
|
|
182
|
+
FAILED = 'failed',
|
|
183
|
+
CANCELLED = 'cancelled',
|
|
184
|
+
PENDING = 'pending',
|
|
185
|
+
}
|
|
186
|
+
|
|
187
|
+
// Payment Channel Enum
|
|
188
|
+
enum PaymentChannel {
|
|
189
|
+
BANK_TRANSFER = 'BANK_TRANSFER',
|
|
190
|
+
CARD = 'CARD',
|
|
191
|
+
}
|
|
192
|
+
```
|
|
193
|
+
|
|
194
|
+
## Payment Channels
|
|
195
|
+
|
|
196
|
+
```typescript
|
|
197
|
+
import { PaymentChannel } from 'react-native-payvessel';
|
|
198
|
+
|
|
199
|
+
// Use in channels prop
|
|
200
|
+
<PayvesselCheckout
|
|
201
|
+
channels={[PaymentChannel.BANK_TRANSFER, PaymentChannel.CARD]}
|
|
202
|
+
// ... other props
|
|
203
|
+
/>
|
|
204
|
+
```
|
|
205
|
+
|
|
206
|
+
## Result Objects
|
|
207
|
+
|
|
208
|
+
```typescript
|
|
209
|
+
// Success Response
|
|
210
|
+
interface PayvesselSuccessResponse {
|
|
211
|
+
status: 'success';
|
|
212
|
+
reference?: string;
|
|
213
|
+
transactionId?: string;
|
|
214
|
+
accessCode?: string;
|
|
215
|
+
paymentId?: string;
|
|
216
|
+
data?: TransactionData;
|
|
217
|
+
}
|
|
218
|
+
|
|
219
|
+
// Error Response
|
|
220
|
+
interface PayvesselErrorResponse {
|
|
221
|
+
status: 'failed';
|
|
222
|
+
message: string;
|
|
223
|
+
code?: string;
|
|
224
|
+
}
|
|
225
|
+
```
|
|
226
|
+
|
|
227
|
+
## Comparison with npm Package
|
|
228
|
+
|
|
229
|
+
| npm Package | React Native SDK |
|
|
230
|
+
|-------------|------------------|
|
|
231
|
+
| `Checkout({ api_key })` | `<PayvesselCheckout apiKey="..." />` |
|
|
232
|
+
| `initializeCheckout({ ... })` | Props on component |
|
|
233
|
+
| `onSuccess` | `onSuccess` prop |
|
|
234
|
+
| `onError` | `onError` prop |
|
|
235
|
+
| `onClose` | `onClose` prop |
|
|
236
|
+
|
|
237
|
+
## Example App
|
|
238
|
+
|
|
239
|
+
See the [example](example/) directory for a complete sample app.
|
|
240
|
+
|
|
241
|
+
## License
|
|
242
|
+
|
|
243
|
+
MIT License - see [LICENSE](LICENSE) for details.
|
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
import React from 'react';
|
|
2
|
+
import { PayvesselCheckoutProps } from './types';
|
|
3
|
+
/**
|
|
4
|
+
* PayvesselCheckout Component
|
|
5
|
+
*
|
|
6
|
+
* A React Native component that displays Payvessel checkout in a WebView modal.
|
|
7
|
+
* Directly calls the Payvessel API to initialize transactions.
|
|
8
|
+
*
|
|
9
|
+
* @example
|
|
10
|
+
* ```tsx
|
|
11
|
+
* <PayvesselCheckout
|
|
12
|
+
* visible={showCheckout}
|
|
13
|
+
* apiKey="YOUR_API_KEY"
|
|
14
|
+
* customerEmail="customer@example.com"
|
|
15
|
+
* customerPhoneNumber="08012345678"
|
|
16
|
+
* amount="1000"
|
|
17
|
+
* currency="NGN"
|
|
18
|
+
* customerName="John Doe"
|
|
19
|
+
* channels={['BANK_TRANSFER', 'CARD']}
|
|
20
|
+
* metadata={{ order_id: '12345' }}
|
|
21
|
+
* onSuccess={(response) => console.log('Initialized:', response)}
|
|
22
|
+
* onSuccessfulOrder={(response) => console.log('Payment confirmed:', response)}
|
|
23
|
+
* onError={(error) => console.log('Error:', error)}
|
|
24
|
+
* onClose={() => setShowCheckout(false)}
|
|
25
|
+
* />
|
|
26
|
+
* ```
|
|
27
|
+
*/
|
|
28
|
+
declare const PayvesselCheckout: React.FC<PayvesselCheckoutProps>;
|
|
29
|
+
export default PayvesselCheckout;
|
|
30
|
+
//# sourceMappingURL=PayvesselCheckout.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"PayvesselCheckout.d.ts","sourceRoot":"","sources":["../src/PayvesselCheckout.tsx"],"names":[],"mappings":"AAAA,OAAO,KAAmD,MAAM,OAAO,CAAC;AAaxE,OAAO,EAEL,sBAAsB,EAIvB,MAAM,SAAS,CAAC;AA2BjB;;;;;;;;;;;;;;;;;;;;;;;;GAwBG;AACH,QAAA,MAAM,iBAAiB,EAAE,KAAK,CAAC,EAAE,CAAC,sBAAsB,CA6UvD,CAAC;AAsGF,eAAe,iBAAiB,CAAC"}
|
|
@@ -0,0 +1,387 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
|
|
3
|
+
if (k2 === undefined) k2 = k;
|
|
4
|
+
var desc = Object.getOwnPropertyDescriptor(m, k);
|
|
5
|
+
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
|
|
6
|
+
desc = { enumerable: true, get: function() { return m[k]; } };
|
|
7
|
+
}
|
|
8
|
+
Object.defineProperty(o, k2, desc);
|
|
9
|
+
}) : (function(o, m, k, k2) {
|
|
10
|
+
if (k2 === undefined) k2 = k;
|
|
11
|
+
o[k2] = m[k];
|
|
12
|
+
}));
|
|
13
|
+
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
|
|
14
|
+
Object.defineProperty(o, "default", { enumerable: true, value: v });
|
|
15
|
+
}) : function(o, v) {
|
|
16
|
+
o["default"] = v;
|
|
17
|
+
});
|
|
18
|
+
var __importStar = (this && this.__importStar) || (function () {
|
|
19
|
+
var ownKeys = function(o) {
|
|
20
|
+
ownKeys = Object.getOwnPropertyNames || function (o) {
|
|
21
|
+
var ar = [];
|
|
22
|
+
for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k;
|
|
23
|
+
return ar;
|
|
24
|
+
};
|
|
25
|
+
return ownKeys(o);
|
|
26
|
+
};
|
|
27
|
+
return function (mod) {
|
|
28
|
+
if (mod && mod.__esModule) return mod;
|
|
29
|
+
var result = {};
|
|
30
|
+
if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]);
|
|
31
|
+
__setModuleDefault(result, mod);
|
|
32
|
+
return result;
|
|
33
|
+
};
|
|
34
|
+
})();
|
|
35
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
36
|
+
const react_1 = __importStar(require("react"));
|
|
37
|
+
const react_native_1 = require("react-native");
|
|
38
|
+
const react_native_webview_1 = require("react-native-webview");
|
|
39
|
+
const types_1 = require("./types");
|
|
40
|
+
const PAYVESSEL_BRAND_COLOR = '#ff6b00';
|
|
41
|
+
const CHECKOUT_URL = 'https://checkout.payvessel.com';
|
|
42
|
+
/**
|
|
43
|
+
* PayvesselCheckout Component
|
|
44
|
+
*
|
|
45
|
+
* A React Native component that displays Payvessel checkout in a WebView modal.
|
|
46
|
+
* Directly calls the Payvessel API to initialize transactions.
|
|
47
|
+
*
|
|
48
|
+
* @example
|
|
49
|
+
* ```tsx
|
|
50
|
+
* <PayvesselCheckout
|
|
51
|
+
* visible={showCheckout}
|
|
52
|
+
* apiKey="YOUR_API_KEY"
|
|
53
|
+
* customerEmail="customer@example.com"
|
|
54
|
+
* customerPhoneNumber="08012345678"
|
|
55
|
+
* amount="1000"
|
|
56
|
+
* currency="NGN"
|
|
57
|
+
* customerName="John Doe"
|
|
58
|
+
* channels={['BANK_TRANSFER', 'CARD']}
|
|
59
|
+
* metadata={{ order_id: '12345' }}
|
|
60
|
+
* onSuccess={(response) => console.log('Initialized:', response)}
|
|
61
|
+
* onSuccessfulOrder={(response) => console.log('Payment confirmed:', response)}
|
|
62
|
+
* onError={(error) => console.log('Error:', error)}
|
|
63
|
+
* onClose={() => setShowCheckout(false)}
|
|
64
|
+
* />
|
|
65
|
+
* ```
|
|
66
|
+
*/
|
|
67
|
+
const PayvesselCheckout = ({
|
|
68
|
+
// Visibility
|
|
69
|
+
visible = false,
|
|
70
|
+
// Config
|
|
71
|
+
apiKey,
|
|
72
|
+
// Customer details (required)
|
|
73
|
+
customerEmail, customerPhoneNumber, amount, currency = 'NGN', customerName,
|
|
74
|
+
// Optional
|
|
75
|
+
channels = ['BANK_TRANSFER'], metadata, reference,
|
|
76
|
+
// Callbacks
|
|
77
|
+
onSuccess, onError, onClose, onSuccessfulOrder,
|
|
78
|
+
// UI customization
|
|
79
|
+
showCloseButton = true, closeButtonText = '✕', loadingText = 'Initializing checkout...', headerTitle = 'Checkout', showHeader = true, }) => {
|
|
80
|
+
const webViewRef = (0, react_1.useRef)(null);
|
|
81
|
+
const [isLoading, setIsLoading] = (0, react_1.useState)(true);
|
|
82
|
+
const [isInitializing, setIsInitializing] = (0, react_1.useState)(false);
|
|
83
|
+
const [hasError, setHasError] = (0, react_1.useState)(false);
|
|
84
|
+
const [errorMessage, setErrorMessage] = (0, react_1.useState)('');
|
|
85
|
+
const [checkoutUrl, setCheckoutUrl] = (0, react_1.useState)(null);
|
|
86
|
+
const [transactionData, setTransactionData] = (0, react_1.useState)(null);
|
|
87
|
+
// Get the API base URL based on API key (test or live)
|
|
88
|
+
const getApiBaseUrl = (0, react_1.useCallback)(() => {
|
|
89
|
+
if (apiKey?.startsWith('PVTESTKEY-')) {
|
|
90
|
+
return 'https://sandbox.payvessel.com';
|
|
91
|
+
}
|
|
92
|
+
return 'https://api.payvessel.com';
|
|
93
|
+
}, [apiKey]);
|
|
94
|
+
// Initialize the transaction by calling Payvessel API directly
|
|
95
|
+
const initializeTransaction = (0, react_1.useCallback)(async () => {
|
|
96
|
+
if (!apiKey) {
|
|
97
|
+
setHasError(true);
|
|
98
|
+
setErrorMessage('API key is required');
|
|
99
|
+
onError?.({
|
|
100
|
+
status: types_1.PaymentStatus.FAILED,
|
|
101
|
+
message: 'API key is required',
|
|
102
|
+
});
|
|
103
|
+
return;
|
|
104
|
+
}
|
|
105
|
+
if (!customerEmail || !amount || !customerName) {
|
|
106
|
+
setHasError(true);
|
|
107
|
+
setErrorMessage('Customer email, amount, and name are required');
|
|
108
|
+
onError?.({
|
|
109
|
+
status: types_1.PaymentStatus.FAILED,
|
|
110
|
+
message: 'Customer email, amount, and name are required',
|
|
111
|
+
});
|
|
112
|
+
return;
|
|
113
|
+
}
|
|
114
|
+
setIsInitializing(true);
|
|
115
|
+
setHasError(false);
|
|
116
|
+
setErrorMessage('');
|
|
117
|
+
try {
|
|
118
|
+
const baseUrl = getApiBaseUrl();
|
|
119
|
+
const payload = {
|
|
120
|
+
amount: String(amount),
|
|
121
|
+
currency,
|
|
122
|
+
customer_email: customerEmail,
|
|
123
|
+
customer_name: customerName,
|
|
124
|
+
metadata: metadata || {},
|
|
125
|
+
};
|
|
126
|
+
// Add optional fields
|
|
127
|
+
if (customerPhoneNumber) {
|
|
128
|
+
payload.customer_phone_number = customerPhoneNumber;
|
|
129
|
+
}
|
|
130
|
+
if (channels && channels.length > 0) {
|
|
131
|
+
payload.channels = channels;
|
|
132
|
+
}
|
|
133
|
+
if (reference) {
|
|
134
|
+
payload.reference = reference;
|
|
135
|
+
}
|
|
136
|
+
const response = await fetch(`${baseUrl}/pms/checkout/initialize/`, {
|
|
137
|
+
method: 'POST',
|
|
138
|
+
headers: {
|
|
139
|
+
'Content-Type': 'application/json',
|
|
140
|
+
'api-key': apiKey,
|
|
141
|
+
},
|
|
142
|
+
body: JSON.stringify(payload),
|
|
143
|
+
});
|
|
144
|
+
const result = await response.json();
|
|
145
|
+
if (result?.success === false || !result?.data?.access_code) {
|
|
146
|
+
throw new Error(result?.message || 'Failed to initialize checkout');
|
|
147
|
+
}
|
|
148
|
+
const data = result.data;
|
|
149
|
+
// Store transaction data
|
|
150
|
+
setTransactionData(data);
|
|
151
|
+
// Call onSuccess callback with transaction initialization response
|
|
152
|
+
onSuccess?.({
|
|
153
|
+
status: types_1.PaymentStatus.SUCCESS,
|
|
154
|
+
reference: data.reference,
|
|
155
|
+
transactionId: data.id,
|
|
156
|
+
accessCode: data.access_code,
|
|
157
|
+
data: data,
|
|
158
|
+
});
|
|
159
|
+
// Set the checkout URL with access code
|
|
160
|
+
setCheckoutUrl(`${CHECKOUT_URL}/${data.access_code}`);
|
|
161
|
+
}
|
|
162
|
+
catch (error) {
|
|
163
|
+
const errorMsg = error instanceof Error ? error.message : 'Failed to initialize checkout';
|
|
164
|
+
setHasError(true);
|
|
165
|
+
setErrorMessage(errorMsg);
|
|
166
|
+
onError?.({
|
|
167
|
+
status: types_1.PaymentStatus.FAILED,
|
|
168
|
+
message: errorMsg,
|
|
169
|
+
});
|
|
170
|
+
}
|
|
171
|
+
finally {
|
|
172
|
+
setIsInitializing(false);
|
|
173
|
+
}
|
|
174
|
+
}, [
|
|
175
|
+
apiKey,
|
|
176
|
+
customerEmail,
|
|
177
|
+
customerPhoneNumber,
|
|
178
|
+
amount,
|
|
179
|
+
currency,
|
|
180
|
+
customerName,
|
|
181
|
+
channels,
|
|
182
|
+
metadata,
|
|
183
|
+
reference,
|
|
184
|
+
getApiBaseUrl,
|
|
185
|
+
onSuccess,
|
|
186
|
+
onError
|
|
187
|
+
]);
|
|
188
|
+
// Handle messages from WebView
|
|
189
|
+
const handleMessage = (0, react_1.useCallback)((event) => {
|
|
190
|
+
try {
|
|
191
|
+
const data = JSON.parse(event.nativeEvent.data);
|
|
192
|
+
const { event: eventType, payload: eventPayload } = data;
|
|
193
|
+
switch (eventType) {
|
|
194
|
+
case 'payment_success':
|
|
195
|
+
onSuccessfulOrder?.({
|
|
196
|
+
status: types_1.PaymentStatus.SUCCESS,
|
|
197
|
+
reference: transactionData?.reference,
|
|
198
|
+
transactionId: transactionData?.id,
|
|
199
|
+
data: eventPayload,
|
|
200
|
+
});
|
|
201
|
+
break;
|
|
202
|
+
case 'payment_failed':
|
|
203
|
+
onError?.({
|
|
204
|
+
status: types_1.PaymentStatus.FAILED,
|
|
205
|
+
message: 'Payment failed',
|
|
206
|
+
});
|
|
207
|
+
break;
|
|
208
|
+
case 'payment_closed':
|
|
209
|
+
case 'checkout_closed':
|
|
210
|
+
onClose?.();
|
|
211
|
+
break;
|
|
212
|
+
default:
|
|
213
|
+
break;
|
|
214
|
+
}
|
|
215
|
+
}
|
|
216
|
+
catch {
|
|
217
|
+
// Not a JSON message, ignore
|
|
218
|
+
}
|
|
219
|
+
}, [transactionData, onSuccessfulOrder, onError, onClose]);
|
|
220
|
+
// Inject script to capture postMessage events from checkout
|
|
221
|
+
const injectedJavaScript = `
|
|
222
|
+
(function() {
|
|
223
|
+
// Override window.parent.postMessage to capture events
|
|
224
|
+
const originalPostMessage = window.parent.postMessage;
|
|
225
|
+
window.parent.postMessage = function(message, targetOrigin) {
|
|
226
|
+
// Forward to React Native
|
|
227
|
+
if (window.ReactNativeWebView) {
|
|
228
|
+
try {
|
|
229
|
+
const data = typeof message === 'string' ? JSON.parse(message) : message;
|
|
230
|
+
window.ReactNativeWebView.postMessage(JSON.stringify(data));
|
|
231
|
+
} catch (e) {
|
|
232
|
+
window.ReactNativeWebView.postMessage(JSON.stringify({ event: 'message', payload: message }));
|
|
233
|
+
}
|
|
234
|
+
}
|
|
235
|
+
// Call original
|
|
236
|
+
return originalPostMessage.call(window.parent, message, targetOrigin);
|
|
237
|
+
};
|
|
238
|
+
|
|
239
|
+
// Also listen for message events
|
|
240
|
+
window.addEventListener('message', function(event) {
|
|
241
|
+
if (window.ReactNativeWebView) {
|
|
242
|
+
try {
|
|
243
|
+
const data = typeof event.data === 'string' ? JSON.parse(event.data) : event.data;
|
|
244
|
+
window.ReactNativeWebView.postMessage(JSON.stringify(data));
|
|
245
|
+
} catch (e) {}
|
|
246
|
+
}
|
|
247
|
+
});
|
|
248
|
+
|
|
249
|
+
true;
|
|
250
|
+
})();
|
|
251
|
+
`;
|
|
252
|
+
// Handle close button press
|
|
253
|
+
const handleClose = (0, react_1.useCallback)(() => {
|
|
254
|
+
onClose?.();
|
|
255
|
+
}, [onClose]);
|
|
256
|
+
// Initialize transaction when modal becomes visible
|
|
257
|
+
(0, react_1.useEffect)(() => {
|
|
258
|
+
if (visible) {
|
|
259
|
+
setIsLoading(true);
|
|
260
|
+
setHasError(false);
|
|
261
|
+
setErrorMessage('');
|
|
262
|
+
setCheckoutUrl(null);
|
|
263
|
+
setTransactionData(null);
|
|
264
|
+
initializeTransaction();
|
|
265
|
+
}
|
|
266
|
+
}, [visible, initializeTransaction]);
|
|
267
|
+
// Update loading state when checkout URL is set
|
|
268
|
+
(0, react_1.useEffect)(() => {
|
|
269
|
+
if (checkoutUrl) {
|
|
270
|
+
setIsLoading(false);
|
|
271
|
+
}
|
|
272
|
+
}, [checkoutUrl]);
|
|
273
|
+
if (!visible)
|
|
274
|
+
return null;
|
|
275
|
+
return (react_1.default.createElement(react_native_1.Modal, { visible: visible, animationType: "slide", transparent: false, onRequestClose: handleClose },
|
|
276
|
+
react_1.default.createElement(react_native_1.SafeAreaView, { style: styles.container },
|
|
277
|
+
showHeader && (react_1.default.createElement(react_native_1.View, { style: styles.header },
|
|
278
|
+
react_1.default.createElement(react_native_1.Text, { style: styles.headerTitle }, headerTitle),
|
|
279
|
+
showCloseButton && (react_1.default.createElement(react_native_1.TouchableOpacity, { onPress: handleClose, style: styles.closeButton },
|
|
280
|
+
react_1.default.createElement(react_native_1.Text, { style: styles.closeButtonText }, closeButtonText))))),
|
|
281
|
+
react_1.default.createElement(react_native_1.View, { style: styles.webviewContainer },
|
|
282
|
+
(isLoading || isInitializing) && !hasError && (react_1.default.createElement(react_native_1.View, { style: styles.loadingOverlay },
|
|
283
|
+
react_1.default.createElement(react_native_1.ActivityIndicator, { size: "large", color: PAYVESSEL_BRAND_COLOR }),
|
|
284
|
+
react_1.default.createElement(react_native_1.Text, { style: styles.loadingText }, loadingText))),
|
|
285
|
+
checkoutUrl && !hasError && (react_1.default.createElement(react_native_webview_1.WebView, { ref: webViewRef, source: { uri: checkoutUrl }, style: styles.webview, onMessage: handleMessage, onLoadStart: () => setIsLoading(true), onLoadEnd: () => setIsLoading(false), onError: (e) => {
|
|
286
|
+
setHasError(true);
|
|
287
|
+
setErrorMessage(e.nativeEvent.description || 'WebView error');
|
|
288
|
+
setIsLoading(false);
|
|
289
|
+
onError?.({
|
|
290
|
+
status: types_1.PaymentStatus.FAILED,
|
|
291
|
+
message: e.nativeEvent.description || 'WebView error',
|
|
292
|
+
});
|
|
293
|
+
}, injectedJavaScript: injectedJavaScript, javaScriptEnabled: true, domStorageEnabled: true, startInLoadingState: false, scalesPageToFit: true, mixedContentMode: "compatibility", allowsInlineMediaPlayback: true, mediaPlaybackRequiresUserAction: false, originWhitelist: ['*'] })),
|
|
294
|
+
hasError && (react_1.default.createElement(react_native_1.View, { style: styles.errorOverlay },
|
|
295
|
+
react_1.default.createElement(react_native_1.Text, { style: styles.errorIcon }, "\u26A0\uFE0F"),
|
|
296
|
+
react_1.default.createElement(react_native_1.Text, { style: styles.errorTitle }, "Failed to load checkout"),
|
|
297
|
+
react_1.default.createElement(react_native_1.Text, { style: styles.errorMessage }, errorMessage || 'Please try again or contact support.'),
|
|
298
|
+
react_1.default.createElement(react_native_1.TouchableOpacity, { style: styles.retryButton, onPress: () => {
|
|
299
|
+
setHasError(false);
|
|
300
|
+
setIsLoading(true);
|
|
301
|
+
initializeTransaction();
|
|
302
|
+
} },
|
|
303
|
+
react_1.default.createElement(react_native_1.Text, { style: styles.retryButtonText }, "Retry"))))))));
|
|
304
|
+
};
|
|
305
|
+
const styles = react_native_1.StyleSheet.create({
|
|
306
|
+
container: {
|
|
307
|
+
flex: 1,
|
|
308
|
+
backgroundColor: '#fff',
|
|
309
|
+
},
|
|
310
|
+
header: {
|
|
311
|
+
flexDirection: 'row',
|
|
312
|
+
alignItems: 'center',
|
|
313
|
+
justifyContent: 'center',
|
|
314
|
+
paddingHorizontal: 16,
|
|
315
|
+
paddingVertical: 12,
|
|
316
|
+
borderBottomWidth: 1,
|
|
317
|
+
borderBottomColor: '#eee',
|
|
318
|
+
backgroundColor: '#fff',
|
|
319
|
+
},
|
|
320
|
+
headerTitle: {
|
|
321
|
+
fontSize: 17,
|
|
322
|
+
fontWeight: '600',
|
|
323
|
+
color: '#333',
|
|
324
|
+
},
|
|
325
|
+
closeButton: {
|
|
326
|
+
position: 'absolute',
|
|
327
|
+
right: 16,
|
|
328
|
+
padding: 8,
|
|
329
|
+
},
|
|
330
|
+
closeButtonText: {
|
|
331
|
+
fontSize: 20,
|
|
332
|
+
color: '#666',
|
|
333
|
+
},
|
|
334
|
+
webviewContainer: {
|
|
335
|
+
flex: 1,
|
|
336
|
+
},
|
|
337
|
+
webview: {
|
|
338
|
+
flex: 1,
|
|
339
|
+
},
|
|
340
|
+
loadingOverlay: {
|
|
341
|
+
...react_native_1.StyleSheet.absoluteFillObject,
|
|
342
|
+
backgroundColor: '#fff',
|
|
343
|
+
alignItems: 'center',
|
|
344
|
+
justifyContent: 'center',
|
|
345
|
+
},
|
|
346
|
+
loadingText: {
|
|
347
|
+
marginTop: 16,
|
|
348
|
+
fontSize: 14,
|
|
349
|
+
color: '#666',
|
|
350
|
+
},
|
|
351
|
+
errorOverlay: {
|
|
352
|
+
...react_native_1.StyleSheet.absoluteFillObject,
|
|
353
|
+
backgroundColor: '#fff',
|
|
354
|
+
alignItems: 'center',
|
|
355
|
+
justifyContent: 'center',
|
|
356
|
+
padding: 24,
|
|
357
|
+
},
|
|
358
|
+
errorIcon: {
|
|
359
|
+
fontSize: 48,
|
|
360
|
+
marginBottom: 16,
|
|
361
|
+
},
|
|
362
|
+
errorTitle: {
|
|
363
|
+
fontSize: 18,
|
|
364
|
+
fontWeight: '600',
|
|
365
|
+
color: '#333',
|
|
366
|
+
marginBottom: 8,
|
|
367
|
+
},
|
|
368
|
+
errorMessage: {
|
|
369
|
+
fontSize: 14,
|
|
370
|
+
color: '#666',
|
|
371
|
+
textAlign: 'center',
|
|
372
|
+
marginBottom: 24,
|
|
373
|
+
},
|
|
374
|
+
retryButton: {
|
|
375
|
+
backgroundColor: PAYVESSEL_BRAND_COLOR,
|
|
376
|
+
paddingHorizontal: 32,
|
|
377
|
+
paddingVertical: 12,
|
|
378
|
+
borderRadius: 8,
|
|
379
|
+
},
|
|
380
|
+
retryButtonText: {
|
|
381
|
+
color: '#fff',
|
|
382
|
+
fontSize: 16,
|
|
383
|
+
fontWeight: '600',
|
|
384
|
+
},
|
|
385
|
+
});
|
|
386
|
+
exports.default = PayvesselCheckout;
|
|
387
|
+
//# sourceMappingURL=PayvesselCheckout.js.map
|