Passed
Pull Request — main (#2)
by Leith
02:47
created

PurchaseRequest::setTokenize()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 1
c 0
b 0
f 0
nc 1
nop 1
dl 0
loc 3
rs 10
1
<?php
2
3
namespace Omnipay\Worldline\Message;
4
5
/**
6
 * Worldline Purchase Request
7
 *
8
 * @see https://docs.direct.worldline-solutions.com/en/api-reference#tag/HostedCheckout/operation/CreateHostedCheckoutApi
9
 */
10
class PurchaseRequest extends AbstractRequest
11
{
12
    /** @var string  Can be "FINAL_AUTHORIZATION" "PRE_AUTHORIZATION" or "SALE" */
13
    protected $authorizationMode = 'SALE';
14
    protected $requestMethod = 'POST';
15
16
    public function getAvailablePaymentProducts()
17
    {
18
        return $this->getParameter('availablePaymentProducts');
19
    }
20
21
    /**
22
     * @param int[] $value  @see https://docs.direct.worldline-solutions.com/en/payment-methods-and-features/
23
     */
24
    public function setAvailablePaymentProducts($value)
25
    {
26
        return $this->setParameter('availablePaymentProducts', $value);
27
    }
28
29
    public function getExcludedPaymentProducts()
30
    {
31
        return $this->getParameter('excludedPaymentProducts');
32
    }
33
34
    /**
35
     * @param int[] $value  @see https://docs.direct.worldline-solutions.com/en/payment-methods-and-features/
36
     */
37
    public function setExcludedPaymentProducts($value)
38
    {
39
        return $this->setParameter('excludedPaymentProducts', $value);
40
    }
41
42
    public function getMerchantName()
43
    {
44
        return $this->getParameter('merchantName');
45
    }
46
47
    public function setMerchantName($value)
48
    {
49
        return $this->setParameter('merchantName', $value);
50
    }
51
52
    public function getShowResultPage()
53
    {
54
        return $this->getParameter('showResultPage');
55
    }
56
57
    public function setShowResultPage($value)
58
    {
59
        return $this->setParameter('showResultPage', $value);
60
    }
61
62
    public function getSessionTimeout()
63
    {
64
        return $this->getParameter('sessionTimeout');
65
    }
66
67
    /**
68
     * Timeout is in minutes, default 180
69
     */
70
    public function setSessionTimeout($value)
71
    {
72
        return $this->setParameter('sessionTimeout', $value);
73
    }
74
75
    public function getTransactionChannel()
76
    {
77
        return $this->getParameter('transactionChannel');
78
    }
79
80
    public function setTokenize($value)
81
    {
82
        return $this->setParameter('tokenize', $value);
83
    }
84
85
    public function getTokenize()
86
    {
87
        return $this->getParameter('tokenize');
88
    }
89
90
    /**
91
     * Transaction channel can only be either 'ECOMMERCE' or 'MOTO'
92
     */
93
    public function setTransactionChannel($value)
94
    {
95
        if (!in_array($value, ['ECOMMERCE', 'MOTO'])) {
96
            $value = null;
97
        }
98
        return $this->setParameter('transactionChannel', $value);
99
    }
100
101
    public function getData()
102
    {
103
        $this->validate('merchantId', 'amount', 'currency');
104
105
        $formattedItems = [];
106
        $items = $this->getItems();
107
        if ($items) {
108
            foreach ($items as $item) {
109
                $itemPrice = $this->getItemPriceInteger($item);
110
                $formattedItems[] = [
111
                    'amountOfMoney' => [
112
                        'amount' => $item->getQuantity() * $itemPrice,
113
                        'currencyCode' => $this->getCurrency(),
114
                    ],
115
                    'orderLineDetails' => [
116
                        'productName' => $item->getName(),
117
                        'productPrice' => $itemPrice,
118
                        'quantity' => (int) $item->getQuantity(),
119
                    ],
120
                ];
121
            }
122
        }
123
124
        $data = [
125
            'cardPaymentMethodSpecificInput' => [
126
                'authorizationMode' => $this->authorizationMode ?? 'SALE',
127
                'tokenize' => (bool) $this->getTokenize(),
128
                'transactionChannel' => $this->getTransactionChannel() ?? 'ECOMMERCE',
129
            ],
130
            'hostedCheckoutSpecificInput' => [
131
                // if adding locale, validate locale against known formats
132
                // @see https://docs.direct.worldline-solutions.com/en/integration/basic-integration-methods/hosted-checkout-page#chooselanguageversion
133
                // 'locale' => 'en_UK',
134
                'returnUrl' => $this->getReturnUrl(),
135
            ],
136
            'order' => [
137
                'amountOfMoney' => [
138
                    'amount' => $this->getAmountInteger(),
139
                    'currencyCode' => $this->getCurrency(),
140
                ],
141
                'references' => [
142
                    'descriptor' => $this->getMerchantName(),
143
                    'merchantReference' => $this->getTransactionId(),
144
                ],
145
                'shoppingCart' => [
146
                    'items' => $formattedItems,
147
                ],
148
            ],
149
        ];
150
151
        if ($this->getAvailablePaymentProducts() !== null) {
152
            if (!isset($data['hostedCheckoutSpecificInput']['paymentProductFilters'])) {
153
                $data['hostedCheckoutSpecificInput']['paymentProductFilters'] = [];
154
            }
155
            $data['hostedCheckoutSpecificInput']['paymentProductFilters']['restrictTo'] = [
156
                'products' => $this->getAvailablePaymentProducts(),
157
            ];
158
        }
159
160
        if ($this->getExcludedPaymentProducts() !== null) {
161
            if (!isset($data['hostedCheckoutSpecificInput']['paymentProductFilters'])) {
162
                $data['hostedCheckoutSpecificInput']['paymentProductFilters'] = [];
163
            }
164
            $data['hostedCheckoutSpecificInput']['paymentProductFilters']['exclude'] = [
165
                'products' => $this->getExcludedPaymentProducts(),
166
            ];
167
        }
168
169
        if ($this->getShowResultPage() !== null) {
170
            $data['hostedCheckoutSpecificInput']['showResultPage'] = (bool) $this->getShowResultPage();
171
        }
172
173
        if ($this->getSessionTimeout() !== null) {
174
            $data['hostedCheckoutSpecificInput']['sessionTimeout'] = (int) $this->getSessionTimeout();
175
        }
176
177
        if ($this->getToken() !== null || $this->getCardReference() !== null) {
0 ignored issues
show
introduced by
The condition $this->getToken() !== null is always true.
Loading history...
178
            $data['cardPaymentMethodSpecificInput']['token'] = $this->getToken() ?? $this->getCardReference();
179
        }
180
181
        if ($this->getNotifyUrl() !== null) {
0 ignored issues
show
introduced by
The condition $this->getNotifyUrl() !== null is always true.
Loading history...
182
            $data['feedbacks'] = [
183
                'webhookUrl' => $this->getNotifyUrl(),
184
            ];
185
        }
186
187
        return $data;
188
    }
189
190
    protected function createResponse($data)
191
    {
192
        return $this->response = new PurchaseResponse($this, json_decode($data));
193
    }
194
195
    protected function getAction()
196
    {
197
        return '/v2/'.$this->getMerchantId().'/hostedcheckouts';
198
    }
199
}
200