Laravel PayMob
A Laravel online payment gateway., (*1)
Table of Contents
- Installation
-
Steps to make a transaction on PayMob servers, (*2)
- API Authentication Request (server side)
- Order Registration Request (server side)
- Payment Key Generation Request (server side)
-
Prepare Client Code to Perform Payment Request (Webclients and mobile apps) (client side)
- Iframe for websites/webapps
- Mobile clients
-
PayMobController, (*3)
- PayMob Postman Collection
- Other PayMob Methods
- TODO
- License
Installation
Require via composer, (*4)
$ composer require baklysystems/laravel-paymob
In config/app.php file, (*5)
'providers' => [
...
BaklySystems\PayMob\PayMobServiceProvider::class,
...
];
'aliases' => [
...
'PayMob' => BaklySystems\PayMob\Facades\PayMob::class,
...
];
First of all, make an account on WeAccept portal, run this command to generate the PayMob configuration file, (*6)
$ php artisan vendor:publish
Then fill in the credentials in config/paymob.php file. Make sure to make an iframe in your dashboard and get the integration id for payment requests., (*7)
Fill in the processed callback and response callback routes in integration details with the routes for processedCallback and invoice methods in PayMobController, (*8)
Steps to make a transaction on PayMob servers
- API Authentication Request
- Order Registration Request
- Payment Key Generation Request
- Prepare Client Code to Perform Payment Request (Webclients and mobile apps)
- Merchant Notification Endpoint
- Transaction Response Endpoint
You can refer to PayMob online guide for more information., (*9)
1. API Authentication Request (server side)
In this step you are required to perform a post request to PayMob's authentication API to obtain authentication token, (*10)
Use PayMob Facade to make requests., (*11)
$auth = PayMob::authPaymob();
// Run this method to get a sample response of auth request.
PayMob::sample('authPaymob');
This method gets the credentials from config/paymob.php file, so fill in username and password first to make this auth request., (*12)
2. Order Registration Request (server side)
At this step you will register an order on Paymob Accept so that you can pay for it later using a transaction., (*13)
$paymobOrder = PayMob::makeOrderPaymob(
$auth->token, // this is token from step 1.
$auth->profile->id, // this is the merchant id from step 1.
$order->totalCost * 100, // total amount by cents/piasters.
$order->id // your (merchant) order id.
);
// Run this method to get a sample response of make order request.
PayMob::sample('makeOrderPaymob');
Store the returned paymob order id in your DB to make transactions with using this id in future., (*14)
3. Payment Key Generation Request (server side)
At this step you will obtain a payment_key token. This key will be used to authenticate your payment request., (*15)
$paymentKey = PayMob::getPaymentKeyPaymob(
$auth->token, // from step 1.
$order->totalCost * 100, // total amount by cents/piasters.
$order->paymob_order_id, // paymob order id from step 2.
// For billing data
$user->email, // optional
$user->firstname, // optional
$user->lastname, // optional
$user->phone, // optional
$city->name, // optional
$country->name // optional
);
// Run this method to get a sample response of payment key request.
PayMob::sample('getPaymentKeyPaymob');
Now that you have obtained payment key, you need to prepare your checkout experience (i.e. client-side code)., (*16)
Iframe for websites/webapps
PayMob recommended iframe, (*17)
<iframe src="https://accept.paymobsolutions.com/api/acceptance/iframes/{{config('paymob.iframe_id')}}?payment_token={{$paymentKey->token}}"></iframe>
Mobile clients
In case of mobile apps, you will need to import Accept native IOS or Android SDK to proceed with the payment and/or save the card details., (*18)
Please request the needed SDK by emailing support@weaccept.co
For more information visit PayMob mobile guid, (*19)
$payment = PayMob::makePayment(
$paymentKey->token, // payment key token from step 3.
$request->card_number,
$request->card_holdername,
$request->card_expiry_mm,
$request->card_expiry_yy,
$request->card_cvn,
$order->paymob_order_id, // PayMob order id from step 2.
$user->firstname,
$user->lastname,
$user->email,
$user->phone
);
// Run this method to get a sample response of make payment for API request.
// processedCallback is for the post response to your processed callback route from PayMob.
PayMob::sample('processedCallback');
// responseCallback is for the Get response to your response callback route from PayMob.
PayMob::sample('responseCallback');
You can use some test cards to make a test payment., (*20)
You can run PayMob::sample() to see available samples., (*21)
PayMobController
We have 4 methods in PayMobController., (*22)
First use checkingOut method to display the payment form page with the iframe. Or simply make payment using payAPI method for mobile clients., (*23)
Then, we have the processedCallback method to catch the POST callback response from PayMob servers, and invoice method to catch the GET callback response and display your invoice page., (*24)
Replace all #code ... with your logic., (*25)
Don't forget to make routes for these methods, and to save the processedCallback and invoice routes in the integration details in PayMob dashboard., (*26)
PayMob Postman Collection
There is a Postman collection for PayMob requests., (*27)
Other PayMob Methods
There are some GET methods to get your data from PayMob., (*28)
1. Get All Orders
PayMob::getOrders(
$auth->token, // token from step 1.
$page // optional for pagination, by default set to 1
);
2. Get a Specific Order
PayMob::getOrder(
$auth->token, // token from step 1.
$order->paymob_order_id // PayMob order id from step 2.
);
3. Get All Transactions
PayMob::getTransactions(
$auth->token, // token from step 1.
$page // optional for pagination, by default set to 1
);
4. Get a Specific Transaction
PayMob::getTransaction(
$auth->token, // token from step 1.
$transactionId // PayMob transaction id from step 4.
);
5. Capture For Auth Transactions
If your transactions is auth type (not standalone), then you have to capture your payment through capture method., (*29)
PayMob::capture(
$auth->token, // token from step 1.
$transactionId, // the returned id from step 4.
$totalCost * 100 // total price/cost in cents/piasters.
);
TODO
- Invoice page.
- Sample transaction cycle.
- Get all orders/transactions page.
- Refund from backend.
- Iframe with JS validations.
- Top level redirect request for 3D secure.
License
Laravel PayMob is a free software distributed under the terms of the MIT license., (*30)