Skip to main content

flutter_xpay 0.1.0 copy "flutter_xpay: ^0.1.0" to clipboard
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:

  1. creates a Checkout Session (POST https://api.xpay.app/checkout/sessions), or takes one your server created;
  2. opens the hosted page in a WebView;
  3. 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 secretKey in the app only for testing. In production your server creates the session and the app calls openCheckout.


πŸ”§ 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
);
  • returnUrl must be HTTPS on a domain you control. XPay accepts localhost in 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.


  1. 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 url and id to the app.

  2. 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 calls GET /checkout/sessions/:id.

  3. Server fulfils the order on the checkout.session.completed / checkout.session.async_payment_succeeded webhook, and only when paymentStatus == "paid". Verify the XPay-Signature header.


🧰 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 get processing, and session.fawryVoucher has the reference when the Payment Intent is expanded.
  • Sessions expire after 24 h by default. Pass extra: {'expiresAfterMinutes': 60} to change it.

πŸ“ž Contact #

Ahmed Hosni - LinkedIn

0
likes
150
points
--
downloads

Documentation

API reference

Publisher

unverified uploader

Accept XPay (xpay.app) payments in Flutter: cards, Valu, Fawry and more through XPay Hosted Checkout.

Repository (GitHub)

Topics

#payments #xpay #payment-gateway #egypt

License

GPL-3.0 (license)

Dependencies

flutter, http, webview_flutter

More

Packages that depend on flutter_xpay