Skip to content

Fix unwanted UUID change #238

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

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
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
9 changes: 4 additions & 5 deletions src/Drivers/Asanpardakht/Asanpardakht.php
Original file line number Diff line number Diff line change
Expand Up @@ -70,8 +70,6 @@ public function __construct(Invoice $invoice, $settings)
*/
public function purchase()
{
$this->invoice->uuid(crc32($this->invoice->getUuid()));

$result = $this->token();

if (!isset($result['status_code']) or $result['status_code'] != 200) {
Expand Down Expand Up @@ -190,16 +188,17 @@ protected function createReceipt($referenceId): Receipt
*/
public function token(): array
{
$this->invoice->convertUuidToNumeric();
if (strpos($this->settings->callbackUrl, '?') !== false) {
$query = '&' . http_build_query(['invoice' => $this->invoice->getUuid()]);
$query = '&' . http_build_query(['invoice' => $this->invoice->getNumericUuid()]);
} else {
$query = '?' . http_build_query(['invoice' => $this->invoice->getUuid()]);
$query = '?' . http_build_query(['invoice' => $this->invoice->getNumericUuid()]);
}

return $this->callApi('POST', self::TokenURL, [
'serviceTypeId' => 1,
'merchantConfigurationId' => $this->settings->merchantConfigID,
'localInvoiceId' => $this->invoice->getUuid(),
'localInvoiceId' => $this->invoice->getNumericUuid(),
'amountInRials' => $this->invoice->getAmount() * ($this->settings->currency == 'T' ? 10 : 1), // convert to rial
'localDate' => $this->getTime()['content'],
'callbackURL' => $this->settings->callbackUrl . $query,
Expand Down
2 changes: 1 addition & 1 deletion src/Drivers/Behpardakht/Behpardakht.php
Original file line number Diff line number Diff line change
Expand Up @@ -239,7 +239,7 @@ protected function preparePurchaseData()
'amount' => $this->invoice->getAmount() * ($this->settings->currency == 'T' ? 10 : 1), // convert to rial
'localDate' => Carbon::now()->format('Ymd'),
'localTime' => Carbon::now()->format('Gis'),
'orderId' => crc32($this->invoice->getUuid()),
'orderId' => $this->invoice->getNumericUuid(),
'additionalData' => $description,
'payerId' => $payerId
);
Expand Down
4 changes: 1 addition & 3 deletions src/Drivers/Etebarino/Etebarino.php
Original file line number Diff line number Diff line change
Expand Up @@ -48,8 +48,6 @@ public function __construct(Invoice $invoice, $settings)
*/
public function purchase()
{
$this->invoice->uuid(crc32($this->invoice->getUuid()));

$result = $this->token();

if (!isset($result['status_code']) or $result['status_code'] != 200) {
Expand Down Expand Up @@ -149,7 +147,7 @@ public function token(): array
'terminalUser' => $this->settings->username,
'merchantCode' => $this->settings->merchantId,
'terminalPass' => $this->settings->password,
'merchantRefCode' => $this->invoice->getUuid(),
'merchantRefCode' => $this->invoice->getNumericUuid(),
"description" => $this->invoice->getDetail('description'),
"returnUrl" => $this->settings->callbackUrl,
'paymentItems' => $this->getItems(),
Expand Down
4 changes: 2 additions & 2 deletions src/Drivers/Fanavacard/Fanavacard.php
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ public function __construct(Invoice $invoice, $settings)
*/
public function purchase()
{
$this->invoice->uuid(crc32($this->invoice->getUuid()));
$this->invoice->uuid($this->invoice->getNumericUuid());
$token = $this->getToken();
$this->invoice->transactionId($token['Token']);

Expand Down Expand Up @@ -158,7 +158,7 @@ public function getToken(): array
'json'=>[
'WSContext'=> $this->getWsContext(),
'TransType'=>'EN_GOODS',
'ReserveNum'=>$this->invoice->getDetail('invoice_number') ?? crc32($this->invoice->getUuid()),
'ReserveNum'=>$this->invoice->getDetail('invoice_number') ?? $this->invoice->getNumericUuid(),
'Amount'=> $this->invoice->getAmount() * ($this->settings->currency == 'T' ? 10 : 1), // convert to rial
'RedirectUrl'=>$this->settings->callbackUrl,
]]);
Expand Down
3 changes: 2 additions & 1 deletion src/Drivers/Nextpay/Nextpay.php
Original file line number Diff line number Diff line change
Expand Up @@ -59,9 +59,10 @@ public function __construct(Invoice $invoice, $settings)
*/
public function purchase()
{
$this->invoice->uuid(intval(1, time()) . $this->invoice->getNumericUuid());
$data = [
'api_key' => $this->settings->merchantId,
'order_id' => intval(1, time()) . crc32($this->invoice->getUuid()),
'order_id' => $this->invoice->getUuid(),
'amount' => $this->invoice->getAmount() / ($this->settings->currency == 'T' ? 1 : 10), // convert to toman
'callback_uri' => $this->settings->callbackUrl,
];
Expand Down
2 changes: 1 addition & 1 deletion src/Drivers/Parsian/Parsian.php
Original file line number Diff line number Diff line change
Expand Up @@ -178,7 +178,7 @@ protected function preparePurchaseData()
return [
'LoginAccount' => $this->settings->merchantId,
'Amount' => $this->invoice->getAmount() * ($this->settings->currency == 'T' ? 10 : 1), // convert to rial
'OrderId' => crc32($this->invoice->getUuid()),
'OrderId' => $this->invoice->getNumericUuid(),
'CallBackUrl' => $this->settings->callbackUrl,
'Originator' => $phone,
'AdditionalData' => $description,
Expand Down
3 changes: 2 additions & 1 deletion src/Drivers/Pasargad/Pasargad.php
Original file line number Diff line number Diff line change
Expand Up @@ -194,12 +194,13 @@ protected function getPreparedInvoiceData()
*/
protected function prepareInvoiceData(): array
{
$this->invoice->uuid($this->invoice->getNumericUuid() . rand(0, time()));
$action = 1003; // 1003 : for buy request (bank standard)
$merchantCode = $this->settings->merchantId;
$terminalCode = $this->settings->terminalCode;
$amount = $this->invoice->getAmount() * ($this->settings->currency == 'T' ? 10 : 1); // convert to rial
$redirectAddress = $this->settings->callbackUrl;
$invoiceNumber = crc32($this->invoice->getUuid()) . rand(0, time());
$invoiceNumber = $this->invoice->getNumericUuid();

$iranTime = new DateTime('now', new DateTimeZone('Asia/Tehran'));
$timeStamp = $iranTime->format("Y/m/d H:i:s");
Expand Down
2 changes: 1 addition & 1 deletion src/Drivers/Sadad/Sadad.php
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ public function __construct(Invoice $invoice, $settings)
public function purchase()
{
$terminalId = $this->settings->terminalId;
$orderId = crc32($this->invoice->getUuid());
$orderId = $this->invoice->getNumericUuid();
$amount = $this->invoice->getAmount() * ($this->settings->currency == 'T' ? 10 : 1); // convert to rial
$key = $this->settings->key;

Expand Down
3 changes: 2 additions & 1 deletion src/Drivers/Yekpay/Yekpay.php
Original file line number Diff line number Diff line change
Expand Up @@ -71,11 +71,12 @@ public function purchase()
} else {
$description = $this->settings->description;
}
$this->invoice->uuid(intval(1, time()) . $this->invoice->getNumericUuid());

$data->merchantId = $this->settings->merchantId;
$data->amount = $this->invoice->getAmount();
$data->callback = $this->settings->callbackUrl;
$data->orderNumber = intval(1, time()).crc32($this->invoice->getUuid());
$data->orderNumber = $this->invoice->getUuid();

$data->fromCurrencyCode = (int) $this->settings->fromCurrencyCode;
$data->toCurrencyCode = (int) $this->settings->toCurrencyCode;
Expand Down
3 changes: 2 additions & 1 deletion src/Drivers/Zibal/Zibal.php
Original file line number Diff line number Diff line change
Expand Up @@ -58,11 +58,12 @@ public function __construct(Invoice $invoice, $settings)
*/
public function purchase()
{
$this->invoice->uuid($this->invoice->getNumericUuid() . time());
$details = $this->invoice->getDetails();

$amount = $this->invoice->getAmount() * ($this->settings->currency == 'T' ? 10 : 1); // convert to rial

$orderId = crc32($this->invoice->getUuid()).time();
$orderId = $this->invoice->getNumericUuid();
if (!empty($details['orderId'])) {
$orderId = $details['orderId'];
} elseif (!empty($details['order_id'])) {
Expand Down
45 changes: 39 additions & 6 deletions src/Invoice.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,8 @@ class Invoice
*/
protected $driver;

protected $uuidIsProvidedByUser = false;

/**
* Invoice constructor.
*
Expand All @@ -46,19 +48,21 @@ public function __construct()
}

/**
* Set invoice uuid
* Set invoice uuid. this method wouldn't allow to modify uuid if it's provided by user.
*
* @param $uuid|null
*
* @throws \Exception
* @return self
*/
public function uuid($uuid = null)
{
if (empty($uuid)) {
$uuid = Uuid::uuid4()->toString();
if (empty($this->uuid)) {
$this->uuidIsProvidedByUser = true;
$this->uuid = $uuid;
} elseif (!$this->uuidIsProvidedByUser) {
$this->uuid = $uuid;
}

$this->uuid = $uuid;
return $this;
}

/**
Expand All @@ -68,9 +72,38 @@ public function uuid($uuid = null)
*/
public function getUuid()
{
if (empty($this->uuid)) {
$this->uuid = Uuid::uuid4()->toString();
}
return $this->uuid;
}

/**
* Convert invoice uuid to numeric when needed and get it.
*
* @return string
*/
public function getNumericUuid()
{
if (is_numeric($this->uuid)) {
return $this->uuid;
}

$this->convertUuidToNumeric();

return $this->uuid;
}


/**
* Convert uuid value to numeric if its not provided by user.
* @return self
*/
public function convertUuidToNumeric()
{
return $this->uuid(crc32($this->getUuid()));
}

/**
* Set the amount of invoice
*
Expand Down