Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Chore] Update Zarinpal Sandbox URLs and Refactor API Integration #277

Merged
merged 4 commits into from
Oct 1, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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