Skip to content

Commit

Permalink
support mobile wallet
Browse files Browse the repository at this point in the history
  • Loading branch information
mgamal92 committed Oct 12, 2022
1 parent ffd4a53 commit d38105a
Showing 1 changed file with 50 additions and 18 deletions.
68 changes: 50 additions & 18 deletions src/Paymob.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,21 +8,21 @@
class Paymob
{
const URL = 'https://accept.paymob.com/api';

/**
* The Integration ID
*
* @var String
*/
protected $integrationId;

/**
* The Iframe ID
*
* @var String
*/
protected $iframeId;

/**
* Constructor
*
Expand All @@ -44,10 +44,10 @@ public function __construct(string $integrationId = null, string $iframeId = nul
public function setIntegrationId(string $integrationId): self
{
$this->integrationId = $integrationId;

return $this;
}

/**
* Set The Iframe ID
*
Expand All @@ -57,10 +57,10 @@ public function setIntegrationId(string $integrationId): self
public function setIframeId(string $iframeId): self
{
$this->iframeId = $iframeId;

return $this;
}

/**
* Paymob Authentication
*
Expand Down Expand Up @@ -121,7 +121,7 @@ public function makeOrder(string $token, bool $deliveryNeeded, int $amountCents,
* @return array
*/
public function getPaymentKey(string $token, int $amountCents, int $expiration, int $orderId, array $billingData, string $currency): array
{
{
$json = [
'auth_token' => $token,
'amount_cents' => $amountCents,
Expand All @@ -137,17 +137,17 @@ public function getPaymentKey(string $token, int $amountCents, int $expiration,
$json
);

return $response->json();
return $response->json();
}

/**
* Make payment for API (moblie clients).
* Return iframe_url
*
*
* @param string $paymentToken
* @return string
*/
public function makePayment(array $data): string
public function makePayment(array $data, $mobileWallet = null): string
{
// step 1 -> Authentication
$authToken = $this->autheticate();
Expand All @@ -158,10 +158,14 @@ public function makePayment(array $data): string
// step 3 => Get Payment Key
$paymentToken = $this->createPaymentToken($authToken, $orderId, $data);

// step 4 => build iframe url
$iframeUrl = $this->buildIframeUrl($paymentToken);
if ($mobileWallet) {
$walletResponse = $this->prepareWalletRedirectionUrl($paymentToken, $mobileWallet);

return $iframeUrl;
return $walletResponse['redirect_url'];
}

// step 4 => build iframe url
return $this->buildIframeUrl($paymentToken);
}

/**
Expand Down Expand Up @@ -240,7 +244,7 @@ private function autheticate(): string
/**
* register order request
* return orderId
*
*
* @param string $authToken
* @param array $data
* @return string
Expand All @@ -255,11 +259,11 @@ private function registerOrder(string $authToken, array $data): string
$orderResponse = $this->makeOrder($authToken, $deliveryNeeded, $amountCents, $items, $merchantOrderId);
return $orderResponse['id'];
}

/**
* create payment token request
* return paymentToken
*
*
* @param string $authToken
* @param string $orderId
* @param array $data
Expand All @@ -274,14 +278,15 @@ private function createPaymentToken(string $authToken, string $orderId, array $d
$currency = (isset($data['currency']) && $data['currency']) ? $data['currency'] : 'EGP';

$paymentKeyResponse = $this->getPaymentKey($authToken, $amountCents, $expiration, $orderId, $billingData, $currency);

return $paymentKeyResponse['token'];
}


/**
* build iframe url using payment token and iframe id
* return iframeUrl
*
*
* @param string $paymentToken
* @return string
*/
Expand All @@ -290,4 +295,31 @@ private function buildIframeUrl(string $paymentToken): string
$iframeUrl = 'https://accept.paymobsolutions.com/api/acceptance/iframes/'. $this->iframeId .'?payment_token='.$paymentToken;
return $iframeUrl;
}

/**
* Send order to paymob servers
*
* @param string $token
* @param bool $deliveryNeeded
* @param int $amountCents
* @param array $items
* @return array
*/
public function prepareWalletRedirectionUrl(string $paymentToken, string $mobileWallet = null)
{
$json = [
'payment_token' => $paymentToken,
'source' => [
'subtype' => 'WALLET',
'identifier' => $mobileWallet,
],
];

$response = Http::post(
self::URL.'/acceptance/payments/pay',
$json
);

return $response->json();
}
}

0 comments on commit d38105a

Please sign in to comment.