Skip to content

Commit

Permalink
WIP
Browse files Browse the repository at this point in the history
  • Loading branch information
omalizadeh committed May 19, 2023
1 parent c590513 commit dca0fb2
Show file tree
Hide file tree
Showing 17 changed files with 116 additions and 108 deletions.
10 changes: 5 additions & 5 deletions src/Drivers/Contracts/Driver.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@

namespace Omalizadeh\MultiPayment\Drivers\Contracts;

use Omalizadeh\MultiPayment\GatewayRedirection;
use Omalizadeh\MultiPayment\Invoice;
use Omalizadeh\MultiPayment\Receipt;
use Omalizadeh\MultiPayment\RedirectionForm;

abstract class Driver implements PurchaseInterface
{
Expand All @@ -30,15 +30,15 @@ public function getInvoice(): Invoice

abstract public function purchase(): string;

abstract public function pay(): GatewayRedirection;
abstract public function pay(): RedirectionForm;

abstract public function verify(): Receipt;

abstract protected function getPurchaseData(): array;

abstract protected function getVerificationData(): array;

abstract protected function getStatusMessage($statusCode): string;
abstract protected function getStatusMessage(int|string $statusCode): string;

abstract protected function getSuccessResponseStatusCode();

Expand All @@ -48,8 +48,8 @@ abstract protected function getPaymentUrl(): string;

abstract protected function getVerificationUrl(): string;

protected function redirect($action, array $inputs = [], $method = 'POST'): GatewayRedirection
protected function redirect(string $action, array $inputs = [], string $method = 'POST'): RedirectionForm
{
return new GatewayRedirection($action, $inputs, $method);
return new RedirectionForm($action, $inputs, $method);
}
}
4 changes: 2 additions & 2 deletions src/Drivers/Contracts/PurchaseInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,15 @@

namespace Omalizadeh\MultiPayment\Drivers\Contracts;

use Omalizadeh\MultiPayment\GatewayRedirection;
use Omalizadeh\MultiPayment\Invoice;
use Omalizadeh\MultiPayment\Receipt;
use Omalizadeh\MultiPayment\RedirectionForm;

interface PurchaseInterface
{
public function purchase(): string;

public function pay(): GatewayRedirection;
public function pay(): RedirectionForm;

public function verify(): Receipt;

Expand Down
24 changes: 13 additions & 11 deletions src/Drivers/IDPay/IDPay.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,33 +9,35 @@
use Omalizadeh\MultiPayment\Exceptions\PaymentAlreadyVerifiedException;
use Omalizadeh\MultiPayment\Exceptions\PaymentFailedException;
use Omalizadeh\MultiPayment\Exceptions\PurchaseFailedException;
use Omalizadeh\MultiPayment\GatewayRedirection;
use Omalizadeh\MultiPayment\Receipt;
use Omalizadeh\MultiPayment\RedirectionForm;

class IDPay extends Driver
{
private ?string $link = null;
private ?string $paymentUrl = null;

public function purchase(): string
{
$response = $this->callApi($this->getPurchaseUrl(), $this->getPurchaseData());
$purchaseData = $this->getPurchaseData();

$response = $this->callApi($this->getPurchaseUrl(), $purchaseData);

if (isset($response['error_code'])) {
$message = $response['error_message'] ?? $this->getStatusMessage($response['error_code']);

throw new PurchaseFailedException($message, $response['error_code']);
throw new PurchaseFailedException($message, $response['error_code'], $purchaseData);
}

$this->getInvoice()->setTransactionId($response['id']);

$this->setLink($response['link']);
$this->setPaymentUrl($response['link']);

return $response['id'];
}

public function pay(): GatewayRedirection
public function pay(): RedirectionForm
{
return $this->redirect($this->link, [], 'GET');
return $this->redirect($this->getPaymentUrl(), [], 'GET');
}

public function verify(): Receipt
Expand Down Expand Up @@ -115,7 +117,7 @@ protected function getVerificationData(): array
];
}

protected function getStatusMessage($statusCode): string
protected function getStatusMessage(int|string $statusCode): string
{
$messages = [
1 => 'پرداخت انجام نشده است.',
Expand Down Expand Up @@ -182,7 +184,7 @@ protected function getPurchaseUrl(): string

protected function getPaymentUrl(): string
{
return 'link';
return $this->paymentUrl;
}

protected function getVerificationUrl(): string
Expand Down Expand Up @@ -219,8 +221,8 @@ private function checkPhoneNumberFormat(string $phoneNumber): string
return $phoneNumber;
}

private function setLink(string $link): void
private function setPaymentUrl(string $url): void
{
$this->link = $link;
$this->paymentUrl = $url;
}
}
16 changes: 11 additions & 5 deletions src/Drivers/Mellat/Mellat.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,21 +8,26 @@
use Omalizadeh\MultiPayment\Exceptions\PaymentAlreadyVerifiedException;
use Omalizadeh\MultiPayment\Exceptions\PaymentFailedException;
use Omalizadeh\MultiPayment\Exceptions\PurchaseFailedException;
use Omalizadeh\MultiPayment\GatewayRedirection;
use Omalizadeh\MultiPayment\Receipt;
use Omalizadeh\MultiPayment\RedirectionForm;
use SoapClient;

class Mellat extends Driver
{
public function purchase(): string
{
$soap = new SoapClient($this->getPurchaseUrl(), $this->getSoapOptions());
$response = $soap->bpPayRequest($this->getPurchaseData());
$purchaseData = $this->getPurchaseData();
$response = $soap->bpPayRequest($purchaseData);
$responseData = explode(',', $response->return);
$responseCode = $responseData[0];

if ($responseCode != $this->getSuccessResponseStatusCode()) {
throw new PurchaseFailedException($this->getStatusMessage($responseCode), $responseCode);
throw new PurchaseFailedException(
$this->getStatusMessage($responseCode),
$responseCode,
$purchaseData,
);
}

$hashCode = $responseData[1];
Expand All @@ -32,7 +37,7 @@ public function purchase(): string
return $hashCode;
}

public function pay(): GatewayRedirection
public function pay(): RedirectionForm
{
$mobileNo = $this->getInvoice()->getPhoneNumber();

Expand Down Expand Up @@ -134,7 +139,7 @@ protected function getVerificationData(): array
];
}

protected function getStatusMessage($statusCode): string
protected function getStatusMessage(int|string $statusCode): string
{
$translations = [
'0' => 'تراکنش با موفقیت انجام شد',
Expand Down Expand Up @@ -184,6 +189,7 @@ protected function getStatusMessage($statusCode): string
'62' => 'مسیر بازگشت به سایت در دامنه ثبت شده برای پذیرنده قرار ندارد',
'98' => 'سقف استفاده از رمز ایستا به پایان رسیده است',
];

$unknownError = 'خطای ناشناخته رخ داده است.';

return array_key_exists($statusCode, $translations) ? $translations[$statusCode] : $unknownError;
Expand Down
25 changes: 15 additions & 10 deletions src/Drivers/Novin/Novin.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,15 @@

namespace Omalizadeh\MultiPayment\Drivers\Novin;

use Illuminate\Support\Arr;
use Illuminate\Support\Facades\Http;
use Illuminate\Support\Str;
use Omalizadeh\MultiPayment\Drivers\Contracts\Driver;
use Omalizadeh\MultiPayment\Exceptions\HttpRequestFailedException;
use Omalizadeh\MultiPayment\Exceptions\InvalidConfigurationException;
use Omalizadeh\MultiPayment\Exceptions\PaymentFailedException;
use Omalizadeh\MultiPayment\Exceptions\PurchaseFailedException;
use Omalizadeh\MultiPayment\GatewayRedirection;
use Omalizadeh\MultiPayment\Receipt;
use Omalizadeh\MultiPayment\RedirectionForm;

class Novin extends Driver
{
Expand All @@ -21,12 +20,17 @@ class Novin extends Driver

public function purchase(): string
{
$response = $this->callApi($this->getLoginUrl(), $this->getLoginData());
$loginData = $this->getLoginData();
$response = $this->callApi($this->getLoginUrl(), $loginData);

if ($response['Result'] == $this->getSuccessResponseStatusCode()) {
$this->sessionId = $response['SessionId'];
} else {
throw new PurchaseFailedException($this->getStatusMessage($response['Result']));
throw new PurchaseFailedException(
$this->getStatusMessage($response['Result']),
$response['Result'],
$loginData,
);
}

$purchaseData = $this->getPurchaseData();
Expand Down Expand Up @@ -54,13 +58,14 @@ public function purchase(): string
}
}

throw new PurchaseFailedException($this->getStatusMessage($response['Result']), 0, Arr::except($purchaseData, [
'WSContext',
'TransType',
]));
throw new PurchaseFailedException(
$this->getStatusMessage($response['Result']),
$response['Result'],
$purchaseData,
);
}

public function pay(): GatewayRedirection
public function pay(): RedirectionForm
{
$payUrl = $this->getPaymentUrl();

Expand Down Expand Up @@ -194,7 +199,7 @@ protected function getVerificationData(): array
];
}

protected function getStatusMessage($statusCode): string
protected function getStatusMessage(int|string $statusCode): string
{
$messages = [
'erSucceed' => 'سرویس با موفقیت اجرا شد.',
Expand Down
12 changes: 7 additions & 5 deletions src/Drivers/Parsian/Parsian.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,16 +6,17 @@
use Omalizadeh\MultiPayment\Exceptions\InvalidConfigurationException;
use Omalizadeh\MultiPayment\Exceptions\PaymentFailedException;
use Omalizadeh\MultiPayment\Exceptions\PurchaseFailedException;
use Omalizadeh\MultiPayment\GatewayRedirection;
use Omalizadeh\MultiPayment\Receipt;
use Omalizadeh\MultiPayment\RedirectionForm;
use SoapClient;

class Parsian extends Driver
{
public function purchase(): string
{
$soap = new SoapClient($this->getPurchaseUrl(), $this->getSoapOptions());
$response = $soap->SalePaymentRequest($this->getPurchaseData());
$purchaseData = $this->getPurchaseData();
$response = $soap->SalePaymentRequest($purchaseData);

if ($response->SalePaymentRequestResult && $response->SalePaymentRequestResult->Status === $this->getSuccessResponseStatusCode()) {
$this->getInvoice()->setTransactionId($response->SalePaymentRequestResult->Token);
Expand All @@ -26,10 +27,11 @@ public function purchase(): string
throw new PurchaseFailedException(
$this->getStatusMessage($response->SalePaymentRequestResult->Status),
$response->SalePaymentRequestResult->Status,
$purchaseData,
);
}

public function pay(): GatewayRedirection
public function pay(): RedirectionForm
{
$paymentUrl = $this->getPaymentUrl();

Expand Down Expand Up @@ -86,7 +88,7 @@ protected function getVerificationData(): array
];
}

protected function getStatusMessage($statusCode): string
protected function getStatusMessage(int|string $statusCode): string
{
$messages = [
'-32768' => 'ﺧﻄﺎﻱ ﻧﺎﺷﻨﺎﺧﺘﻪ ﺭﺥ ﺩﺍﺩﻩ ﺍﺳﺖ',
Expand Down Expand Up @@ -385,7 +387,7 @@ protected function getStatusMessage($statusCode): string
'200' => 'ﺳﺎﻳﺮ ﺧﻄﺎﻫﺎﻱ ﻧﮕﺎﺷﺖ ﻧﺸﺪﻩ ﺳﺎﻣﺎﻧﻪ ﻫﺎﻱ ﺑﺎﻧﻜﻲ',
];

return array_key_exists($statusCode, $messages) ? $messages[$statusCode] : $statusCode;
return array_key_exists($statusCode, $messages) ? $messages[$statusCode] : 'خطای تعریف نشده رخ داد.';
}

protected function getSuccessResponseStatusCode(): int
Expand Down
18 changes: 9 additions & 9 deletions src/Drivers/Pasargad/Pasargad.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,34 +9,34 @@
use Omalizadeh\MultiPayment\Exceptions\InvalidConfigurationException;
use Omalizadeh\MultiPayment\Exceptions\PaymentFailedException;
use Omalizadeh\MultiPayment\Exceptions\PurchaseFailedException;
use Omalizadeh\MultiPayment\GatewayRedirection;
use Omalizadeh\MultiPayment\Receipt;
use Omalizadeh\MultiPayment\RedirectionForm;

class Pasargad extends Driver
{
protected const BANK_BUY_ACTION_CODE = 1003;

public function purchase(): string
{
$data = $this->getPurchaseData();
$response = $this->callApi($this->getPurchaseUrl(), $data);
$purchaseData = $this->getPurchaseData();
$response = $this->callApi($this->getPurchaseUrl(), $purchaseData);

if ($response->successful()) {
$result = $response->json();

if ($result['IsSuccess'] != $this->getSuccessResponseStatusCode()) {
throw new PurchaseFailedException($result['Message'], $response->status());
throw new PurchaseFailedException($result['Message'], $response->status(), $purchaseData);
}

$this->getInvoice()->setToken($result['Token']);

return $data['InvoiceNumber'];
return $purchaseData['InvoiceNumber'];
}

throw new PurchaseFailedException($response->body(), $response->status());
throw new PurchaseFailedException($response->body(), $response->status(), $purchaseData);
}

public function pay(): GatewayRedirection
public function pay(): RedirectionForm
{
$payUrl = $this->getPaymentUrl();

Expand Down Expand Up @@ -160,12 +160,12 @@ protected function getCheckTransactionData(): array
];
}

protected function getStatusMessage($statusCode): string
protected function getStatusMessage(int|string $statusCode): string
{
return 'خطا در تبادل اطلاعات با درگاه';
}

protected function getSuccessResponseStatusCode()
protected function getSuccessResponseStatusCode(): bool
{
return true;
}
Expand Down

0 comments on commit dca0fb2

Please sign in to comment.