Skip to content

Commit 4f9eb6e

Browse files
committed
added fake adapter for test
1 parent 11ffb00 commit 4f9eb6e

File tree

3 files changed

+168
-0
lines changed

3 files changed

+168
-0
lines changed

config/larapay.php

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -121,6 +121,18 @@
121121
'pay_ir' => [
122122
'api' => env('PAY_IR_API_KEY', ''),
123123
],
124+
125+
/*
126+
|--------------------------------------------------------------------------
127+
| Fake gateway configuration -- for TESTING purposes only
128+
|--------------------------------------------------------------------------
129+
*/
130+
'fake' => [
131+
'base_url' => env('FAKE_BASE_URL', 'http://fake.paysuper.com'),
132+
'merchant_id' => env('FAKE_MERCHANT_ID', 1),
133+
],
134+
135+
124136
/*
125137
|--------------------------------------------------------------------------
126138
| SoapClient Options

src/Adapter/Fake.php

Lines changed: 142 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,142 @@
1+
<?php
2+
3+
namespace Tartan\Larapay\Adapter;
4+
5+
use Tartan\Larapay\Adapter\Saman\Exception;
6+
use Illuminate\Support\Facades\Log;
7+
use Tartan\Larapay\Adapter\Pasargad\Helper;
8+
9+
/**
10+
* Class Fake
11+
* @package Tartan\Larapay\Adapter
12+
*/
13+
class Fake extends AdapterAbstract implements AdapterInterface
14+
{
15+
protected $baseUrl;
16+
protected $endPoint;
17+
18+
protected $reverseSupport = true;
19+
20+
public function init()
21+
{
22+
parent::init();
23+
24+
$this->baseUrl = config('larapay.fake.base_url');
25+
}
26+
27+
/**
28+
* @return string
29+
* @throws \Tartan\Larapay\Adapter\Exception
30+
*/
31+
protected function requestToken(): string
32+
{
33+
if ($this->getTransaction()->checkForRequestToken() == false) {
34+
throw new Exception('larapay::larapay.could_not_request_payment');
35+
}
36+
37+
$this->checkRequiredParameters([
38+
'amount',
39+
'redirect_url',
40+
]);
41+
42+
$sendParams = [
43+
'amount' => intval($this->amount),
44+
'order_id' => $this->order_id,
45+
'merchant_id' => $this->merchant_id,
46+
'redirect' => $this->redirect_url,
47+
];
48+
49+
try {
50+
Log::debug('PaymentRequest call', $sendParams);
51+
$result = Helper::post2https($sendParams, $this->baseUrl . '/api/token');
52+
53+
54+
$resultObj = json_decode($result);
55+
Log::info('PaymentRequest response', $this->obj2array($resultObj));
56+
57+
58+
if (isset($resultObj->status)) {
59+
60+
if ($resultObj->status == 1) {
61+
$this->getTransaction()->setGatewayToken($resultObj->transId); // update transaction reference id
62+
63+
return $resultObj->transId;
64+
} else {
65+
throw new Exception($resultObj->status);
66+
}
67+
} else {
68+
throw new Exception('larapay::larapay.invalid_response');
69+
}
70+
} catch (\Exception $e) {
71+
throw new Exception('PayIr Fault: ' . $e->getMessage() . ' #' . $e->getCode(), $e->getCode());
72+
}
73+
}
74+
75+
public function generateForm(): string
76+
{
77+
Log::debug(__METHOD__, $this->getParameters());
78+
79+
$token = $this->requestToken();
80+
81+
Log::info(__METHOD__, ['fetchedToken' => $token]);
82+
83+
return view('larapay::fake-form', [
84+
'endPoint' => $this->getEndPoint(),
85+
'submitLabel' => !empty($this->submit_label) ? $this->submit_label : trans("larapay::larapay.goto_gate"),
86+
'autoSubmit' => boolval($this->auto_submit),
87+
]);
88+
}
89+
90+
/**
91+
* @return bool
92+
* @throws \Tartan\Larapay\Adapter\Exception
93+
*/
94+
protected function verifyTransaction(): bool
95+
{
96+
return true;
97+
}
98+
99+
/**
100+
* @return bool
101+
* @throws \Tartan\Larapay\Adapter\Exception
102+
*/
103+
protected function reverseTransaction(): bool
104+
{
105+
return true;
106+
}
107+
108+
/**
109+
* @return bool
110+
*/
111+
public function canContinueWithCallbackParameters(): bool
112+
{
113+
114+
if (!empty($this->parameters['transId'])) {
115+
return true;
116+
}
117+
118+
return false;
119+
}
120+
121+
/**
122+
* @return string
123+
* @throws \Tartan\Larapay\Adapter\Exception
124+
*/
125+
public function getGatewayReferenceId(): string
126+
{
127+
$this->checkRequiredParameters([
128+
'transId',
129+
]);
130+
131+
return $this->transId;
132+
}
133+
134+
protected function getEndPoint(): string
135+
{
136+
if (config('larapay.mode') == 'production') {
137+
throw new \Tartan\Larapay\Adapter\Exception('You have used fake adapter in production environment!');
138+
} else {
139+
return $this->baseUrl . '/ipg/gateway';
140+
}
141+
}
142+
}

views/fake-form.blade.php

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
<form id="goto_fake_bank" class="form-horizontal goto-bank-form" method="POST" action="{!! $endPoint !!}">
2+
<div class="control-group">
3+
<div class="controls">
4+
<button type="submit" class="btn btn-success">{{$submitLabel}}</button>
5+
</div>
6+
</div>
7+
</form>
8+
9+
@if($autoSubmit === true)
10+
<script type="text/javascript">
11+
var f=document.getElementById('goto_fake_bank');
12+
f.submit();
13+
</script>
14+
@endif

0 commit comments

Comments
 (0)