PayTabs Payment Flow
Flow Overview
These diagrams outline the process for handling VIP subscription purchases using the PayTabs payment gateway.
1. Purchase Initiation
2. Payment Execution, Callback & Subscription Update
Process Description
-
Purchase Initiation:
- User selects a
VipPackageon the frontend and chooses to pay via Credit Card (PayTabs). - Frontend calls the backend's
createOrderendpoint, passing thevip_package_id. PayTabsService::createOrdercalculates totals and creates anOrderrecord withstatus = PENDINGandpayment_method = CREDIT_CARD.- It then calls
Initiating, which validates config and callscreatePayPage. createPayPageuses the PayTabs SDK Facade (paypage) to configure the payment request (cart details, customer info, URLs, language, framing).- The service returns configuration details (like cart ID, amount, description) needed by the frontend Flutter SDK to initiate the PayTabs payment UI.
- User selects a
-
Payment Execution:
- The user interacts with the PayTabs payment interface within the mobile app (using the Flutter SDK) and enters their payment details.
- PayTabs processes the payment.
-
PayTabs Callback Handling:
- Upon completion (success or failure), PayTabs sends a callback request to the backend endpoint (
/paytabs/callback), including thecart_id(Order ID) andtran_ref(PayTabs transaction reference). - The backend route invokes
PayTabsService::callbackwith these parameters. PayTabsService::callbackcallsgetTransactionResponse(which usesPaypage::queryTransaction) to get the final status of the transaction from PayTabs using thetran_ref.- The service checks
isPaymentSuccessfulbased on the response status ('A'), cart ID match, transaction type ('Sale'), and overall success flag. - If Successful:
- It checks if the
UseralreadyhasSubscription. - Renewal: If a subscription exists, it updates the
Orderstatus toCOMPLETEDquietly (to prevent the observer from firing immediately) and stores thepayment_reference_idandpayment_card_type. It then calls its ownrenewSubscriptionmethod, which in turn callsVipSubscriptionService::renewVipSubscriptionto update the existingVipSubscription. (See VIP Subscriptions Flow for details). - New Subscription: If no subscription exists, it updates the
Orderstatus toCOMPLETEDnormally, storing thepayment_reference_idandpayment_card_type. This update triggers theOrderObserver, which calls theVipSubscriptionServiceto create a newVipSubscription. (See VIP Subscriptions Flow for details).
- It checks if the
- If Failed:
- The service checks
isPaymentFailed. - It updates the
Orderstatus toFAILEDand stores thepayment_reference_idandpayment_card_type.
- The service checks
- Upon completion (success or failure), PayTabs sends a callback request to the backend endpoint (
Key Models & Services
- PayTabsService: Handles the creation of PayTabs payment requests and processes the callbacks.
- Order: Tracks the purchase transaction, status, payment method, and PayTabs transaction reference.
- VipPackage: Defines the subscription plan being purchased.
- User: The customer account making the purchase.
- VipSubscriptionService: Handles the creation and renewal logic for
VipSubscriptionrecords, triggered after successful payment confirmation byPayTabsServiceor theOrderObserver. (See VIP Subscriptions Flow). - OrderObserver: Listens for
Orderupdates to triggerVipSubscriptionServicefor new subscriptions. - Paytabscom\Laravel_paytabs\Facades\paypage: The PayTabs SDK Facade used for interacting with the PayTabs API.