Skip to content

Commit a59580a

Browse files
committed
added order test
added void,refund and verify to transactions
1 parent 8ca29b3 commit a59580a

File tree

10 files changed

+236
-17
lines changed

10 files changed

+236
-17
lines changed

lib/Enums/MastercardApiOperations.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,4 +15,7 @@ abstract class MastercardApiOperations
1515

1616
// Transactions
1717
const PAY = 'PAY';
18+
const VOID = 'VOID';
19+
const REFUND = 'REFUND';
20+
const VERIFY = 'VERIFY';
1821
}

lib/Order.php

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,24 @@
33

44
namespace Mastercard;
55

6-
7-
use Mastercard\ApiResource;
8-
6+
/**
7+
* Class Order
8+
*
9+
* @property string id
10+
* @property string amount
11+
* @property string creationTime
12+
* @property string currency
13+
* @property string merchant
14+
* @property string result
15+
* @property string totalAuthorizedAmount
16+
* @property string totalCapturedAmount
17+
* @property string totalRefundedAmount
18+
*
19+
* @package Mastercard
20+
*/
921
class Order extends ApiResource
1022
{
1123
const OBJECT_NAME = 'order';
24+
25+
use ApiOperations\Retrieve;
1226
}

lib/Transaction.php

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,4 +28,22 @@ public static function pay($trans_id, $order_id, Factory $factory, $opts = null)
2828
$params = $factory->apiOperation(ApiOp::PAY)->get();
2929
return static::update($order_id, $params, $opts, $trans_id);
3030
}
31+
32+
public static function void($trans_id, $order_id, Factory $factory, $opts = null)
33+
{
34+
$params = $factory->apiOperation(ApiOp::VOID)->get();
35+
return static::update($order_id, $params, $opts, $trans_id);
36+
}
37+
38+
public static function refund($trans_id, $order_id, Factory $factory, $opts = null)
39+
{
40+
$params = $factory->apiOperation(ApiOp::REFUND)->get();
41+
return static::update($order_id, $params, $opts, $trans_id);
42+
}
43+
44+
public static function verify($trans_id, $order_id, Factory $factory, $opts = null)
45+
{
46+
$params = $factory->apiOperation(ApiOp::VERIFY)->get();
47+
return static::update($order_id, $params, $opts, $trans_id);
48+
}
3149
}

lib/Util/Factory.php

Lines changed: 24 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,9 @@ public function __construct()
1717
$this->builder = new ObjectBuilder();
1818
}
1919

20+
/**
21+
* @return Factory
22+
*/
2023
public static function create()
2124
{
2225
return new Factory();
@@ -90,7 +93,7 @@ public function apiOperation($op = null)
9093
* inject 3DSecureId into mastercard api
9194
*
9295
* @param $id
93-
* @return $this
96+
* @return Factory
9497
*/
9598
public function threeDSecureId($id)
9699
{
@@ -144,7 +147,7 @@ public function session($id, $version = null)
144147
* add sourceOfFund item to mastercard api array
145148
*
146149
* @param string $type
147-
* @return $this
150+
* @return Factory
148151
*/
149152
public function sourceOfFunds($type)
150153
{
@@ -157,7 +160,7 @@ public function sourceOfFunds($type)
157160
* add payment type to mastercard api array
158161
*
159162
* @param $type
160-
* @return $this
163+
* @return Factory
161164
*/
162165
public function paymentType($type = null)
163166
{
@@ -173,7 +176,7 @@ public function paymentType($type = null)
173176
* @param null $city
174177
* @param null $state_province
175178
* @param null $street
176-
* @return $this
179+
* @return Factory
177180
*/
178181
public function billing($country, $city = null, $state_province = null, $street = null)
179182
{
@@ -182,6 +185,21 @@ public function billing($country, $city = null, $state_province = null, $street
182185
return $this;
183186
}
184187

188+
/**
189+
* add transaction id to mastercard api array
190+
*
191+
* @param $trans_id
192+
* @param $amount
193+
* @param $currency
194+
* @return Factory
195+
*/
196+
public function transaction($trans_id = null, $amount = null, $currency = null)
197+
{
198+
$this->builder->transaction($trans_id, $amount, $currency);
199+
200+
return $this;
201+
}
202+
185203
/**
186204
* return all params as array with correct format that accepted by mastercard api
187205
*
@@ -192,7 +210,8 @@ public function get()
192210
return $this->builder->get();
193211
}
194212

195-
public function reset() {
213+
public function reset()
214+
{
196215
$this->builder = new ObjectBuilder();
197216
}
198217
}

lib/Util/ObjectBuilder.php

Lines changed: 33 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ class ObjectBuilder
2222
protected $billing = [];
2323
protected $sourceOfFunds = [];
2424
protected $paymentType = [];
25+
protected $transaction = [];
2526

2627
/**
2728
* @param $first_name
@@ -42,7 +43,7 @@ public function customer($first_name, $last_name, $email, $phone = null, $params
4243
];
4344

4445
if ($phone) {
45-
$this->customer['customer']['phone'] = $phone;
46+
$this->customer['customer']['mobilePhone'] = $phone;
4647
}
4748

4849
foreach ($params as $k => $v) {
@@ -245,6 +246,33 @@ public function paymentType($type = null): ObjectBuilder
245246
return $this;
246247
}
247248

249+
/**
250+
* @param $trans_id
251+
* @param $amount
252+
* @param $currency
253+
* @return ObjectBuilder
254+
*/
255+
public function transaction($trans_id = null, $amount = null, $currency = null): ObjectBuilder
256+
{
257+
$this->transaction = [
258+
'transaction' => []
259+
];
260+
261+
if ($trans_id) {
262+
$this->transaction['transaction']['targetTransactionId'] = $trans_id;
263+
}
264+
265+
if ($amount) {
266+
$this->transaction['transaction']['amount'] = $amount;
267+
}
268+
269+
if ($currency) {
270+
$this->transaction['transaction']['currency'] = $currency;
271+
}
272+
273+
return $this;
274+
}
275+
248276
/**
249277
* return all filled data as array with mastercard api write format
250278
*
@@ -294,6 +322,10 @@ public function get(): array
294322
$all_array = array_merge($this->paymentType, $all_array);
295323
}
296324

325+
if (!empty($this->transaction)) {
326+
$all_array = array_merge($this->transaction, $all_array);
327+
}
328+
297329
return $all_array;
298330
}
299331

tests/Mastercard/OrderTest.php

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
<?php
2+
3+
4+
namespace Mastercard\Mastercard;
5+
6+
7+
use Mastercard\Enums\Currency;
8+
use Mastercard\Enums\MastercardApiOperations as ApiOp;
9+
use Mastercard\Order;
10+
use Mastercard\Session;
11+
use Mastercard\TestCase;
12+
use Mastercard\Transaction;
13+
use Mastercard\Util\Factory;
14+
use Mastercard\Util\RandomGenerator;
15+
16+
class OrderTest extends TestCase
17+
{
18+
public function testIsRetrievable()
19+
{
20+
$trans_id = RandomGenerator::uuid();
21+
$order_id = RandomGenerator::uuid();
22+
$session = Session::create();
23+
$session = Session::update($session->session->id, Factory::create()
24+
->card('5123450000000008', 'AMR AHMED', '05', '21', '100')
25+
->get()
26+
);
27+
28+
$factory = Factory::create()
29+
->apiOperation(ApiOp::PAY)
30+
->session($session->session->id)
31+
->order(100, Currency::KWD, false)
32+
->sourceOfFunds('CARD');
33+
34+
Transaction::pay($trans_id, $order_id, $factory);
35+
36+
$this->expectsRequest(
37+
'get',
38+
'/order/' . $order_id
39+
);
40+
41+
$resource = Order::retrieve($order_id);
42+
$this->assertInstanceOf(Order::class, $resource);
43+
44+
print_r($resource);
45+
}
46+
}

tests/Mastercard/SessionTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ public function testIsUpdatable()
6262
Factory::create()
6363
->order('100', 'KWD')
6464
->customer('Amr', 'Ahmed', 'aemaddin@gmail.com')
65-
->card('5123450000000008', 'AMR EMADELDIN AHMED', '05', '21')
65+
->card('5123450000000008', 'AMR AHMED', '05', '21')
6666
->paymentType()
6767
->get()
6868
);

tests/Mastercard/ThreeDSTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ public function testCheckForEnrollment()
3636
Factory::create()
3737
->order(10, Currency::KWD, false)
3838
->customer('Amr', 'Ahmed', 'aemaddin@gmail.com')
39-
->card('5123450000000008', 'AMR EMADELDIN AHMED', '05', '21')
39+
->card('5123450000000008', 'AMR AHMED', '05', '21')
4040
->get()
4141
);
4242

tests/Mastercard/TransactionTest.php

Lines changed: 90 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ public function testIsPayable()
2121
$order_id = RandomGenerator::uuid();
2222
$session = Session::create();
2323
$session = Session::update($session->session->id, Factory::create()
24-
->card('5123450000000008', 'AMR EMADELDIN AHMED', '05', '21', '100')
24+
->card('5123450000000008', 'AMR AHMED', '05', '21', '100')
2525
->get()
2626
);
2727

@@ -49,7 +49,7 @@ public function testIsFailOnMissing3DS()
4949
$three_d_s = RandomGenerator::uuid();
5050
$session = Session::create();
5151
$session = Session::update($session->session->id, Factory::create()
52-
->card('5123450000000008', 'AMR EMADELDIN AHMED', '05', '21', '100')
52+
->card('5123450000000008', 'AMR AHMED', '05', '21', '100')
5353
->get()
5454
);
5555

@@ -86,7 +86,7 @@ public function testIsRetrievable()
8686
$order_id = RandomGenerator::uuid();
8787
$session = Session::create();
8888
$session = Session::update($session->session->id, Factory::create()
89-
->card('5123450000000008', 'AMR EMADELDIN AHMED', '05', '21', '100')
89+
->card('5123450000000008', 'AMR AHMED', '05', '21', '100')
9090
->get()
9191
);
9292

@@ -107,4 +107,91 @@ public function testIsRetrievable()
107107
$this->assertInstanceOf(Transaction::class, $resource);
108108
}
109109

110+
public function testIsAvoidable()
111+
{
112+
$trans_id = RandomGenerator::uuid();
113+
$void_trans_id = RandomGenerator::uuid();
114+
$order_id = RandomGenerator::uuid();
115+
$session = Session::create();
116+
$session = Session::update($session->session->id, Factory::create()
117+
->card('5123450000000008', 'AMR AHMED', '05', '21', '100')
118+
->get()
119+
);
120+
121+
$factory = Factory::create()
122+
->apiOperation(ApiOp::PAY)
123+
->session($session->session->id)
124+
->order(100, Currency::KWD, false)
125+
->sourceOfFunds('CARD');
126+
127+
Transaction::pay($trans_id, $order_id, $factory);
128+
129+
$this->expectsRequest(
130+
'put',
131+
'/order/' . $order_id . '/transaction/' . $void_trans_id
132+
);
133+
134+
$factory = Factory::create()->transaction($trans_id);
135+
136+
$resource = Transaction::void($void_trans_id, $order_id, $factory);
137+
$this->assertInstanceOf(Transaction::class, $resource);
138+
}
139+
140+
public function testIsRefundable()
141+
{
142+
$trans_id = RandomGenerator::uuid();
143+
$void_trans_id = RandomGenerator::uuid();
144+
$order_id = RandomGenerator::uuid();
145+
$session = Session::create();
146+
$session = Session::update($session->session->id, Factory::create()
147+
->card('5123450000000008', 'AMR AHMED', '05', '21', '100')
148+
->get()
149+
);
150+
151+
$factory = Factory::create()
152+
->apiOperation(ApiOp::PAY)
153+
->session($session->session->id)
154+
->order(100, Currency::KWD, false)
155+
->sourceOfFunds('CARD');
156+
157+
Transaction::pay($trans_id, $order_id, $factory);
158+
159+
$this->expectsRequest(
160+
'put',
161+
'/order/' . $order_id . '/transaction/' . $void_trans_id
162+
);
163+
164+
$factory = Factory::create()->transaction(null, 50, Currency::KWD);
165+
166+
$resource = Transaction::refund($void_trans_id, $order_id, $factory);
167+
$this->assertInstanceOf(Transaction::class, $resource);
168+
169+
print_r($resource);
170+
}
171+
172+
public function testIsVerifiable()
173+
{
174+
$trans_id = RandomGenerator::uuid();
175+
$order_id = RandomGenerator::uuid();
176+
$session = Session::create();
177+
$session = Session::update($session->session->id, Factory::create()
178+
->card('5123450000000008', 'AMR AHMED', '05', '21', '100')
179+
->get()
180+
);
181+
182+
$factory = Factory::create()
183+
->apiOperation(ApiOp::PAY)
184+
->session($session->session->id)
185+
->order(100, Currency::KWD, false)
186+
->sourceOfFunds('CARD');
187+
188+
$this->expectsRequest(
189+
'put',
190+
'/order/' . $order_id . '/transaction/' . $trans_id
191+
);
192+
193+
$resource = Transaction::verify($trans_id, $order_id, $factory);
194+
$this->assertInstanceOf(Transaction::class, $resource);
195+
}
196+
110197
}

tests/TestCase.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -36,9 +36,9 @@ protected function setUp(): void
3636
$this->clientMock = $this->createMock('\Mastercard\HttpClient\ClientInterface');
3737

3838
// Set up host and credentials for mastercard-mock
39-
Mastercard::setMerchantId('TEST0045');
40-
Mastercard::setApiKey('8d14b385fdd9e7db7d133a8ea684d3f9');
41-
Mastercard::setApiVersion(53);
39+
Mastercard::setMerchantId('GATEWAY_MERCHANT_ID');
40+
Mastercard::setApiKey('GATEWAY_API_PASSWORD');
41+
Mastercard::setApiVersion('GATEWAY_API_VERSION');
4242
}
4343

4444
protected function tearDown(): void

0 commit comments

Comments
 (0)