Skip to content

Commit

Permalink
[Chore] Update Zarinpal Sandbox URLs and Refactor API Integration (#277)
Browse files Browse the repository at this point in the history
* chore: update zarinpal sandbox urls in config

* refactor: replaced SOAP with REST API in Zarinpal Sandbox strategy

* styles: fix code styles

* styles: fix code styles
  • Loading branch information
mohaphez authored Oct 1, 2024
1 parent 0685fe4 commit fb767e4
Show file tree
Hide file tree
Showing 2 changed files with 68 additions and 32 deletions.
6 changes: 3 additions & 3 deletions config/payment.php
Original file line number Diff line number Diff line change
Expand Up @@ -350,9 +350,9 @@
'apiVerificationUrl' => 'https://api.zarinpal.com/pg/v4/payment/verify.json',

/* sandbox api */
'sandboxApiPurchaseUrl' => 'https://sandbox.zarinpal.com/pg/services/WebGate/wsdl',
'sandboxApiPaymentUrl' => 'https://sandbox.zarinpal.com/pg/StartPay/',
'sandboxApiVerificationUrl' => 'https://sandbox.zarinpal.com/pg/services/WebGate/wsdl',
'sandboxApiPurchaseUrl' => 'https://sandbox.zarinpal.com/pg/v4/payment/request.json',
'sandboxApiPaymentUrl' => 'https://sandbox.zarinpal.com/pg/StartPay/',
'sandboxApiVerificationUrl' => 'https://sandbox.zarinpal.com/pg/v4/payment/verify.json',

/* zarinGate api */
'zaringateApiPurchaseUrl' => 'https://ir.zarinpal.com/pg/services/WebGate/wsdl',
Expand Down
94 changes: 65 additions & 29 deletions src/Drivers/Zarinpal/Strategies/Sandbox.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace Shetabit\Multipay\Drivers\Zarinpal\Strategies;

use GuzzleHttp\Client;
use Shetabit\Multipay\Abstracts\Driver;
use Shetabit\Multipay\Exceptions\InvalidPaymentException;
use Shetabit\Multipay\Exceptions\PurchaseFailedException;
Expand All @@ -10,10 +11,11 @@
use Shetabit\Multipay\Receipt;
use Shetabit\Multipay\RedirectionForm;
use Shetabit\Multipay\Request;
use SoapClient;

class Sandbox extends Driver
{
protected $client;

/**
* Invoice
*
Expand All @@ -39,6 +41,7 @@ public function __construct(Invoice $invoice, $settings)
{
$this->invoice($invoice);
$this->settings = (object) $settings;
$this->client = new Client();
}

/**
Expand All @@ -47,39 +50,48 @@ public function __construct(Invoice $invoice, $settings)
* @return string
*
* @throws PurchaseFailedException
* @throws \SoapFault
*/
public function purchase()
{
$amount = $this->invoice->getAmount() * ($this->settings->currency == 'T' ? 10 : 1); // convert to rial

if (!empty($this->invoice->getDetails()['description'])) {
$description = $this->invoice->getDetails()['description'];
} else {
$description = $this->settings->description;
}

$mobile = !empty($this->invoice->getDetails()['mobile']) ? $this->invoice->getDetails()['mobile'] : '';
$email = !empty($this->invoice->getDetails()['email']) ? $this->invoice->getDetails()['email'] : '';
$amount = $this->invoice->getAmount() / ($this->settings->currency == 'T' ? 1 : 10); // convert to toman

$data = array(
'MerchantID' => $this->settings->merchantId,
'Amount' => $amount,
'CallbackURL' => $this->settings->callbackUrl,
'Description' => $description,
'Mobile' => $mobile ?? '',
'Email' => $email ?? '',
$data = [
"merchant_id" => $this->settings->merchantId,
"amount" => $amount,
"currency" => 'IRR',
"callback_url" => $this->settings->callbackUrl,
"description" => $description,
'AdditionalData' => $this->invoice->getDetails()
);

$client = new SoapClient($this->getPurchaseUrl(), ['encoding' => 'UTF-8']);
$result = $client->PaymentRequest($data);
];

$bodyResponse = $result->Status;
if ($bodyResponse != 100 || empty($result->Authority)) {
$response = $this
->client
->request(
'POST',
$this->getPurchaseUrl(),
[
"json" => $data,
"headers" => [
'Content-Type' => 'application/json',
],
"http_errors" => false,
]
);

$result = json_decode($response->getBody()->getContents(), true);

if (!empty($result['errors']) || empty($result['data']) || $result['data']['code'] != 100) {
$bodyResponse = $result['errors']['code'];
throw new PurchaseFailedException($this->translateStatus($bodyResponse), $bodyResponse);
}

$this->invoice->transactionId($result->Authority);
$this->invoice->transactionId($result['data']["authority"]);

// return the transaction's id
return $this->invoice->getTransactionId();
Expand All @@ -106,26 +118,50 @@ public function pay() : RedirectionForm
* @return ReceiptInterface
*
* @throws InvalidPaymentException
* @throws \SoapFault
*/
public function verify() : ReceiptInterface
{
$authority = $this->invoice->getTransactionId() ?? Request::input('Authority');
$data = [
'MerchantID' => $this->settings->merchantId,
'Authority' => $authority,
'Amount' => $this->invoice->getAmount() / ($this->settings->currency == 'T' ? 1 : 10), // convert to toman
"merchant_id" => $this->settings->merchantId,
"authority" => $authority,
"amount" => $this->invoice->getAmount() * ($this->settings->currency == 'T' ? 10 : 1), // convert to rial
];

$client = new SoapClient($this->getVerificationUrl(), ['encoding' => 'UTF-8']);
$result = $client->PaymentVerification($data);
$response = $this->client->request(
'POST',
$this->getVerificationUrl(),
[
'json' => $data,
"headers" => [
'Content-Type' => 'application/json',
],
"http_errors" => false,
]
);

$result = json_decode($response->getBody()->getContents(), true);

$bodyResponse = $result->Status;
if ($bodyResponse != 100) {
if (empty($result['data']) || !isset($result['data']['ref_id']) || ($result['data']['code'] != 100 && $result['data']['code'] != 101)) {
$bodyResponse = $result['errors']['code'];
throw new InvalidPaymentException($this->translateStatus($bodyResponse), $bodyResponse);
}

return $this->createReceipt($result->RefID);
$refId = $result['data']['ref_id'];

$receipt = $this->createReceipt($refId);
$receipt->detail([
'code' => $result['data']['code'],
'message' => $result['data']['message'] ?? null,
'card_hash' => $result['data']['card_hash'] ?? null,
'card_pan' => $result['data']['card_pan'] ?? null,
'ref_id' => $refId,
'fee_type' => $result['data']['fee_type'] ?? null,
'fee' => $result['data']['fee'] ?? null,
'order_id' => $result['data']['order_id'] ?? null,
]);

return $receipt;
}

/**
Expand Down

0 comments on commit fb767e4

Please sign in to comment.