Skip to content

Commit

Permalink
clean up
Browse files Browse the repository at this point in the history
  • Loading branch information
mgamal92 committed Dec 27, 2022
1 parent 681a1b8 commit 3ee4fa3
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 137 deletions.
44 changes: 0 additions & 44 deletions src/Controllers/PaymobController.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,52 +5,8 @@
namespace MG\Paymob\Controllers;

use App\Http\Controllers\Controller;
use MG\Paymob\Paymob;

class PaymobController extends Controller
{
private $payMob = null;

public function __construct(Paymob $payMob)
{
$this->payMob = $payMob;
}

/**
* @TODO will be removed!
*/
public function test()
{
$billingData = [
'apartment' => 'NA',
'email' => '[email protected]',
'floor' => 'NA',
'first_name' => 'test',
'street' => 'NA',
'building' => 'NA',
'phone_number' => '01234567890',
'shipping_method' => 'NA',
'postal_code' => 'NA',
'city' => 'NA',
'country' => 'NA',
'last_name' => 'NA',
'state' => 'NA',
];

$data = [
'delivery_needed' => false, // default false
'amount_cents' => 10000, // default 0
'items' => [], //default []
'expiration' => 3600, // default 3600
'merchant_order_id' => '123', // default null
'billing_data' => $billingData, // required
'currency' => 'EGP', // default EGP
];

// $token = 'ZXlKMGVYQWlPaUpLVjFRaUxDSmhiR2NpT2lKSVV6VXhNaUo5LmV5SmpiR0Z6Y3lJNklrMWxjbU5vWVc1MElpd2ljR2hoYzJnaU9pSTBPR0V4WVRJeU1tSmtORGswWkRSallUZGpNVFkzWXprd01qZzROV1U0T1RRM016UXhaamRoWWpBME5HUTBNakE1WkdKbFkySXdNR0kwTlRJeVlqazRJaXdpY0hKdlptbHNaVjl3YXlJNk16Y3lOREV4TENKbGVIQWlPakUyTmpFME56UXpPRGg5LlNoV1ltWkc3UkpQOHdXelQycEkxaUdoVU92S1RCSUZ0M2hIdmU1cU5qeXV0b2JQcHo5ZUZNdHR4Ql9hWERfb0VKX2NLNVU1Um5JU0U4N2x0N2IzWi1B';

$iframeUrl = $this->payMob->makePayment($data);

return redirect()->to($iframeUrl);
}
}
6 changes: 4 additions & 2 deletions src/Controllers/TransactionCallbackController.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,18 +12,20 @@ class TransactionCallbackController extends Controller
/**
* Handle Transaction Callback Processed.
*
* @param Request $request
* @return void
*/
public function processed(Request $request)
public function processed(Request $request): void
{
}

/**
* Handle Transaction Callback Response.
*
* @param Request $request
* @return void
*/
public function response(Request $request)
public function response(Request $request): void
{
}
}
2 changes: 1 addition & 1 deletion src/Facades/Paymob.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ class Paymob extends Facade
*
* @return string
*/
protected static function getFacadeAccessor()
protected static function getFacadeAccessor(): string
{
return 'paymob';
}
Expand Down
105 changes: 15 additions & 90 deletions src/Paymob.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,14 +16,14 @@ class Paymob
*
* @var string
*/
protected $integrationId;
protected mixed $integrationId;

/**
* The Iframe ID.
*
* @var string
*/
protected $iframeId;
protected mixed $iframeId;

/**
* Constructor.
Expand All @@ -34,26 +34,6 @@ public function __construct(string $integrationId = null, string $iframeId = nul
$this->iframeId = $iframeId ?: config('paymob.auth.iframe_id');
}

/**
* Set The Integration ID.
*/
public function setIntegrationId(string $integrationId): self
{
$this->integrationId = $integrationId;

return $this;
}

/**
* Set The Iframe ID.
*/
public function setIframeId(string $iframeId): self
{
$this->iframeId = $iframeId;

return $this;
}

/**
* Paymob Authentication.
*/
Expand Down Expand Up @@ -118,22 +98,25 @@ public function getPaymentKey(string $token, int $amountCents, int $expiration,
}

/**
* Make payment for API (moblie clients).
* Make payment for API (mobile clients).
* Return iframe_url.
*
* @param string $paymentToken
* @param array $data
* @param null $mobileWallet
* @return string
*/
public function makePayment(array $data, $mobileWallet = null): string
{
// step 1 -> Authentication
$authToken = $this->autheticate();
$authToken = $this->authenticate();

// step 2 -> Order Registration
$orderId = $this->registerOrder($authToken, $data);

// step 3 => Get Payment Key
$paymentToken = $this->createPaymentToken($authToken, $orderId, $data);

//@TODO: should be refactored!
if ($mobileWallet) {
$walletResponse = $this->prepareWalletRedirectionUrl($paymentToken, $mobileWallet);

Expand All @@ -144,75 +127,21 @@ public function makePayment(array $data, $mobileWallet = null): string
return $this->buildIframeUrl($paymentToken);
}

/**
* Capture authed order.
*
* @param string $token
* @param int $transactionId
* @param int amount
*/
public function capture($token, $transactionId, $amount): array
{
return [];
}

/**
* Get paymob all orders.
*
* @param string $authToken
* @param string $page
*/
public function getOrders($authToken, $page = 1): Response
{
}

/**
* Get paymob order.
*
* @param string $authToken
* @param int $orderId
*/
public function getOrder($authToken, $orderId): Response
{
}

/**
* Get Paymob all transactions.
*
* @param string $authToken
* @param string $page
*/
public function getTransactions($authToken, $page = 1): Response
{
}

/**
* Get Paymob transaction.
*
* @param string $authToken
* @param int $transactionId
*/
public function getTransaction($authToken, $transactionId): Response
{
}

/**
* authenticate request
* return authToken.
*/
private function autheticate(): string
private function authenticate(): string
{
$authResponse = $this->auth();
$authToken = $authResponse['token'];

return $authToken;
return $authResponse['token'];
}

/**
* register order request
* return orderId.
*/
private function registerOrder(string $authToken, array $data): string
private function registerOrder(string $authToken, array $data): int
{
$deliveryNeeded = $data['delivery_needed'] ?? false;
$amountCents = $data['amount_cents'] ?? 0;
Expand All @@ -228,11 +157,10 @@ private function registerOrder(string $authToken, array $data): string
* create payment token request
* return paymentToken.
*/
private function createPaymentToken(string $authToken, string $orderId, array $data): string
private function createPaymentToken(string $authToken, int $orderId, array $data): string
{
$amountCents = (isset($data['amount_cents']) && $data['amount_cents']) ? $data['amount_cents'] : 0;
$expiration = (isset($data['expiration']) && $data['expiration']) ? $data['expiration'] : 3600;
$merchantOrderId = (isset($data['merchant_order_id']) && $data['merchant_order_id']) ? $data['merchant_order_id'] : null;
$billingData = (isset($data['billing_data']) && $data['billing_data']) ? $data['billing_data'] : [];
$currency = (isset($data['currency']) && $data['currency']) ? $data['currency'] : 'EGP';

Expand All @@ -255,14 +183,11 @@ private function buildIframeUrl(string $paymentToken): string
/**
* Send order to paymob servers.
*
* @param string $token
* @param bool $deliveryNeeded
* @param int $amountCents
* @param array $items
*
* @param string $paymentToken
* @param string|null $mobileWallet
* @return array
*/
public function prepareWalletRedirectionUrl(string $paymentToken, string $mobileWallet = null)
public function prepareWalletRedirectionUrl(string $paymentToken, string $mobileWallet = null): array
{
$json = [
'payment_token' => $paymentToken,
Expand Down

0 comments on commit 3ee4fa3

Please sign in to comment.