-
Notifications
You must be signed in to change notification settings - Fork 48
/
Copy pathPaymentmethods.php
129 lines (111 loc) · 3.89 KB
/
Paymentmethods.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
<?php
namespace Paynl;
use Paynl\Api\Transaction as Api;
/**
* Description of Paymentmethods
*
* @author Andy Pieters <[email protected]>
*/
class Paymentmethods
{
/**
* Reorder the result from the Transaction/getService API into a more logical format
*
* @param array $input The result from the getService API
* @return array
*/
private static function reorderOutput($input)
{
$paymentMethods = array();
foreach ((array)$input['countryOptionList'] as $country) {
foreach ((array)$country['paymentOptionList'] as $paymentOption) {
if (isset($paymentMethods[$paymentOption['id']])) {
$paymentMethods[$paymentOption['id']]['countries'][] = $country['id'];
continue;
}
$banks = array();
if (!empty($paymentOption['paymentOptionSubList'])) {
foreach ((array)$paymentOption['paymentOptionSubList'] as $optionSub) {
$image = '';
if (isset($optionSub['image'])) {
$image = $optionSub['image'];
}
$banks[] = array(
'id' => $optionSub['id'],
'name' => $optionSub['name'],
'visibleName' => $optionSub['visibleName'],
'image' => $image,
);
}
}
$paymentMethods[$paymentOption['id']] = array(
'id' => $paymentOption['id'],
'name' => $paymentOption['name'],
'visibleName' => $paymentOption['visibleName'],
'min_amount' => $paymentOption['min_amount'],
'max_amount' => $paymentOption['max_amount'],
'countries' => array($country['id']),
'banks' => $banks,
'brand' => $paymentOption['brand'],
);
}
}
return $paymentMethods;
}
/**
* Filter the result to only return payment methods allowed for a country
*
* @param array $paymentMethods
* @param string $country
* @return array filtered paymentmethods
*/
private static function filterCountry($paymentMethods, $country)
{
$output = array();
foreach ($paymentMethods as $paymentMethod) {
if (in_array($country, $paymentMethod['countries'], true)
|| in_array('ALL', $paymentMethod['countries'], true)
) {
$output[] = $paymentMethod;
}
}
return $output;
}
/**
* Get a list of available payment methods
*
* @param array $options
* @param null $languageCode For translation of names and descriptions. Use for example 'en' or 'nl'.
* @return array
* @throws Error\Api
* @throws Error\Error
* @throws Error\Required\ApiToken
*/
public static function getList(array $options = array(), $languageCode = null)
{
$api = new Api\GetService();
if (!empty($languageCode)) {
$api->setLanguageCode($languageCode);
}
$result = $api->doRequest();
$paymentMethods = self::reorderOutput($result);
if (isset($options['country'])) {
$paymentMethods = self::filterCountry($paymentMethods, $options['country']);
}
return $paymentMethods;
}
/**
* Get a list of available banks
*
* @param int|null $paymentMethodId If empty, the paymentMethodId for iDEAL will be used
* @return array
*/
public static function getBanks($paymentMethodId = 10)
{
$paymentMethods = self::getList();
if (isset($paymentMethods[$paymentMethodId])) {
return $paymentMethods[$paymentMethodId]['banks'];
}
return array();
}
}