forked from devinweb/laravel-hyperpay
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathLaravelHyperpay.php
212 lines (182 loc) · 5.39 KB
/
LaravelHyperpay.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
<?php
namespace Devinweb\LaravelHyperpay;
use Devinweb\LaravelHyperpay\Contracts\BillingInterface;
use Devinweb\LaravelHyperpay\Contracts\Hyperpay;
use Devinweb\LaravelHyperpay\Support\HttpClient;
use Devinweb\LaravelHyperpay\Support\HttpParameters;
use Devinweb\LaravelHyperpay\Support\HttpResponse;
use Devinweb\LaravelHyperpay\Support\TransactionBuilder;
use Devinweb\LaravelHyperpay\Traits\ManageUserTransactions;
use GuzzleHttp\Client as GuzzleClient;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Http\Request;
use Illuminate\Support\Arr;
use Illuminate\Support\Str;
class LaravelHyperpay implements Hyperpay
{
use ManageUserTransactions;
/** @var GuzzleClient */
protected $client;
/**
* @var BillingInterface
*/
protected $billing = [];
/**
* @var string token
*/
protected $token;
/**
* @var string brand
*/
protected $brand;
/**
* @var string redirect_url
*/
protected $redirect_url;
/**
* @var string hyperpay host
*/
protected $gateway_url = 'https://test.oppwa.com';
/**
* Create a new manager instance.
*
* @param \GuzzleHttp\Client as GuzzleClient $client
* @return void
*/
public function __construct(GuzzleClient $client)
{
$this->client = $client;
$this->config = config('hyperpay');
if (! config('hyperpay.sandboxMode')) {
$this->gateway_url = 'https://oppwa.com';
}
}
/**
* Set the mada entityId in the parameters that used to prepare the checkout.
*
* @return void
*/
public function mada()
{
$this->config['entityId'] = config('hyperpay.entityIdMada');
}
/**
* Set the apple pay entityId in the parameters that used to prepare the checkout.
*
* @return void
*/
public function setApplePayEntityId()
{
$this->config['entityId'] = config('hyperpay.entityIdApplePay');
}
/**
* Add billing data to the payment body.
*
* @param BillingInterface $billing;
*
* return $this
*/
public function addBilling(BillingInterface $billing)
{
$this->billing = $billing;
return $this;
}
/**
* Prepare the checkout.
*
* @param array $trackable_data
* @param Model $user
* @param float $amount
* @param string $brand
* @param Request $request
* @return \GuzzleHttp\Psr7\Response
*/
public function checkout(array $trackable_data, Model $user, $amount, $brand, Request $request)
{
$this->brand = $brand;
if (strtolower($this->brand) == 'mada') {
$this->mada();
}
if (strtolower($this->brand) == 'applepay') {
$this->setApplePayEntityId();
}
$trackable_data = array_merge($trackable_data, [
'amount' => $amount,
]);
return $this->prepareCheckout($user, $trackable_data, $request);
}
/**
* Define the data used to generate a successful
* response from hyperpay to generate the payment form.
*
* @param Model $user
* @param array $trackable_data
* @param Request $request
* @return \GuzzleHttp\Psr7\Response
*/
protected function prepareCheckout(Model $user, array $trackable_data, $request)
{
$this->token = $this->generateToken();
$this->config['merchantTransactionId'] = $this->token;
$this->config['userAgent'] = $request->server('HTTP_USER_AGENT');
$result = (new HttpClient($this->client, $this->gateway_url.'/v1/checkouts', $this->config))->post(
$parameters = (new HttpParameters())->postParams(Arr::get($trackable_data, 'amount'), $user, $this->config, $this->billing)
);
$response = (new HttpResponse($result, null, $parameters))
->setUser($user)
->setTrackableData($trackable_data)
->addScriptUrl($this->gateway_url)
->addShopperResultUrl($this->redirect_url)
->prepareCheckout();
return $response;
}
/**
* Check the payment status using $resourcePath and $checkout_id.
*
* @param string $resourcePath
* @param string $checkout_id
* @return \GuzzleHttp\Psr7\Response
*/
public function paymentStatus(string $resourcePath, string $checkout_id)
{
$result = (new HttpClient($this->client, $this->gateway_url.$resourcePath, $this->config))->get(
(new HttpParameters())->getParams($checkout_id),
);
$response = (new HttpResponse(
$result,
(new TransactionBuilder())->findByIdOrCheckoutId($checkout_id),
))->paymentStatus();
return $response;
}
/**
* Add merchantTransactionId.
*
* @param string $id
* @return $this
*/
public function addMerchantTransactionId($id)
{
$this->token = $id;
return $this;
}
/**
* Add redirection url to the shopper to finalize the payment.
*
* @param string $url
* @return $this
*/
public function addRedirectUrl($url)
{
$this->redirect_url = $url;
return $this;
}
/**
* Generate the token that used as merchantTransactionId to generate the payment form.
*
* @return string
*/
private function generateToken()
{
return ($this->token) ?: Str::random('64');
}
}