Skip to content
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
20 changes: 19 additions & 1 deletion config/larapay.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@
| the gateways list is comma separated
|
*/
'gateways' => env('LARAPAY_GATES', 'Mellat,Saman,Pasargad,Parsian,ZarinPal,Payir,Saderat'),
'gateways' => env('LARAPAY_GATES', 'Mellat,Saman,Pasargad,Parsian,ZarinPal,Idpay,Payir,Saderat'),

/*
|--------------------------------------------------------------------------
Expand Down Expand Up @@ -121,6 +121,24 @@
'payir' => [
'api' => env('PAY_IR_API_KEY', ''),
],

/*
|--------------------------------------------------------------------------
| Idpay gateway configuration
|--------------------------------------------------------------------------
|
| types: acceptable values --- normal
|
*/
'idpay' => [
'merchant_id' => env('IDPAY_MERCHANT_ID', ''),
'type' => env('IDPAY_TYPE', 'normal'),
'callback_url' => env('IDPAY_CALLBACK_URL', ''),
'email' => env('IDPAY_EMAIL', ''),
'mobile' => env('IDPAY_MOBILE', '09xxxxxxxxx'),
'description' => env('IDPAY_DESCRIPTION', 'powered-by-Larapay'),
],

/*
|--------------------------------------------------------------------------
| SoapClient Options
Expand Down
201 changes: 201 additions & 0 deletions src/Adapter/Idpay.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,201 @@
<?php
declare(strict_types=1);

namespace Tartan\Larapay\Adapter;

use Tartan\Larapay\Adapter\Idpay\Exception;
use Tartan\Log\Facades\XLog;


/**
* Class Idpay
* @package Tartan\Larapay\Adapter
*/
class Idpay extends AdapterAbstract implements AdapterInterface
{
protected $WSDL = 'https://api.idpay.ir/v1.1/payment';
protected $endPoint = 'https://idpay.ir/p/ws/{order-id}';

protected $testWSDL = 'https://api.idpay.ir/v1.1/payment';
protected $testEndPoint = 'https://idpay.ir/p/ws-sandbox/{order-id}';

public $endPointVerify = 'https://api.idpay.ir/v1.1/payment/verify';

public $reverseSupport = false;

/**
* @return string
* @throws Exception
* @throws \Tartan\Larapay\Adapter\Exception
*/
protected function requestToken(): string
{
if ($this->getTransaction()->checkForRequestToken() == false) {
throw new Exception('larapay::larapay.could_not_request_payment');
}

$this->checkRequiredParameters([
'order_id',
'amount',
'redirect_url',
]);

$sendParams = [
'order_id' => $this->getTransaction()->bank_order_id,
'amount' => intval($this->amount),
'desc' => $this->description ? $this->description : '',
'mail' => $this->email ? $this->email : '',
'phone' => $this->mobile ? $this->mobile : '',
'callback' => $this->redirect_url,
];

$header = [
'Content-Type: application/json',
'X-API-KEY:' .$this->merchant_id,
'X-SANDBOX:' .$this->getSandbox()
];
try {
XLog::debug('PaymentRequest call', $sendParams);
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $this->WSDL);
curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($sendParams));
curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
curl_setopt($ch, CURLOPT_HTTPHEADER, $header);
$response = curl_exec($ch);
$ch_error = curl_error($ch);
curl_close($ch);
$result = json_decode($response);

if (isset($result->error_code)) {
throw new Exception($result->error_code);
}

XLog::info('PaymentRequest response', $this->obj2array($result));
$this->getTransaction()->setGatewayToken(strval($result->id)); // update transaction reference id
return $result->id;
} catch(\Exception $e) {
throw new Exception($e->getMessage());
};
}


/**
* @return string
* @throws Exception
* @throws \Tartan\Larapay\Adapter\Exception
*/
protected function generateForm(): string
{
$authority = $this->requestToken();

$form = view('larapay::idpay-form', [
'endPoint' => strtr($this->getEndPoint(), ['{order-id}' => $authority]),
'submitLabel' => !empty($this->submit_label) ? $this->submit_label : trans("larapay::larapay.goto_gate"),
'autoSubmit' => boolval($this->auto_submit),
]);

return $form->__toString();
}

/**
* @return array
* @throws Exception
* @throws \Tartan\Larapay\Adapter\Exception
*/
public function formParams(): array
{
$authority = $this->requestToken();

return [
'endPoint' => strtr($this->getEndPoint(), ['{authority}' => $authority]),
];
}

public function getSandbox(): string
{
if (config('larapay.mode') == 'production') {
return "0";
} else {
return "1";
}
}

/**
* @return bool
* @throws Exception
* @throws \Tartan\Larapay\Adapter\Exception
*/
protected function verifyTransaction(): bool
{

if ($this->getTransaction()->checkForVerify() == false) {
throw new Exception('larapay::larapay.could_not_verify_payment');
}

$this->checkRequiredParameters([
'merchant_id',
]);

$sendParams = [
'id' => $this->getTransaction()->gate_refid,
'order_id' => $this->getTransaction()->bank_order_id,
];

$header = [
'Content-Type: application/json',
'X-API-KEY:' .$this->merchant_id,
'X-SANDBOX:' .$this->getSandbox()
];

try {
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $this->endPointVerify);
curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($sendParams));
curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
curl_setopt($ch, CURLOPT_HTTPHEADER, $header);
$response = curl_exec($ch);
$ch_error = curl_error($ch);
curl_close($ch);
XLog::debug('PaymentVerification call', $sendParams);
$result = json_decode($response);
XLog::info('PaymentVerification response', $this->obj2array($result));

if (isset($result->status)) {

if ($result->status == 100 || $result->status == 101) {
$this->getTransaction()->setVerified();
$this->getTransaction()->setReferenceId((string)$result->id);
return true;
} else {
throw new Exception($result->status);
}
} else {
throw new Exception($result->error_code);
}

} catch(\Exception $e) {
throw new Exception($e->getMessage());
}
}

/**
* @return bool
*/
public function canContinueWithCallbackParameters(): bool
{
if (!empty($this->transaction['gate_refid'])) {
return true;
}

return false;
}

public function getGatewayReferenceId(): string
{
$this->checkRequiredParameters([
'merchant_id',
]);

return strval($this->transaction['gate_refid']);
}
}
7 changes: 7 additions & 0 deletions src/Adapter/Idpay/Exception.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
<?php

namespace Tartan\Larapay\Adapter\Idpay;

class Exception extends \Tartan\Larapay\Adapter\Exception {
protected $adapter = 'idpay';
}
2 changes: 2 additions & 0 deletions src/Models/Enum/Bank.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,4 +13,6 @@ class Bank
const PAYIR = 'PayIr';
const SADERAT = 'Saderat';
const ZARINPAL = 'Zarinpal';
const IDPAY = 'Idpay';

}
34 changes: 33 additions & 1 deletion translations/en/larapay.php
Original file line number Diff line number Diff line change
Expand Up @@ -133,5 +133,37 @@
'error__42 '=> 'مدت زمان معتبر طول عمر شناسه پرداخت باید بین ۳۰ دقیقه تا ۴۵ روز باشد',
'error__54 '=> 'درخواست مورد نظر آرشیو شده است',
]
]
],
'idpay' => [
'errors' => [
"error_1" => "پرداخت انجام نشده است.",
"error_2" => "پرداخت ناموفق بوده است.",
"error_3" => "خطا رخ داده است.",
"error_4" => "بلوکه شده.",
"error_5" => "برگشت به پرداخت کننده.",
"error_6" => "برگشت خورده سیستمی.",
"error_10" => "در انتظار تایید پرداخت.",
"error_100" => "پرداخت تایید شده است.",
"error_101" => "پرداخت قبلا تایید شده است.",
"error_200" => "به دریافت کننده واریز شد.",
"error_11" => "کاربر مسدود شده است.",
"error_12" => "API Key یافت نشد.",
"error_13" => "درخواست شما از {ip} ارسال شده است. این IP با IP های ثبت شده در وب سرویس همخوانی ندارد.",
"error_14" => "وب سرویس تایید نشده است.",
"error_21" => "حساب بانکی متصل به وب سرویس تایید نشده است.",
"error_31" => "کد تراکنش id نباید خالی باشد.",
"error_32" => "شماره سفارش order_id نباید خالی باشد.",
"error_33" => "مبلغ amount نباید خالی باشد.",
"error_34" => "مبلغ amount باید بیشتر از {min-amount} ریال باشد.",
"error_35" => "مبلغ amount باید کمتر از {max-amount} ریال باشد.",
"error_36" => "مبلغ amount بیشتر از حد مجاز است.",
"error_37" => "آدرس بازگشت callback نباید خالی باشد.",
"error_38" => "درخواست شما از آدرس {domain} ارسال شده است. دامنه آدرس بازگشت callback با آدرس ثبت شده در وب سرویس همخوانی ندارد.",
"error_51" => "تراکنش ایجاد نشد.",
"error_52" => "استعلام نتیجه ای نداشت.",
"error_53" => "تایید پرداخت امکان پذیر نیست.",
"error_54" => "مدت زمان تایید پرداخت سپری شده است.",
]
]

];
34 changes: 33 additions & 1 deletion translations/fa/larapay.php
Original file line number Diff line number Diff line change
Expand Up @@ -133,5 +133,37 @@
'error__42 '=> 'مدت زمان معتبر طول عمر شناسه پرداخت باید بین ۳۰ دقیقه تا ۴۵ روز باشد',
'error__54 '=> 'درخواست مورد نظر آرشیو شده است',
]
]
],
'idpay' => [
'errors' => [
"error_1" => "پرداخت انجام نشده است.",
"error_2" => "پرداخت ناموفق بوده است.",
"error_3" => "خطا رخ داده است.",
"error_4" => "بلوکه شده.",
"error_5" => "برگشت به پرداخت کننده.",
"error_6" => "برگشت خورده سیستمی.",
"error_10" => "در انتظار تایید پرداخت.",
"error_100" => "پرداخت تایید شده است.",
"error_101" => "پرداخت قبلا تایید شده است.",
"error_200" => "به دریافت کننده واریز شد.",
"error_11" => "کاربر مسدود شده است.",
"error_12" => "API Key یافت نشد.",
"error_13" => "درخواست شما از {ip} ارسال شده است. این IP با IP های ثبت شده در وب سرویس همخوانی ندارد.",
"error_14" => "وب سرویس تایید نشده است.",
"error_21" => "حساب بانکی متصل به وب سرویس تایید نشده است.",
"error_31" => "کد تراکنش id نباید خالی باشد.",
"error_32" => "شماره سفارش order_id نباید خالی باشد.",
"error_33" => "مبلغ amount نباید خالی باشد.",
"error_34" => "مبلغ amount باید بیشتر از {min-amount} ریال باشد.",
"error_35" => "مبلغ amount باید کمتر از {max-amount} ریال باشد.",
"error_36" => "مبلغ amount بیشتر از حد مجاز است.",
"error_37" => "آدرس بازگشت callback نباید خالی باشد.",
"error_38" => "درخواست شما از آدرس {domain} ارسال شده است. دامنه آدرس بازگشت callback با آدرس ثبت شده در وب سرویس همخوانی ندارد.",
"error_51" => "تراکنش ایجاد نشد.",
"error_52" => "استعلام نتیجه ای نداشت.",
"error_53" => "تایید پرداخت امکان پذیر نیست.",
"error_54" => "مدت زمان تایید پرداخت سپری شده است.",
]
]

];
14 changes: 14 additions & 0 deletions views/idpay-form.blade.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<form id="goto_idpay_bank" class="form-horizontal goto-bank-form" method="GET" action="{!! $endPoint !!}">
<div class="control-group">
<div class="controls">
<button type="submit" class="btn btn-success">{{$submitLabel}}</button>
</div>
</div>
</form>

@if($autoSubmit === true)
<script type="text/javascript">
var f=document.getElementById('goto_idpay_bank');
f.submit();
</script>
@endif