PHP code example of samir-hussein / paymob
1. Go to this page and download the library: Download samir-hussein/paymob library . Choose the download type require .
2. Extract the ZIP file and open the index.php.
3. Add this code to the index.php.
<?php
require_once('vendor/autoload.php');
/* Start to develop here. Best regards https://php-download.com/ */
samir-hussein / paymob example snippets
use Paymob\PayMob;
$config = [
'PayMob_User_Name' => 'your_username',
'PayMob_Password' => 'your_password',
'PayMob_Integration_Id' => 'Integration_Id',
];
$init = new PayMob($config);
$auth = PayMob::AuthenticationRequest();
$order = PayMob::OrderRegistrationAPI([
'auth_token' => $auth->token, // from step 3
'amount_cents' => 150 * 100, //put your price
'currency' => 'EGP',
'delivery_needed' => false, // another option true
'merchant_order_id' => 6, //put order id from your database must be unique id
'items' => [[ // all items information
"name" => "ASC1515",
"amount_cents" => 150 * 100,
"description" => "Smart Watch",
"quantity" => "2"
]]
]);
$PaymentKey = PayMob::PaymentKeyRequest([
'auth_token' => $auth->token, // from step 3
'amount_cents' => 150 * 100,//put your price
'currency' => 'EGP',
'order_id' => $order->id, // from step 4
"billing_data" => [ // put your client information
"apartment" => "803",
"email" => "[email protected] ",
"floor" => "42",
"first_name" => "Clifford",
"street" => "Ethan Land",
"building" => "8028",
"phone_number" => "+86(8)9135210487",
"shipping_method" => "PKG",
"postal_code" => "01898",
"city" => "Jaskolskiburgh",
"country" => "CR",
"last_name" => "Nicolas",
"state" => "Utah"
]
]);
PayMob::refundTransaction(
$auth_token, // from step 3
$transaction_id,
$amount_cents // amount in cent 100 EGP = 100 * 100 cent
);
PayMob::voidTransaction(
$auth_token, // from step 3
$transaction_id,
);
//in providers
PayMob\PayMobServiceProvider::class,
//in aliases
'PayMob' => PayMob\Facades\PayMob::class,
namespace App\Http\Controllers;
use PayMob\Facades\PayMob;
use Illuminate\Http\Request;
class PayMobController extends Controller
{
public function index()
{
$auth = PayMob::AuthenticationRequest();
$order = PayMob::OrderRegistrationAPI([
'auth_token' => $auth->token,
'amount_cents' => 150 * 100, //put your price
'currency' => 'EGP',
'delivery_needed' => false, // another option true
'merchant_order_id' => 1000, //put order id from your database must be unique id
'items' => [] // all items information or leave it empty
]);
$PaymentKey = PayMob::PaymentKeyRequest([
'auth_token' => $auth->token,
'amount_cents' => 150 * 100, //put your price
'currency' => 'EGP',
'order_id' => $order->id,
"billing_data" => [ // put your client information
"apartment" => "803",
"email" => "[email protected] ",
"floor" => "42",
"first_name" => "Clifford",
"street" => "Ethan Land",
"building" => "8028",
"phone_number" => "+86(8)9135210487",
"shipping_method" => "PKG",
"postal_code" => "01898",
"city" => "Jaskolskiburgh",
"country" => "CR",
"last_name" => "Nicolas",
"state" => "Utah"
]
]);
return view('paymob')->with(['token' => $PaymentKey->token]);
}
}
Route::post('/checkout/processed',function(Request $request){
$request_hmac = $request->hmac;
$calc_hmac = PayMob::calcHMAC($request);
if ($request_hmac == $calc_hmac) {
$order_id = $request->obj['order']['merchant_order_id'];
$amount_cents = $request->obj['amount_cents'];
$transaction_id = $request->obj['id'];
$order = Order::find($order_id);
if ($request->obj['success'] == true && ($order->total_price * 100) == $amount_cents) {
$order->update([
'payment_status' => 'finished',
'transaction_id' => $transaction_id
]);
} else {
$order->update([
'payment_status' => "failed",
'transaction_id' => $transaction_id
]);
}
}
});
Route::post('/refund', function () {
$auth = PayMob::AuthenticationRequest();
return PayMob::refundTransaction(
$auth->token,
$transaction_id,
$amount_cents // amount in cent 100 EGP = 100 * 100 cent
);
});
Route::post('/void', function () {
$auth = PayMob::AuthenticationRequest();
return PayMob::voidTransaction(
$auth->token,
$transaction_id,
);
});
bash
php artisan vendor:publish --provider="PayMob\PayMobServiceProvider"
html
<iframe
width="100%"
height="800"
src="https://accept.paymob.com/api/acceptance/iframes/your_iframe_id?payment_token={{$token}}"
>
</iframe>