-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathOrder.php
123 lines (110 loc) · 3.37 KB
/
Order.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
<?php
namespace Tco\Source\Api\Rest\V6;
use Tco\Exceptions\TcoException;
class Order{
/**
* @var ApiCore
*/
private $core;
/**
* @var array
*/
private $acceptedSearchOrderParams = array(
'RefNo',
'ApproveStatus',
'Newer',
'Status',
'StartDate',
'EndDate',
'PartnerOrders',
'ExternalRefNo',
'Page',
'Limit'
);
/**
* Order constructor.
*
* @param ApiCore $apiCore
*/
public function __construct( $apiCore ) {
$this->core = $apiCore;
}
/**
* @param array $params
*
* @return array
* @throws \TcoException
*/
public function place( $params) {
try {
return $this->core->call( '/orders/', $params );
} catch ( TcoException $exception ) {
throw new TcoException( sprintf( 'Error when trying to place order: %s', $exception->getMessage() ) );
}
}
/**
* @param array $searchParams
*
* @return array
* @throws \TcoException
*/
public function getOrder( $searchParams) {
$rejectedParams = $this->validateOrderSearchParams( $searchParams );
if ( count( $rejectedParams ) == 0 ) {
try {
$search = '/orders/';
//If RefNo then we only search by it
if ( isset( $searchParams['RefNo'] ) ) {
$search .= $searchParams['RefNo'] . '/';
unset( $searchParams );
}
//Else do search for multiple orders using the mix of parameters.
if ( ! empty( $searchParams ) ) {
$search .= '?' . http_build_query( $searchParams );
}
return $this->core->call( $search, [], 'GET' );
} catch ( TcoException $exception ) {
throw new TcoException( sprintf( 'Error when trying to search for order: %s', $exception->getMessage() ) );
}
} else {
throw new TcoException( sprintf( 'Exception! Some order search parameters are not accepted: %s', implode(
', ',
$rejectedParams ) ) );
}
}
/**
* @param array $params
*
* @return array
* @throws \TcoException
*/
public function issueRefund( $params ) {
if ( ! isset( $params['RefNo'] ) ) {
throw new TcoException( 'RefNo is required for Refund!' );
}
$refNo = $params['RefNo'];
if ( ! isset( $params['refundParams'] ) ) {
throw new TcoException( 'Refund parameters are required for Refund!' );
}
$refundParams = $params['refundParams'];
try {
return $this->core->call( 'orders/' . $refNo . '/refund/', $refundParams, 'POST' );
} catch ( TcoException $exception ) {
throw new TcoException( sprintf( 'Error when placing refund request: %s', $exception->getMessage() ) );
}
}
/**
* @param $searchParams
*
* @return array
*/
public function validateOrderSearchParams( $searchParams ) {
$notAccepted = array();
foreach ( $searchParams as $k => $v ) {
if ( ! array_key_exists( $k, array_flip( $this->acceptedSearchOrderParams ) ) ) {
$notAccepted[] = $k;
}
}
return $notAccepted;
}
}