From 1d15964a52478e4b2a13618a9acbd6ce9428ab73 Mon Sep 17 00:00:00 2001 From: Mahmoud Saad <67297856+iOSMahmoudSaad@users.noreply.github.com> Date: Tue, 22 Sep 2026 13:04:47 +0300 Subject: [PATCH] add embded view to full doc Updated import statements and payment result handling in the README. Added details about SDK listener and payment status handling. --- README.md | 293 ++++++++++++++++++++++++++++++++++++++++++++++++++---- 1 file changed, 272 insertions(+), 21 deletions(-) diff --git a/README.md b/README.md index 501ba9a..0b15c70 100644 --- a/README.md +++ b/README.md @@ -63,7 +63,10 @@ To get started with the `paymob-reactnative` package, follow these steps: To begin using the Paymob SDK in your React Native application, start by importing the module in your component: ```javascript -import Paymob, { PaymentResult, CreditCardType } from 'paymob-reactnative'; +import Paymob, { + PaymentStatus, + FailureCallBackVersion, +} from 'paymob-reactnative'; ``` ### SDK Customization @@ -88,46 +91,294 @@ These customization options allow you to tailor the Paymob SDK interface to alig ### Adding a Listener -To handle payment results effectively, you can add a listener that will respond to different transaction statuses. This is crucial for providing feedback to users about their payment transactions: +To handle payment results, register a listener with `Paymob.setSdkListener`. The listener receives a response object each time the full-screen payment flow started by `Paymob.presentPayVC()` finishes, and it is the single entry point for the success, failure, cancellation, and pending outcomes: ```javascript -Paymob.setSdkListener((status: PaymentResult) => { - switch (status) { - case PaymentResult.SUCCESS: - // Handle successful payment +Paymob.setSdkListener((response) => { + switch (response.status) { + case PaymentStatus.SUCCESS: + // Handle successful payment (transaction data is in response.details) break; - case PaymentResult.FAIL: - // Handle failed payment + case PaymentStatus.FAIL: + // Handle failed payment (the reason is in response.message) break; - case PaymentResult.PENDING: + case PaymentStatus.CANCELLED: + // Handle a payment cancelled by the user + break; + case PaymentStatus.PENDING: // Handle pending payment status break; } }); ``` -This listener will allow you to implement logic based on the result of the payment process, enhancing the user experience. +The `response` object has the following fields: + +| Field | Type | Description | +| --- | --- | --- | +| `status` | `string` | One of `PaymentStatus.SUCCESS` (`'Success'`), `PaymentStatus.FAIL` (`'Fail'`), `PaymentStatus.CANCELLED` (`'Cancelled'`), or `PaymentStatus.PENDING` (`'Pending'`). | +| `details` | `object` | Transaction data returned by the native SDK for a successful payment. On Android it is also present, as an empty object, for the other statuses; on iOS it is omitted. | +| `message` | `string` | The failure reason. Only present when `status` is `'Fail'`. | + +Keep the following in mind: + +- Only one listener is active at a time. Calling `setSdkListener` again replaces the previous listener, and `Paymob.removeSdkListener()` removes it (for example, when your component unmounts). +- Register the listener before calling `Paymob.presentPayVC()`. On Android, results are only delivered while a listener is registered. +- The native module delivers results through the `onTransactionStatus` event. `setSdkListener` subscribes to it for you, so you do not need to use `NativeEventEmitter` directly. + +### Handling Payment Failures + +A failed payment is reported through the same listener, as a response with `status` equal to `PaymentStatus.FAIL`. There is no separate failure API for the full-screen flow. + +**Callback parameters** + +- `response.status`: `'Fail'` (`PaymentStatus.FAIL`). +- `response.message`: a string describing why the payment failed, as reported by the native SDK. + +**Failure callback version** + +You can select the failure callback version used by the native SDK with `Paymob.setFailureCallbackVersion`: + +```javascript +Paymob.setFailureCallbackVersion(FailureCallBackVersion.V2); +``` + +- Supported values are `FailureCallBackVersion.V1` and `FailureCallBackVersion.V2`. The strings `'V1'` and `'V2'` are also accepted (case-insensitive); any other value falls back to `V1`. +- Call it before `Paymob.presentPayVC()`, like the other SDK customizations. +- It applies to the full-screen flow started by `Paymob.presentPayVC()`. The embedded view does not use it. +- The React Native bridge always delivers the failure reason to your listener as `response.message`. What differs between `V1` and `V2` is defined by the native SDKs and is not documented in this package. + +**Complete example** + +```javascript +import React, { useEffect } from 'react'; +import { Alert, Button, View } from 'react-native'; +import Paymob, { + PaymentStatus, + FailureCallBackVersion, +} from 'paymob-reactnative'; + +const showAlert = (title, message) => + requestAnimationFrame(() => Alert.alert(title, message)); + +export default function CheckoutScreen({ clientSecret, publicKey }) { + useEffect(() => { + Paymob.setSdkListener((response) => { + switch (response.status) { + case PaymentStatus.SUCCESS: + console.log('Payment succeeded', response.details); + break; + case PaymentStatus.FAIL: + showAlert('Payment failed', response.message ?? 'Unknown error'); + break; + case PaymentStatus.CANCELLED: + showAlert('Payment cancelled'); + break; + case PaymentStatus.PENDING: + showAlert('Payment pending'); + break; + } + }); + + return () => { + Paymob.removeSdkListener(); + }; + }, []); + + const pay = () => { + Paymob.setFailureCallbackVersion(FailureCallBackVersion.V2); + Paymob.presentPayVC(clientSecret, publicKey); + }; + + return ( + +