flutter_xpay 0.1.0
flutter_xpay: ^0.1.0 copied to clipboard
Accept XPay (xpay.app) payments in Flutter: cards, Valu, Fawry and more through XPay Hosted Checkout.
π³ Flutter XPay β XPay Checkout for Flutter #
Accept Visa, Mastercard, Valu, Fawry and the other methods on your XPay account inside your Flutter app.
XPay has no native mobile SDK yet. Its docs say Hosted Checkout "works in any in-app browser or WebView". This package does that for you:
- creates a Checkout Session (
POST https://api.xpay.app/checkout/sessions), or takes one your server created; - opens the hosted page in a WebView;
- closes it when XPay redirects to your return URL and tells you whether the payment is paid.
Unofficial package, not made by XPay.
π₯ Installation #
dependencies:
flutter_xpay: ^0.1.0
Uses webview_flutter: Android minSdk 21+, iOS 12+.
π Keys #
| Key | Where | Used for |
|---|---|---|
Secret key sk_test_... / sk_live_... |
Dashboard β Developer β API Keys | Creating / reading sessions |
The key decides the mode: sk_test_* creates cs_test_* sessions. The API URL is the same for test and live.
β οΈ Don't ship a live secret key in the app. Put
secretKeyin the app only for testing. In production your server creates the session and the app callsopenCheckout.
π§ Initialize #
XPay.instance.initialize(
returnUrl: 'https://yourshop.example/xpay/return/{CHECKOUT_SESSION_ID}',
cancelUrl: 'https://yourshop.example/xpay/failed', // optional
secretKey: 'sk_test_...', // testing only
);
returnUrlmust be HTTPS on a domain you control. XPay acceptslocalhostin test mode only. The page doesn't have to exist, because the WebView closes as soon as XPay redirects to it. XPay replaces{CHECKOUT_SESSION_ID}with the session id.cancelUrl: XPay redirects here when a payment attempt fails (card declined, 3-D Secure rejectedβ¦). Without it, the customer sees the error on the XPay page and can retry there.
β‘ Quick test flow (secret key in the app) #
final result = await XPay.instance.pay(
context,
lineItems: const [
XPayLineItem(name: 'T-shirt', unitAmount: 250, quantity: 2), // 250.00 EGP each
XPayLineItem(name: 'Shipping', unitAmount: 50),
],
locale: XPayLocale.ar,
paymentMethodTypes: ['card', 'valu'], // optional
metadata: {'orderId': '1042'},
);
switch (result.status) {
case XPayCheckoutStatus.paid: // XPay says paymentStatus == paid
case XPayCheckoutStatus.processing: // finished, not confirmed yet (e.g. Fawry)
case XPayCheckoutStatus.failed: // redirected to cancelUrl
case XPayCheckoutStatus.dismissed: // customer closed the screen
}
Amounts are in major units (1499.0 = 1,499.00 EGP). The package converts them to the minor units XPay expects.
π Production flow (recommended) #
-
Server creates the session:
curl -X POST https://api.xpay.app/checkout/sessions \ -H "Authorization: Bearer sk_live_..." \ -H "Content-Type: application/json" \ -d '{ "afterCompletion": { "type": "redirect", "redirect": { "url": "https://yourshop.example/xpay/return/{CHECKOUT_SESSION_ID}" } }, "cancelUrl": "https://yourshop.example/xpay/failed", "lineItems": [ { "priceData": { "currency": "EGP", "unitAmount": 149900, "productData": { "name": "Order #1042" } }, "quantity": 1 } ] }'It returns
urlandidto the app. -
App opens it:
XPay.instance.initialize( returnUrl: 'https://yourshop.example/xpay/return/{CHECKOUT_SESSION_ID}', cancelUrl: 'https://yourshop.example/xpay/failed', ); final result = await XPay.instance.openCheckout( context, checkoutUrl: session['url'], sessionId: session['id'], );Without a secret key the app can't read the session, so a finished checkout comes back as
processing. Ask your server for the session status: it callsGET /checkout/sessions/:id. -
Server fulfils the order on the
checkout.session.completed/checkout.session.async_payment_succeededwebhook, and only whenpaymentStatus == "paid". Verify theXPay-Signatureheader.
π§° API client #
XPay.instance.api (or XPayApi(secretKey: ...), e.g. in a Dart backend):
| Method | Endpoint |
|---|---|
createCheckoutSession(...) |
POST /checkout/sessions |
retrieveCheckoutSession(id) |
GET /checkout/sessions/:id |
expireCheckoutSession(id) |
POST /checkout/sessions/:id/expire |
createCheckoutSession takes extra for fields this package doesn't model yet (customerDetails, discounts, feeConfig, brandingSettings, β¦). Errors throw XPayException with statusCode, type, code, param, requestId.
XPayCheckoutSession gives you id, status (open / complete / expired), paymentStatus (unpaid / paid / noPaymentRequired), url, amountTotal, paymentIntentId (needed for refunds), fawryVoucher and raw.
Line items #
XPayLineItem(
name: 'Hoodie',
unitAmount: 899,
quantity: 1,
description: 'Size L',
image: 'https://yourshop.example/hoodie.png',
externalId: 'SKU-42',
adjustableQuantity: const XPayAdjustableQuantity(minimum: 1, maximum: 5),
);
const XPayLineItem.price('price_...', quantity: 2); // price from the dashboard
All lines must use the same currency.
π§ͺ Test data (test mode) #
| Card | Network |
|---|---|
| 5123 4500 0000 0008 | Mastercard (EGP) |
| 4508 7500 1574 1019 | Visa (EGP) |
The expiry date decides the outcome: 01/39 approved, 05/39 declined, 01/27 insufficient funds. CVV 100 = match.
Valu: mobile 01222728511, OTP 123456. Fawry and Valu need a total of at least 50 EGP.
π¨ Notes #
- The in-app result is for your UI only. Fulfil orders from the webhook.
- Fawry is asynchronous. The checkout finishes with
paymentStatus: unpaid, and the customer pays later at an outlet. You getprocessing, andsession.fawryVoucherhas the reference when the Payment Intent is expanded. - Sessions expire after 24 h by default. Pass
extra: {'expiresAfterMinutes': 60}to change it.