-
Notifications
You must be signed in to change notification settings - Fork 5
/
Api.php
170 lines (138 loc) · 5.24 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
167
168
169
170
<?php
namespace Payum\Sofort;
use Sofort\SofortLib\Refund;
use Sofort\SofortLib\Sofortueberweisung;
use Sofort\SofortLib\TransactionData;
class Api
{
public const STATUS_LOSS = 'loss';
public const SUB_LOSS = 'not_credited';
public const STATUS_PENDING = 'pending';
public const SUB_PENDING = 'not_credited_yet';
public const STATUS_RECEIVED = 'received';
public const SUB_CREDITED = 'credited';
public const SUB_PARTIALLY = 'partially_credited';
public const SUB_OVERPAYMENT = 'overpayment';
public const STATUS_REFUNDED = 'refunded';
public const SUB_COMPENSATION = 'compensation';
public const SUB_REFUNDED = 'refunded';
public const STATUS_UNTRACEABLE = 'untraceable';
public const SUB_SOFORT_NEEDED = 'sofort_bank_account_needed';
protected $options = [
'config_key' => null,
];
public function __construct(array $options)
{
$this->options = array_replace([
'config_key' => null,
'abort_url' => null,
'disable_notification' => false,
], $options);
}
/**
* @return array
*/
public function createTransaction(array $fields)
{
$fields = (array_replace([
'success_url' => null,
'success_link_redirect' => true,
'abort_url' => null,
'notification_url' => null,
'notify_on' => implode(',', [self::STATUS_PENDING, self::STATUS_LOSS, self::STATUS_RECEIVED, self::STATUS_REFUNDED, self::STATUS_UNTRACEABLE]),
'reason' => '',
'reason_2' => '',
'product_code' => null,
], $fields));
$sofort = new Sofortueberweisung($this->options['config_key']);
$sofort->setAmount($fields['amount']);
$sofort->setCurrencyCode($fields['currency_code']);
$sofort->setReason($fields['reason'], $fields['reason_2'], $fields['product_code']);
$sofort->setSuccessUrl($fields['success_url'], $fields['success_link_redirect']);
$sofort->setAbortUrl($fields['abort_url']);
if (! $this->options['disable_notification']) {
$sofort->setNotificationUrl($fields['notification_url'], $fields['notify_on']);
}
$sofort->sendRequest();
return array_filter([
'error' => $sofort->getError(),
'transaction_id' => $sofort->getTransactionId(),
'payment_url' => $sofort->getPaymentUrl(),
]);
}
/**
* @return array
*/
public function getTransactionData($transactionId)
{
$transactionData = new TransactionData($this->options['config_key']);
$transactionData->addTransaction($transactionId);
$transactionData->setApiVersion('2.0');
$transactionData->sendRequest();
$fields = [];
$methods = [
'getAmount' => '',
'getAmountRefunded' => '',
'getCount' => '',
'getPaymentMethod' => '',
'getConsumerProtection' => '',
'getStatus' => '',
'getStatusReason' => '',
'getStatusModifiedTime' => '',
'getLanguageCode' => '',
'getCurrency' => '',
'getTransaction' => '',
'getReason' => [0, 0],
'getUserVariable' => 0,
'getTime' => '',
'getProjectId' => '',
'getRecipientHolder' => '',
'getRecipientAccountNumber' => '',
'getRecipientBankCode' => '',
'getRecipientCountryCode' => '',
'getRecipientBankName' => '',
'getRecipientBic' => '',
'getRecipientIban' => '',
'getSenderHolder' => '',
'getSenderAccountNumber' => '',
'getSenderBankCode' => '',
'getSenderCountryCode' => '',
'getSenderBankName' => '',
'getSenderBic' => '',
'getSenderIban' => '',
];
foreach ($methods as $method => $params) {
$varName = $method;
$varName = strtolower(preg_replace('/([^A-Z])([A-Z])/', '$1_$2', substr($varName, 3)));
if (is_array($params) && 2 == count($params)) {
$fields[$varName] = $transactionData->{$method}($params[0], $params[1]);
} elseif ('' !== $params) {
$fields[$varName] = $transactionData->{$method}($params);
} else {
$fields[$varName] = $transactionData->{$method}();
}
}
if ($transactionData->isError()) {
$fields['error'] = $transactionData->getError();
}
return $fields;
}
/**
* @return array
*/
public function refundTransaction(array $fields)
{
$refund = new Refund($this->options['config_key']);
$refund->setSenderSepaAccount($fields['recipient_bic'], $fields['recipient_iban'], $fields['recipient_holder']);
$refund->addRefund($fields['transaction_id'], $fields['refund_amount']);
$refund->setPartialRefundId(md5(uniqid()));
$refund->setReason($fields['reason']);
$refund->sendRequest();
if ($refund->isError()) {
$fields['refund_error'] = $refund->getError();
} else {
$fields['refund_url'] = $refund->getPaymentUrl();
}
return $fields;
}
}