From e67d95e0ea3f51450c79f03dd6f8665f53633abc Mon Sep 17 00:00:00 2001 From: Omid Date: Sat, 20 Nov 2021 21:55:56 +0330 Subject: [PATCH 1/6] chore: use Arrayable interface for redirection form --- src/RedirectionForm.php | 40 ++++++++++++++++++++-------------------- 1 file changed, 20 insertions(+), 20 deletions(-) diff --git a/src/RedirectionForm.php b/src/RedirectionForm.php index 14efc2c..87d0949 100644 --- a/src/RedirectionForm.php +++ b/src/RedirectionForm.php @@ -2,7 +2,13 @@ namespace Omalizadeh\MultiPayment; -class RedirectionForm +use Illuminate\Contracts\Foundation\Application; +use Illuminate\Contracts\Support\Arrayable; +use Illuminate\Contracts\View\Factory; +use Illuminate\Contracts\View\View; +use Illuminate\Http\JsonResponse; + +class RedirectionForm implements Arrayable { protected string $method; protected array $inputs; @@ -45,11 +51,11 @@ public function getInputs(): array } /** - * Returns a json response where form fields are wrapped in data key. + * Returns a json response with form info wrapped in data key. * - * @return \Illuminate\Http\JsonResponse + * @return JsonResponse */ - public function toJsonResponse(): \Illuminate\Http\JsonResponse + public function toJsonResponse(): JsonResponse { return response()->json([ 'data' => $this->toArray() @@ -57,17 +63,23 @@ public function toJsonResponse(): \Illuminate\Http\JsonResponse } /** + * Returns redirection form data as array. + * * @return array */ public function toArray(): array { - return $this->getData(); + return [ + 'action' => $this->getUrl(), + 'inputs' => $this->getInputs(), + 'method' => $this->getMethod(), + ]; } /** * Renders a view that redirects to payment gateway automatically. * - * @return \Illuminate\Contracts\Foundation\Application|\Illuminate\Contracts\View\Factory|\Illuminate\Contracts\View\View + * @return Application|Factory|View */ public function view() { @@ -75,24 +87,12 @@ public function view() } /** - * @deprecated + * @return Application|Factory|View * - * @return \Illuminate\Contracts\Foundation\Application|\Illuminate\Contracts\View\Factory|\Illuminate\Contracts\View\View + * @deprecated */ public function render() { return view('multipayment::gateway_redirect', $this->toArray()); } - - /** - * @return array - */ - protected function getData(): array - { - return [ - 'action' => $this->getUrl(), - 'inputs' => $this->getInputs(), - 'method' => $this->getMethod(), - ]; - } } From 35b4959a6cdf6f3d6bf48ce1278bdcb0fe3cb30d Mon Sep 17 00:00:00 2001 From: Ali Kesht Kar Date: Fri, 26 Nov 2021 12:01:36 +0330 Subject: [PATCH 2/6] add callbackUrl setter and getter to invoice class --- src/Invoice.php | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) diff --git a/src/Invoice.php b/src/Invoice.php index 02ce840..91f47b3 100644 --- a/src/Invoice.php +++ b/src/Invoice.php @@ -16,6 +16,7 @@ class Invoice protected ?string $description = null; protected ?string $phoneNumber = null; protected ?string $transactionId = null; + protected ?string $callbackUrl = null; /** * @param float $amount @@ -127,6 +128,24 @@ public function setInvoiceId(string $invoiceId): Invoice return $this; } + /** + * @param string $callbackUrl + * @return $this + */ + public function setCallbackUrl(string $callbackUrl): Self + { + $this->callbackUrl = $callbackUrl; + return $this; + } + + /** + * @return ?string + */ + public function getCallbackUrl(): ?string + { + return $this->callbackUrl; + } + /** * @return float */ From 3231442bbd55976155caea5a34d59ea3860491ad Mon Sep 17 00:00:00 2001 From: Ali Kesht Kar Date: Fri, 26 Nov 2021 12:06:37 +0330 Subject: [PATCH 3/6] add custom callbackUrl support to available drivers --- src/Drivers/IDPay/IDPay.php | 2 +- src/Drivers/Mellat/Mellat.php | 2 +- src/Drivers/Novin/Novin.php | 2 +- src/Drivers/Pasargad/Pasargad.php | 2 +- src/Drivers/PayIr/PayIr.php | 2 +- src/Drivers/Saman/Saman.php | 2 +- src/Drivers/Zarinpal/Zarinpal.php | 2 +- src/Drivers/Zibal/Zibal.php | 2 +- 8 files changed, 8 insertions(+), 8 deletions(-) diff --git a/src/Drivers/IDPay/IDPay.php b/src/Drivers/IDPay/IDPay.php index f2a50ef..03a9b76 100644 --- a/src/Drivers/IDPay/IDPay.php +++ b/src/Drivers/IDPay/IDPay.php @@ -101,7 +101,7 @@ protected function getPurchaseData(): array 'phone' => $mobile, 'mail' => $email, 'desc' => $description, - 'callback' => $this->settings['callback_url'] + 'callback' => $this->getInvoice()->getCallbackUrl() ?: $this->settings['callback_url'] ]; } diff --git a/src/Drivers/Mellat/Mellat.php b/src/Drivers/Mellat/Mellat.php index a5e670f..2b6f656 100644 --- a/src/Drivers/Mellat/Mellat.php +++ b/src/Drivers/Mellat/Mellat.php @@ -96,7 +96,7 @@ protected function getPurchaseData(): array 'terminalId' => $this->settings['terminal_id'], 'userName' => $this->settings['username'], 'userPassword' => $this->settings['password'], - 'callBackUrl' => $this->settings['callback_url'], + 'callBackUrl' => $this->getInvoice()->getCallbackUrl() ?: $this->settings['callback_url'], 'amount' => $this->getInvoice()->getAmount(), 'localDate' => now()->format('Ymd'), 'localTime' => now()->format('Gis'), diff --git a/src/Drivers/Novin/Novin.php b/src/Drivers/Novin/Novin.php index d4626f9..a70b4db 100644 --- a/src/Drivers/Novin/Novin.php +++ b/src/Drivers/Novin/Novin.php @@ -162,7 +162,7 @@ protected function getPurchaseData(): array 'TransType' => static::BANK_BUY_TRANSACTION_TYPE, 'ReserveNum' => $this->getInvoice()->getInvoiceId(), 'Amount' => $this->getInvoice()->getAmount(), - 'RedirectUrl' => $this->settings['callback_url'], + 'RedirectUrl' => $this->getInvoice()->getCallbackUrl() ?: $this->settings['callback_url'], 'MobileNo' => $phoneNumber, 'Email' => $this->getInvoice()->getEmail(), 'UserId' => $this->getInvoice()->getUserId(), diff --git a/src/Drivers/Pasargad/Pasargad.php b/src/Drivers/Pasargad/Pasargad.php index 6d7be1d..fc8eaa4 100644 --- a/src/Drivers/Pasargad/Pasargad.php +++ b/src/Drivers/Pasargad/Pasargad.php @@ -116,7 +116,7 @@ protected function getPurchaseData(): array 'Action' => static::BANK_BUY_ACTION_CODE, 'MerchantCode' => $this->settings['merchant_code'], 'TerminalCode' => $this->settings['terminal_code'], - 'RedirectAddress' => $this->settings['callback_url'], + 'RedirectAddress' => $this->getInvoice()->getCallbackUrl() ?: $this->settings['callback_url'], 'Amount' => $this->getInvoice()->getAmount(), 'Mobile' => $mobile, 'Email' => $this->getInvoice()->getEmail(), diff --git a/src/Drivers/PayIr/PayIr.php b/src/Drivers/PayIr/PayIr.php index 71fe015..36e1c8d 100644 --- a/src/Drivers/PayIr/PayIr.php +++ b/src/Drivers/PayIr/PayIr.php @@ -80,7 +80,7 @@ protected function getPurchaseData(): array return [ 'api' => $this->settings['api_key'], 'amount' => $this->getInvoice()->getAmount(), - 'redirect' => $this->settings['callback_url'], + 'redirect' => $this->getInvoice()->getCallbackUrl() ?: $this->settings['callback_url'], 'mobile' => $mobile, 'factorNumber' => $this->getInvoice()->getInvoiceId(), 'description' => $description, diff --git a/src/Drivers/Saman/Saman.php b/src/Drivers/Saman/Saman.php index ca27add..a6f948a 100644 --- a/src/Drivers/Saman/Saman.php +++ b/src/Drivers/Saman/Saman.php @@ -75,7 +75,7 @@ protected function getPurchaseData(): array 'Action' => 'Token', 'TerminalId' => $this->settings['terminal_id'], 'Amount' => $this->getInvoice()->getAmount(), - 'RedirectUrl' => $this->settings['callback_url'], + 'RedirectUrl' => $this->getInvoice()->getCallbackUrl() ?: $this->settings['callback_url'], 'CellNumber' => $cellNumber, 'ResNum' => $this->getInvoice()->getInvoiceId(), ]; diff --git a/src/Drivers/Zarinpal/Zarinpal.php b/src/Drivers/Zarinpal/Zarinpal.php index f9c5045..db53b3a 100644 --- a/src/Drivers/Zarinpal/Zarinpal.php +++ b/src/Drivers/Zarinpal/Zarinpal.php @@ -118,7 +118,7 @@ protected function getPurchaseData(): array return [ 'merchant_id' => $this->settings['merchant_id'], 'amount' => $this->getInvoice()->getAmount(), - 'callback_url' => $this->settings['callback_url'], + 'callback_url' => $this->getInvoice()->getCallbackUrl() ?: $this->settings['callback_url'], 'description' => $description, 'meta_data' => [ 'mobile' => $mobile, diff --git a/src/Drivers/Zibal/Zibal.php b/src/Drivers/Zibal/Zibal.php index 0518cb8..7e1868e 100644 --- a/src/Drivers/Zibal/Zibal.php +++ b/src/Drivers/Zibal/Zibal.php @@ -92,7 +92,7 @@ protected function getPurchaseData(): array return [ 'merchant' => $this->settings['merchant'], 'amount' => $this->getInvoice()->getAmount(), - 'callbackUrl' => $this->settings['callback_url'], + 'callbackUrl' => $this->getInvoice()->getCallbackUrl() ?: $this->settings['callback_url'], 'description' => $description, 'orderId' => $this->getInvoice()->getInvoiceId(), 'mobile' => $mobile, From 8eecfbb530c74bd57e79448a6fc84797db2ae8e2 Mon Sep 17 00:00:00 2001 From: Omid Date: Sat, 27 Nov 2021 19:33:18 +0330 Subject: [PATCH 4/6] chore: add responsable interface to form --- src/RedirectionForm.php | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/src/RedirectionForm.php b/src/RedirectionForm.php index 87d0949..a08d53c 100644 --- a/src/RedirectionForm.php +++ b/src/RedirectionForm.php @@ -4,11 +4,12 @@ use Illuminate\Contracts\Foundation\Application; use Illuminate\Contracts\Support\Arrayable; +use Illuminate\Contracts\Support\Responsable; use Illuminate\Contracts\View\Factory; use Illuminate\Contracts\View\View; use Illuminate\Http\JsonResponse; -class RedirectionForm implements Arrayable +class RedirectionForm implements Arrayable, Responsable { protected string $method; protected array $inputs; @@ -86,6 +87,11 @@ public function view() return view('multipayment::gateway_redirect', $this->toArray()); } + public function toResponse($request): JsonResponse + { + return $this->toJsonResponse(); + } + /** * @return Application|Factory|View * From 36d3aecc284960b923cd03f0b8e0c738ff127556 Mon Sep 17 00:00:00 2001 From: Omid Date: Sat, 27 Nov 2021 19:40:01 +0330 Subject: [PATCH 5/6] refactor: import exception --- src/Invoice.php | 33 +++++++++++++++++++++------------ src/Receipt.php | 4 +++- 2 files changed, 24 insertions(+), 13 deletions(-) diff --git a/src/Invoice.php b/src/Invoice.php index 91f47b3..2550053 100644 --- a/src/Invoice.php +++ b/src/Invoice.php @@ -2,6 +2,7 @@ namespace Omalizadeh\MultiPayment; +use Exception; use Ramsey\Uuid\Uuid; class Invoice @@ -36,7 +37,7 @@ public function __construct(float $amount, ?string $transactionId = null) * @param $amount * @return $this */ - public function setAmount($amount): Invoice + public function setAmount($amount): self { if (config('multipayment.convert_to_rials')) { $this->amount = $amount * 10; @@ -51,9 +52,10 @@ public function setAmount($amount): Invoice * @param string $id * @return $this */ - public function setTransactionId(string $id): Invoice + public function setTransactionId(string $id): self { $this->transactionId = $id; + return $this; } @@ -61,9 +63,10 @@ public function setTransactionId(string $id): Invoice * @param string $token * @return $this */ - public function setToken(string $token): Invoice + public function setToken(string $token): self { $this->token = $token; + return $this; } @@ -71,9 +74,10 @@ public function setToken(string $token): Invoice * @param string $description * @return $this */ - public function setDescription(string $description): Invoice + public function setDescription(string $description): self { $this->description = $description; + return $this; } @@ -81,9 +85,10 @@ public function setDescription(string $description): Invoice * @param string $phone * @return $this */ - public function setPhoneNumber(string $phone): Invoice + public function setPhoneNumber(string $phone): self { $this->phoneNumber = $phone; + return $this; } @@ -91,9 +96,10 @@ public function setPhoneNumber(string $phone): Invoice * @param string $email * @return $this */ - public function setEmail(string $email): Invoice + public function setEmail(string $email): self { $this->email = $email; + return $this; } @@ -101,7 +107,7 @@ public function setEmail(string $email): Invoice * @param string $name * @return $this */ - public function setUserName(string $name): Invoice + public function setUserName(string $name): self { $this->userName = $name; @@ -112,9 +118,10 @@ public function setUserName(string $name): Invoice * @param int $userId * @return $this */ - public function setUserId(int $userId): Invoice + public function setUserId(int $userId): self { $this->userId = $userId; + return $this; } @@ -122,9 +129,10 @@ public function setUserId(int $userId): Invoice * @param string $invoiceId * @return $this */ - public function setInvoiceId(string $invoiceId): Invoice + public function setInvoiceId(string $invoiceId): self { $this->invoiceId = $invoiceId; + return $this; } @@ -132,14 +140,15 @@ public function setInvoiceId(string $invoiceId): Invoice * @param string $callbackUrl * @return $this */ - public function setCallbackUrl(string $callbackUrl): Self + public function setCallbackUrl(string $callbackUrl): self { $this->callbackUrl = $callbackUrl; + return $this; } /** - * @return ?string + * @return string|null */ public function getCallbackUrl(): ?string { @@ -228,7 +237,7 @@ public function getUserId(): ?int /** * @return string - * @throws \Exception + * @throws Exception */ public function getInvoiceId(): string { diff --git a/src/Receipt.php b/src/Receipt.php index afa6383..680544d 100644 --- a/src/Receipt.php +++ b/src/Receipt.php @@ -2,6 +2,8 @@ namespace Omalizadeh\MultiPayment; +use Exception; + class Receipt { protected Invoice $invoice; @@ -30,7 +32,7 @@ public function __construct( /** * @return string - * @throws \Exception + * @throws Exception */ public function getInvoiceId(): string { From 056ed6701e83ea559fba3ad37ed74f601e205374 Mon Sep 17 00:00:00 2001 From: Omid Date: Sat, 27 Nov 2021 19:51:07 +0330 Subject: [PATCH 6/6] fix: replace general exception in purchase --- src/Gateway.php | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/src/Gateway.php b/src/Gateway.php index 210798a..8a8dd50 100644 --- a/src/Gateway.php +++ b/src/Gateway.php @@ -4,12 +4,12 @@ use Closure; use Exception; -use ReflectionClass; +use Omalizadeh\MultiPayment\Drivers\Contracts\PurchaseInterface; +use Omalizadeh\MultiPayment\Drivers\Contracts\UnverifiedPaymentsInterface; use Omalizadeh\MultiPayment\Exceptions\ConfigurationNotFoundException; use Omalizadeh\MultiPayment\Exceptions\DriverNotFoundException; use Omalizadeh\MultiPayment\Exceptions\InvalidConfigurationException; -use Omalizadeh\MultiPayment\Drivers\Contracts\PurchaseInterface; -use Omalizadeh\MultiPayment\Drivers\Contracts\UnverifiedPaymentsInterface; +use ReflectionClass; class Gateway { @@ -22,7 +22,7 @@ class Gateway * @param Invoice $invoice * @param Closure|null $callback * @return RedirectionForm - * @throws Exception + * @throws DriverNotFoundException */ public function purchase(Invoice $invoice, ?Closure $callback = null): RedirectionForm { @@ -175,7 +175,7 @@ private function validateDriverInterfaceImplementation(string $interfaceName): v $reflect = new ReflectionClass($this->getDriver()); if (!$reflect->implementsInterface($interfaceName)) { - throw new Exception("Driver does not implement $interfaceName."); + throw new DriverNotFoundException("Driver does not implement $interfaceName."); } } }