-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathApi.php
executable file
·151 lines (131 loc) · 3.9 KB
/
Api.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
<?php
/*
* This file is part of the mbe4 Payum package.
*
* (c) Sourcefabric z.ú. and contributors.
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
declare(strict_types=1);
namespace PayHelper\Payum\Mbe4;
class Api
{
const VERSION = '3.1.2';
const FIELD_CONTENTCLASS = 'contentclass';
const FIELD_DESCRIPTION = 'description';
const FIELD_CLIENT_TRANSACTION_ID = 'clienttransactionid';
const FIELD_AMOUNT = 'amount';
const FIELD_RETURNURL = 'returnurl';
const FIELD_CALLBACKURL = 'callbackurl';
const FIELD_TIMESTAMP = 'timestamp';
const FIELD_HASH = 'hash';
const FIELD_ID = 'id';
const FIELD_CURRENCY = 'currency';
const FIELD_RESPONSE_CODE = 'responsecode';
/**
* @var array
*/
protected $options = [];
/**
* @param array $options
*
* @throws \Payum\Core\Exception\InvalidArgumentException if an option is invalid
*/
public function __construct(array $options)
{
$this->options = $options;
}
/**
* @param array $fields
*
* @return string
*/
public function getOffsiteUrl(array $fields): string
{
$fields = $this->preparePayment($fields);
return urldecode($this->getApiEndpoint().'?'.http_build_query($fields));
}
/**
* @param array $fields
*
* @return array
*/
public function preparePayment(array $fields): array
{
$supportedParams = [
'password' => null,
'username' => null,
'clientid' => null,
'serviceid' => null,
self::FIELD_CONTENTCLASS => null,
self::FIELD_DESCRIPTION => null,
self::FIELD_ID => null,
self::FIELD_CLIENT_TRANSACTION_ID => null,
self::FIELD_AMOUNT => null,
self::FIELD_RETURNURL => null,
self::FIELD_CALLBACKURL => null,
self::FIELD_TIMESTAMP => null,
self::FIELD_HASH => null,
];
$fields[self::FIELD_CONTENTCLASS] = $this->options['contentclass'];
$fields = array_filter(array_replace(
$supportedParams,
array_intersect_key($fields, $supportedParams)
));
$config['username'] = $this->options['username'];
$config['clientid'] = $this->options['clientId'];
$config['serviceid'] = $this->options['serviceId'];
$fields = $config + $fields;
$fields[self::FIELD_CALLBACKURL] = $fields[self::FIELD_RETURNURL];
$fields[self::FIELD_TIMESTAMP] = $this->generateTimestamp();
$fields[self::FIELD_HASH] = $this->generateHash($fields);
unset($fields[self::FIELD_RETURNURL]);
return (array) $fields;
}
/**
* @param array $fields
*
* @return string
*/
private function generateHash(array $fields): string
{
return md5(implode('', [
$this->options['password'],
$this->options['username'],
$this->options['clientId'],
$this->options['serviceId'],
$fields[self::FIELD_CONTENTCLASS],
$fields[self::FIELD_DESCRIPTION],
$fields[self::FIELD_CLIENT_TRANSACTION_ID],
$fields[self::FIELD_AMOUNT],
$fields[self::FIELD_RETURNURL],
$fields[self::FIELD_TIMESTAMP],
]));
}
/**
* @return string
*/
private function generateTimestamp(): string
{
$now = new \DateTime();
$now->setTimezone(new \DateTimeZone('UTC'));
return $now->format('Y-m-d\Th:i:s.\0\0\0\Z');
}
/**
* @return string
*/
protected function getApiEndpoint(): string
{
return 'https://billing.mbe4.de/widget/singlepayment';
}
/**
* @return array
*/
public static function getSupportedCurrencies(): array
{
return [
'EUR',
];
}
}