-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathKlarnaInvoiceGatewayFactory.php
115 lines (103 loc) · 5.29 KB
/
KlarnaInvoiceGatewayFactory.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
<?php
namespace Payum\Klarna\Invoice;
use Klarna;
use KlarnaCountry;
use KlarnaCurrency;
use KlarnaLanguage;
use Payum\Core\Bridge\Spl\ArrayObject;
use Payum\Core\Exception\LogicException;
use Payum\Core\GatewayFactory;
use Payum\Klarna\Invoice\Action\Api\ActivateAction;
use Payum\Klarna\Invoice\Action\Api\ActivateReservationAction;
use Payum\Klarna\Invoice\Action\Api\CancelReservationAction;
use Payum\Klarna\Invoice\Action\Api\CheckOrderStatusAction;
use Payum\Klarna\Invoice\Action\Api\CreditInvoiceAction;
use Payum\Klarna\Invoice\Action\Api\CreditPartAction;
use Payum\Klarna\Invoice\Action\Api\EmailInvoiceAction;
use Payum\Klarna\Invoice\Action\Api\GetAddressesAction;
use Payum\Klarna\Invoice\Action\Api\PopulateKlarnaFromDetailsAction;
use Payum\Klarna\Invoice\Action\Api\ReserveAmountAction;
use Payum\Klarna\Invoice\Action\Api\ReturnAmountAction;
use Payum\Klarna\Invoice\Action\Api\SendInvoiceAction;
use Payum\Klarna\Invoice\Action\Api\UpdateAction;
use Payum\Klarna\Invoice\Action\AuthorizeAction;
use Payum\Klarna\Invoice\Action\CaptureAction;
use Payum\Klarna\Invoice\Action\RefundAction;
use Payum\Klarna\Invoice\Action\StatusAction;
use Payum\Klarna\Invoice\Action\SyncAction;
class KlarnaInvoiceGatewayFactory extends GatewayFactory
{
protected function populateConfig(ArrayObject $config): void
{
if (! class_exists(KlarnaCurrency::class)) {
throw new \LogicException('You must install "fp/klarna-invoice" library.');
}
$config->defaults([
'payum.factory_name' => 'klarna_invoice',
'payum.factory_title' => 'Klarna Invoice',
'sandbox' => true,
'pClassStorage' => 'json',
'pClassStoragePath' => './pclasses.json',
'xmlRpcVerifyHost' => 2,
'xmlRpcVerifyPeer' => true,
'payum.action.capture' => new CaptureAction(),
'payum.action.authorize' => new AuthorizeAction(),
'payum.action.status' => new StatusAction(),
'payum.action.sync' => new SyncAction(),
'payum.action.refund' => new RefundAction(),
'payum.action.api.activate' => new ActivateAction(),
'payum.action.api.activate_reservation' => new ActivateReservationAction(),
'payum.action.api.cancel_reservation' => new CancelReservationAction(),
'payum.action.api.check_order_status' => new CheckOrderStatusAction(),
'payum.action.api.get_addresses' => new GetAddressesAction(),
'payum.action.api.populate_klarna_from_details' => new PopulateKlarnaFromDetailsAction(),
'payum.action.api.credit_invoice' => new CreditInvoiceAction(),
'payum.action.api.credit_part' => new CreditPartAction(),
'payum.action.api.reserve_amount' => new ReserveAmountAction(),
'payum.action.api.return_amount' => new ReturnAmountAction(),
'payum.action.api.email_invoice' => new EmailInvoiceAction(),
'payum.action.api.send_invoice' => new SendInvoiceAction(),
'payum.action.api.update' => new UpdateAction(),
]);
if (! $config['payum.api']) {
$config['payum.default_options'] = [
'eid' => '',
'secret' => '',
'country' => '',
'language' => '',
'currency' => '',
'sandbox' => true,
];
$config->defaults($config['payum.default_options']);
$config['payum.required_options'] = ['eid', 'secret', 'country', 'language', 'currency'];
$config->defaults([
'sandbox' => true,
]);
$config['payum.api'] = function (ArrayObject $config) {
$config->validateNotEmpty($config['payum.required_options']);
$config['mode'] = $config['sandbox'] ? Klarna::BETA : Klarna::LIVE;
if (null === $country = KlarnaCountry::fromCode($config['country'])) {
throw new LogicException(sprintf('Given %s country code is not valid. Klarna cannot recognize it.', $config['country']));
}
if (null === $language = KlarnaLanguage::fromCode($config['language'])) {
throw new LogicException(sprintf('Given %s language code is not valid. Klarna cannot recognize it.', $config['language']));
}
if (null === $currency = KlarnaCurrency::fromCode($config['currency'])) {
throw new LogicException(sprintf('Given %s currency code is not valid. Klarna cannot recognize it.', $config['currency']));
}
$klarnaConfig = new Config();
$klarnaConfig->eid = $config['eid'];
$klarnaConfig->secret = $config['secret'];
$klarnaConfig->mode = $config['mode'];
$klarnaConfig->country = $country;
$klarnaConfig->language = $language;
$klarnaConfig->currency = $currency;
$klarnaConfig->pClassStorage = $config['pClassStorage'];
$klarnaConfig->pClassStoragePath = $config['pClassStoragePath'];
$klarnaConfig->xmlRpcVerifyHost = $config['xmlRpcVerifyHost'];
$klarnaConfig->xmlRpcVerifyHost = $config['xmlRpcVerifyHost'];
return $klarnaConfig;
};
}
}
}