Skip to content

Commit

Permalink
Merge pull request #23 from dena-a/development
Browse files Browse the repository at this point in the history
Development
  • Loading branch information
dena-a authored Jun 2, 2023
2 parents e0e557a + 9497317 commit 8f1f85d
Show file tree
Hide file tree
Showing 13 changed files with 193 additions and 84 deletions.
6 changes: 3 additions & 3 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -38,9 +38,9 @@
"ext-curl": "*",
"ext-json": "*",
"ext-soap": "*",
"illuminate/support": "^5.7|^6.0|^7.0|^8.0|^9.0",
"illuminate/database": "^5.7|^6.0|^7.0|^8.0|^9.0",
"illuminate/http": "^5.7|^6.0|^7.0|^8.0|^9.0",
"illuminate/support": "^5.7|^6.0|^7.0|^8.0|^9.0|^10.0",
"illuminate/database": "^5.7|^6.0|^7.0|^8.0|^9.0|^10.0",
"illuminate/http": "^5.7|^6.0|^7.0|^8.0|^9.0|^10.0",
"nesbot/carbon": "*"
},
"require-dev": {
Expand Down
11 changes: 11 additions & 0 deletions config/iranpayment.php
Original file line number Diff line number Diff line change
Expand Up @@ -103,4 +103,15 @@
'merchant-id' => env('NOVINOPAY_MERCHANT_ID', 'xxxxxxxxxxxxxxxx-xxxx-xxxx-xxxx-xxxx'),
// 'callback-url' => 'http://example.com/payments/navinopay/callback',
],

/*
|--------------------------------------------------------------------------
| TEST gateway
|--------------------------------------------------------------------------
*/
'test' => [
'active' => env('IRANPAYMENT_TEST_ACTIVE', false),
'url' => '/iranpayments/test',
// 'callback-url' => 'http://example.com/payments/test/callback',
],
];
29 changes: 29 additions & 0 deletions resources/views/pages/status.blade.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
@extends('iranpayment::layouts.master')

@php
if ($status === \Dena\IranPayment\Models\IranPaymentTransaction::T_SUCCEED) {
$title = 'پرداخت با موفقیت انجام شد';
} else {
$title = 'پرداخت با خطا مواجه شد';
}
@endphp

@section('title', $title)

@section('content')
<div class="container">
<div class="content">
<h1>{{ $title }}</h1>

<h2>شناسه خرید شما</h2>
<h2 class="content-head">{{ $transaction_code }}</h2>
@if (isset($image))
<div class="l-box-lrg is-center pure-u-1 pure-u-md-1-2 pure-u-lg-2-5">
<img class="pure-img-responsive" src="{{ $image }}">
</div>
@endif

<a class="pure-button pure-button-default" href="{{ $button_url }}">{{ $button_text }}</a>
</div>
</div>
@endsection
25 changes: 14 additions & 11 deletions resources/views/pages/test.blade.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,17 +5,20 @@
@section('content')
<div class="container">
<div class="content">
<h1>شناسه خرید شما</h1>
<h1 class="content-head">{!! $transaction_code !!}</h1>
<h1>شناسه پرداخت شما</h1>
<h1 class="content-head">{!! $reference_number ?? '' !!}</h1>
<h2>پرداخت با موفقیت انجام شد</h2>
<h3><a href="{{route('iranpayment.test.verify', $transaction_code)}}">تایید پرداخت</a></h3>
<h2>شناسه خرید شما</h2>
<h2 class="content-head">{{ $transaction_code }}</h2>

<h1>وضعیت مورد نظر را انتخاب کنید:</h1>
<form method="POST" action="{{ $callback_url }}" class="pure-form pure-form-aligned">
<fieldset>
<div class="pure-control-group">
<button class="pure-button button-success" type="submit" name="status" value="success">پرداخت موفق</button>
</div>
<div class="pure-control-group">
<button class="pure-button button-error" type="submit" name="status" value="error">پرداخت ناموفق</button>
</div>
</fieldset>
</form>
</div>
</div>
@endsection

@section('javascript')
<script type="text/javascript">
</script>
@endsection
24 changes: 20 additions & 4 deletions resources/views/styles/style.blade.php
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,6 @@
color: #455a64;
border: 3px solid #455a64;
padding: 1em 1.6em;
margin: auto;
border-radius: 5px;
letter-spacing: 0.1em;
line-height: 1.7em;
Expand All @@ -106,12 +105,29 @@
border-radius: 5px;
}
button.pure-button-default {
button.pure-button-default,
a.pure-button-default {
color: white;
background: #455a64;
font-size: 150%;
font-size: 150%;
}
.button-success,
.button-error,
.button-warning,
.button-secondary {
color: white;
border-radius: 4px;
text-shadow: 0 1px 1px rgba(0, 0, 0, 0.2);
}
.button-success {
background: rgb(28, 184, 65);
}
.button-error {
background: rgb(202, 60, 60);
}
footer {
background-color: #263238;
Expand Down Expand Up @@ -180,4 +196,4 @@
}
}
</style>
</style>
26 changes: 18 additions & 8 deletions src/Gateways/AbstractGateway.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,7 @@

use Dena\IranPayment\Helpers\Currency;

/**
* @method getName()
* @method purchase()
* @method purchaseUri()
* @method verify()
*/
abstract class AbstractGateway
abstract class AbstractGateway implements GatewayInterface
{
use UserData,
PaymentData,
Expand All @@ -44,6 +38,9 @@ abstract class AbstractGateway
*/
protected array $gateway_request_options = [];

abstract public function verify(): void;
abstract public function purchase(): void;

/**
* Initialize Gateway function
*
Expand Down Expand Up @@ -299,7 +296,7 @@ protected function postVerify(): void
*/
public function confirm(IranPaymentTransaction $transaction = null)
{
if(isset($transaction)) {
if (isset($transaction)) {
$this->setTransaction($transaction);
}

Expand All @@ -321,4 +318,17 @@ public function confirm(IranPaymentTransaction $transaction = null)

return $this;
}

public function statusView(array $parameters = [])
{
return response()->view('iranpayment::pages.status', array_merge(
[
'status' => $parameters['status'] ?? $this->getTransaction()->status,
'transaction_code' => $parameters['transaction_code'] ?? $this->getTransactionCode(),
'button_url' => url('/'),
'button_text' => 'بازگشت به صفحه‌اصلی',
],
$parameters
));
}
}
2 changes: 1 addition & 1 deletion src/Gateways/Saman/Saman.php
Original file line number Diff line number Diff line change
Expand Up @@ -226,7 +226,7 @@ protected function purchaseViewParams(): array
'method' => 'POST',
'form_data' => [
'Token' => $this->getToken(),
'RedirectURL' => $this->getCallbackUrl(),
'RedirectURL' => $this->preparedCallbackUrl(),
],
];
}
Expand Down
17 changes: 17 additions & 0 deletions src/Gateways/Test/TestException.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<?php

namespace Dena\IranPayment\Gateways\Test;

use Dena\IranPayment\Exceptions\GatewayException;

class TestException extends GatewayException
{
public static array $errors = [
-100 => 'تراکنش ناموفق میباشد',
];

public static function error($error_code)
{
return new self(self::$errors[$error_code] ?? self::$errors[-100], $error_code);
}
}
85 changes: 58 additions & 27 deletions src/Gateways/Test/TestGateway.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@

class TestGateway extends AbstractGateway implements GatewayInterface
{
protected ?string $url;

public function getName(): string
{
return 'test';
Expand All @@ -26,58 +28,87 @@ public function initialize(array $parameters = []): self
?? app('config')->get('iranpayment.callback-url')
);

$this->url = $parameters['url'] ?? app('config')->get('iranpayment.test.url');

return $this;
}

protected function prePurchase(): void
{
parent::prePurchase();

if ($this->preparedAmount() < 1000 || $this->preparedAmount() > 500000000) {
if ($this->preparedAmount() < 0 || $this->preparedAmount() > 500000000) {
throw InvalidDataException::invalidAmount();
}
}

public function preVerify(): void
{
parent::preVerify();
}

public function verify(): void
public function purchase(): void
{
$code = rand(1, 10000);
$this->transactionSucceed(['tracking_code' => $code]);
$this->transactionUpdate([
'reference_number' => uniqid(),
]);
}

/**
* @throws Exception
* Purchase View Params function
*
* @return array
*/
public function redirect()
protected function purchaseViewParams(): array
{
$this->addExtra($this->getCallbackUrl(), 'callback_url');
return view('iranpayment::pages.test')->with([
'transaction_code' => $this->getTransactionCode(),
'reference_number' => $this->getReferenceNumber(),
]);
return [
'title' => 'تست',
'method' => 'POST',
];
}

public function purchase(): void
/**
* Pay Link function
*
* @return string
*/
public function purchaseUri(): string
{
$this->transactionUpdate([
'reference_number' => uniqid(),
]);
$url = filter_var($this->url, FILTER_VALIDATE_URL)
? $this->url
: url($this->url);

$url_parts = parse_url($url);
if (isset($url_parts['query'])) {
parse_str($url_parts['query'], $params);
} else {
$params = [];
}

$params['reference_number'] = $this->getReferenceNumber();

$url_parts['query'] = http_build_query($params);

return $url_parts['scheme'].'://'.$url_parts['host']
.(strlen($url_parts['port']) ? ':'.$url_parts['port'] : '')
.$url_parts['path']
.(strlen($url_parts['query']) ? '?'.$url_parts['query'] : '');
}

public function purchaseView(array $arr = [])
public function verify(): void
{
return view('iranpayment::pages.test', [
'reference_number' => uniqid(),
'transaction_code' => $this->getTransactionCode(),
]);
if ($this->request['status'] === 'error') {
throw TestException::error(-100);
}

$trackingCode = rand(111111, 999999);
$this->transactionSucceed([
'card_number' => rand(1111, 9999).'********'.rand(1111, 9999),
'tracking_code' => $trackingCode,
'reference_number' => 'RefNum-'.$trackingCode,
]);
}

public function purchaseUri(): string
public function bankView()
{
return route('iranpayment.test.pay', $this->getReferenceNumber());
return response()->view('iranpayment::pages.test', [
'transaction_code' => $this->getTransactionCode(),
'callback_url' => $this->preparedCallbackUrl(),
]);
}
}
24 changes: 3 additions & 21 deletions src/Gateways/Test/TestGatewayController.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,30 +9,12 @@

class TestGatewayController {

public function paymentView($code)
public function paymentView()
{
$payment = IranPayment::create('test');
$transaction = IranPaymentTransaction::where('reference_number', $code)->first();
$transaction = IranPaymentTransaction::where('reference_number', request()->get('reference_number'))->first();
$payment->setTransaction($transaction);

return $payment->view();
}

public function verify(Request $request, $code)
{
$transaction = IranPaymentTransaction::where('code', $code)->first();

$queryParams = http_build_query([
app('config')->get('iranpayment.transaction_query_param') => $code
]);

$callback = $transaction->extra['callback_url'];;

$question_mark = strpos($callback, '?');
if($question_mark) {
return redirect($callback.'&'.$queryParams);
}

return redirect($callback.'?'.$queryParams);
return $payment->bankView();
}
}
14 changes: 7 additions & 7 deletions src/Gateways/Test/routes.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,10 @@

use Illuminate\Support\Facades\Route;

Route::group(['namespace' => 'Dena\IranPayment\Gateways\Test'], function () {
Route::get('/iranpayment/test/{reference}', 'TestGatewayController@paymentView')
->name('iranpayment.test.pay');

Route::get('/iranpayment/test/{code}/verify', 'TestGatewayController@verify')
->name('iranpayment.test.verify');
});
if (app('config')->get('iranpayment.test.active', false) &&
app('config')->get('iranpayment.test.url') !== null) {
Route::post(
app('config')->get('iranpayment.test.url'),
'Dena\IranPayment\Gateways\Test\TestGatewayController@paymentView'
)->name('iranpayment.test.pay');
}
Loading

0 comments on commit 8f1f85d

Please sign in to comment.