forked from coreshop/payum-payone
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Api.php
executable file
·166 lines (143 loc) · 4.25 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
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
<?php
/**
* CoreShop.
*
* This source file is subject to the GNU General Public License version 3 (GPLv3)
* For the full copyright and license information, please view the LICENSE.md and gpl-3.0.txt
* files that are distributed with this source code.
*
* @copyright Copyright (c) 2015-2020 Dominik Pfaffenbauer (https://www.pfaffenbauer.at)
* @license https://www.coreshop.org/license GNU General Public License version 3 (GPLv3)
*/
namespace CoreShop\Payum\Payone;
use ArvPayoneApi\Api\Client;
use ArvPayoneApi\Api\PostApi;
use ArvPayoneApi\Request\Authorization\RequestFactory as AuthFactory;
use ArvPayoneApi\Request\Capture\RequestFactory as CaptureFactory;
use ArvPayoneApi\Request\PaymentTypes;
use ArvPayoneApi\Request\PreAuthorization\RequestFactory as PreAuthFactory;
use ArvPayoneApi\Request\SerializerFactory;
use Http\Message\MessageFactory;
use Payum\Core\Bridge\Spl\ArrayObject;
use Payum\Core\HttpClientInterface;
class Api
{
/**
* @var mixed
*/
protected $api;
/**
* @var HttpClientInterface
*/
protected $client;
/**
* @var MessageFactory
*/
protected $messageFactory;
const TYPES = [
'PayPal' => PaymentTypes::PAYONE_PAY_PAL,
'Sofort' => PaymentTypes::PAYONE_SOFORT,
'CreditCard' => PaymentTypes::PAYONE_CREDIT_CARD,
];
/**
* @var array|ArrayObject
*/
protected $options = [];
public function __construct(array $options, HttpClientInterface $client, MessageFactory $messageFactory)
{
$this->options = ArrayObject::ensureArrayObject($options);
$this->client = $client;
$this->messageFactory = $messageFactory;
}
/**
* @return bool
*/
public function isOnsite()
{
return in_array($this->getPaymentType(), [PaymentTypes::PAYONE_CREDIT_CARD, PaymentTypes::PAYONE_DIRECT_DEBIT], true);
}
/**
* @return bool
*/
public function getPaymentType()
{
return $this->getOption('paymentType');
}
/**
* @param $model
* @return \ArvPayoneApi\Response\ResponseContract
* @throws \Exception
*/
public function preAuthorize($model)
{
$model = $this->prepareModel($model);
return $this->getPostClient()->doRequest(PreAuthFactory::create($this->getOption('paymentType'), $model));
}
/**
* @param $model
* @return \ArvPayoneApi\Response\ResponseContract
* @throws \Exception
*/
public function authorize($model)
{
$model = $this->prepareModel($model);
return $this->getPostClient()->doRequest(AuthFactory::create($this->getOption('paymentType'), $model));
}
/**
* @param $model
* @return \ArvPayoneApi\Response\ResponseContract
* @throws \Exception
*/
public function capture($model)
{
$model = $this->prepareModel($model);
return $this->getPostClient()->doRequest(CaptureFactory::create($this->getOption('paymentType'), $model));
}
/**
* @param array $model
* @return array
*/
protected function prepareModel($model)
{
$model['context'] = [
'aid' => $this->getOption('accountId'),
'mid' => $this->getOption('merchantId'),
'portalid' => $this->getOption('portalId'),
'key' => $this->getOption('key'),
'mode' => $this->getOption('mode'),
'language' => 'en',
];
$model['systemInfo'] = [
'vendor' => 'THE_VENDOR',
'version' => '1',
'module' => 'THE_MODULE',
'module_version' => '1',
];
// required for bank transfer payments like SOFORT
$model['bankAccount'] = [
'country' => 'DE',
'holder' => 'Test GmbH',
'iban' => 'DE00123456782599100004',
'bic' => 'TESTTESX',
];
return $model;
}
/**
* @param $option
* @return mixed
*/
protected function getOption($option)
{
return $this->options[$option];
}
/**
* @return PostApi
*/
protected function getPostClient()
{
if (null === $this->api) {
$this->api = new PostApi(new Client(), SerializerFactory::createArraySerializer());
}
return $this->api;
}
}