1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Parkwayprojects\PayWithBank3D; |
4
|
|
|
|
5
|
|
|
use Exception; |
6
|
|
|
use Parkwayprojects\PayWithBank3D\Controllers\ApiRequest; |
7
|
|
|
use Parkwayprojects\PayWithBank3D\Exceptions\CouldNotProcess; |
8
|
|
|
|
9
|
|
|
class PayWithBank3D extends ApiRequest |
10
|
|
|
{ |
11
|
|
|
protected $initializeUrl = 'api/transaction/initialize'; |
12
|
|
|
protected $verifyUrl = 'api/payment/verify/'; |
13
|
|
|
|
14
|
|
|
protected $redirectUrl; |
15
|
|
|
|
16
|
|
|
protected function generateUrl($data) |
17
|
|
|
{ |
18
|
|
|
if (empty($data)) { |
19
|
|
|
$data = [ |
20
|
|
|
'amount' => intval(request()->amount), |
21
|
|
|
'currencyCode' => request()->currencyCode ? request()->currencyCode : 'NGN', |
22
|
|
|
'customer' => [ |
23
|
|
|
'name' => request()->name, |
24
|
|
|
'email' => request()->email, |
25
|
|
|
'phone' => request()->phone, |
26
|
|
|
], |
27
|
|
|
'returnUrl' => request()->returnUrl, |
28
|
|
|
'color' => request()->color ? request()->color : '#FF0000', |
29
|
|
|
'metadata' => request()->metadata, |
30
|
|
|
'reference' => ReferenceNumber::getHashedToken(), |
31
|
|
|
]; |
32
|
|
|
} |
33
|
|
|
array_filter($data); |
34
|
|
|
$result = $this->performPostRequest($this->initializeUrl, $data); |
35
|
|
|
$this->redirectUrl = $result['body']['data']['paymentUrl']; |
36
|
|
|
} |
37
|
|
|
|
38
|
|
|
public function setUrl($data = []) |
39
|
|
|
{ |
40
|
|
|
try { |
41
|
|
|
$this->generateUrl($data); |
42
|
|
|
|
43
|
|
|
return $this; |
44
|
|
|
} catch (\Exception $exception) { |
45
|
|
|
throw new Exception($exception->getMessage()); |
46
|
|
|
} |
47
|
|
|
} |
48
|
|
|
|
49
|
|
|
public function getUrl() |
50
|
|
|
{ |
51
|
|
|
return $this->redirectUrl; |
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
/** |
55
|
|
|
* Redirect to PayWithBank3D Payment Page. |
56
|
|
|
*/ |
57
|
|
|
public function redirectNow() |
58
|
|
|
{ |
59
|
|
|
return redirect($this->redirectUrl); |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
/** |
63
|
|
|
* Query PayWithBank3d Verify Route And Return True Or False If Payment Is Successful. |
64
|
|
|
* @return bool |
65
|
|
|
*/ |
66
|
|
|
protected function verifyReference() |
67
|
|
|
{ |
68
|
|
|
$reference = request()->query('reference'); |
69
|
|
|
$result = $this->performGetRequest($this->verifyUrl.$reference); |
70
|
|
|
//dd($result); |
71
|
|
|
return $result['body']['code'] === '00'; |
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
/** |
75
|
|
|
* Get Payment details if the transaction was verified successfully. |
76
|
|
|
* @throws \Parkwayprojects\PayWithBank3D\Exceptions\CouldNotProcess |
77
|
|
|
*/ |
78
|
|
|
public function getData() |
79
|
|
|
{ |
80
|
|
|
if ($this->verifyReference()) { |
81
|
|
|
return $this->getResponse(); |
82
|
|
|
} |
83
|
|
|
throw CouldNotProcess::invalidTransaction($this->getResponse()['body']['description']); |
84
|
|
|
} |
85
|
|
|
} |
86
|
|
|
|