-
Notifications
You must be signed in to change notification settings - Fork 21
/
Copy pathfxPayment.php
209 lines (186 loc) · 4.66 KB
/
fxPayment.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
<?php
/**
* Include composer class autoloader
*/
require_once dirname(__FILE__) . '/../vendor/autoload.php';
use PayU\Alu\Billing;
use PayU\Alu\Card;
use PayU\Alu\Client;
use PayU\Alu\Delivery;
use PayU\Alu\FX;
use PayU\Alu\MerchantConfig;
use PayU\Alu\Order;
use PayU\Alu\Product;
use PayU\Alu\Request;
use PayU\Alu\User;
use PayU\Alu\Exceptions\ConnectionException;
use PayU\Alu\Exceptions\ClientException;
use PayU\PaymentsApi\AluV3\AluV3;
/**
* Create configuration with params:
*
* Merchant Code - Your PayU Merchant Code
* Secret Key - Your PayU Secret Key
* Platform - RO | RU | UA | TR | HU
*/
$cfg = new MerchantConfig('MERCHANT_CODE', 'SECRET_KEY', 'RO');
/**
* Create user with params:
*
* User IP - User's IP address
* User Time - Time of user computer - optional
*
*/
$user = new User('127.0.0.1');
/**
* Create new order
*/
$order = new Order();
/**
* Setup the order params
*
* Full params available in the documentation
*/
$order->withBackRef('http://path/to/your/returnUrlScript')
->withOrderRef('MerchantOrderRef')
->withCurrency('RON')
->withOrderDate(gmdate('Y-m-d H:i:s'))
->withOrderTimeout(1000)
->withPayMethod('CCVISAMC');
/**
* Create new product
*/
$product = new Product();
/**
* Setup the product params
*
* Full params available in the documentation
*/
$product->withCode('PCODE01')
->withName('PNAME01')
->withPrice(100.0)
->withVAT(24.0)
->withQuantity(1);
/**
* Add the product to the order
*/
$order->addProduct($product);
/**
* Create another product
*/
$product = new Product();
/**
* Setup the product params
*
* Full params available in the documentation
*/
$product->withCode('PCODE02')
->withName('PNAME02')
->withPrice(200.0)
->withVAT(24.0)
->withQuantity(1);
/**
* Add the second product to the same order
*/
$order->addProduct($product);
/**
* Create new billing address
*/
$billing = new Billing();
/**
* Setup the billing address params
*
* Full params available in the documentation
*/
$billing->withAddressLine1('Address1')
->withAddressLine2('Address2')
->withCity('City')
->withCountryCode('RO')
->withEmail('[email protected]')
->withFirstName('FirstName')
->withLastName('LastName')
->withPhoneNumber('40123456789')
->withIdentityCardNumber('111222');
/**
* Create new delivery address
*
* If you want to have the same delivery as billing, skip these two steps
* and pass the Billing $billing object to the request twice
*/
$delivery = new Delivery();
/**
* Setup the delivery address params
*
* Full params available in the documentation
*/
$delivery->withAddressLine1('Address1')
->withAddressLine2('Address2')
->withCity('City')
->withCountryCode('RO')
->withEmail('[email protected]')
->withFirstName('FirstName')
->withLastName('LastName')
->withPhoneNumber('40123456789');
/**
* Create new Card with params:
*
* Credit Card Number
* Credit Card Expiration Month
* Credit Card Expiration Year
* Credit Card CVV (Security Code)
* Credit Card Owner
*/
$card = new Card(
'4111111111111111',
'12',
date("Y", strtotime("+1 year")),
'123',
'Card Owner Name'
);
$fx = new FX("EUR", 0.2462);
/**
* Create new Request with params:
*
* Config object
* Order object
* Billing object
* Delivery (or Billing object again, if you want to have the delivery address the same as the billing address)
* User object
* Api version - by default is used 'v3' for ALU v3
*/
$request = new Request($cfg, $order, $billing, $delivery, $user, AluV3::API_VERSION_V3);
$request->setFx($fx);
/**
* Add the Credit Card to the Request
*/
$request->setCard($card);
/**
* Create new API Client, passing the Config object as parameter
*/
$client = new Client($cfg);
/**
* Will throw different Exceptions on errors
*/
try {
/**
* Sends the Request to ALU and returns a Response
*
* See documentation for Response params
*/
$response = $client->pay($request);
/**
* In case of 3DS enrolled cards, PayU will return the URL_3DS that contains a unique url for each
* transaction. The merchant must redirect the browser to this url to allow user to authenticate.
* After the authentication process ends the user will be redirected to BACK_REF url
* with payment result in a HTTP POST request
*/
if ($response->isThreeDs()) {
header("Location:" . $response->getThreeDsUrl());
die();
}
echo $response->getStatus(). ' ' . $response->getReturnCode() . ' ' . $response->getReturnMessage();
} catch (ConnectionException $exception) {
echo $exception->getMessage();
} catch (ClientException $exception) {
echo $exception->getErrorMessage();
}