Passed
Push — main ( 67f80d...eb72a4 )
by Leith
05:54
created

PurchaseRequest::setTransactionChannel()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 3
nc 2
nop 1
dl 0
loc 6
rs 10
c 0
b 0
f 0
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
    /**
81
     * Transaction channel can only be either 'ECOMMERCE' or 'MOTO'
82
     */
83
    public function setTransactionChannel($value)
84
    {
85
        if (!in_array($value, ['ECOMMERCE', 'MOTO'])) {
86
            $value = null;
87
        }
88
        return $this->setParameter('transactionChannel', $value);
89
    }
90
91
    public function getData()
92
    {
93
        $this->validate('merchantId', 'amount', 'currency');
94
95
        $formattedItems = [];
96
        $items = $this->getItems();
97
        if ($items) {
98
            foreach ($items as $item) {
99
                $itemPrice = $this->getItemPriceInteger($item);
100
                $formattedItems[] = [
101
                    'amountOfMoney' => [
102
                        'amount' => $item->getQuantity() * $itemPrice,
103
                        'currencyCode' => $this->getCurrency(),
104
                    ],
105
                    'orderLineDetails' => [
106
                        'productName' => $item->getName(),
107
                        'productPrice' => $itemPrice,
108
                        'quantity' => (int) $item->getQuantity(),
109
                    ],
110
                ];
111
            }
112
        }
113
114
        $data = [
115
            'cardPaymentMethodSpecificInput' => [
116
                'authorizationMode' => $this->authorizationMode ?? 'SALE',
117
                'transactionChannel' => $this->getTransactionChannel() ?? 'ECOMMERCE',
118
            ],
119
            'hostedCheckoutSpecificInput' => [
120
                // if adding locale, validate locale against known formats
121
                // @see https://docs.direct.worldline-solutions.com/en/integration/basic-integration-methods/hosted-checkout-page#chooselanguageversion
122
                // 'locale' => 'en_UK',
123
                'returnUrl' => $this->getReturnUrl(),
124
            ],
125
            'order' => [
126
                'amountOfMoney' => [
127
                    'amount' => $this->getAmountInteger(),
128
                    'currencyCode' => $this->getCurrency(),
129
                ],
130
                'references' => [
131
                    'descriptor' => $this->getMerchantName(),
132
                    'merchantReference' => $this->getTransactionId(),
133
                ],
134
                'shoppingCart' => [
135
                    'items' => $formattedItems,
136
                ],
137
            ],
138
        ];
139
140
        if ($this->getAvailablePaymentProducts() !== null) {
141
            if (!isset($data['hostedCheckoutSpecificInput']['paymentProductFilters'])) {
142
                $data['hostedCheckoutSpecificInput']['paymentProductFilters'] = [];
143
            }
144
            $data['hostedCheckoutSpecificInput']['paymentProductFilters']['restrictTo'] = [
145
                'products' => $this->getAvailablePaymentProducts(),
146
            ];
147
        }
148
149
        if ($this->getExcludedPaymentProducts() !== null) {
150
            if (!isset($data['hostedCheckoutSpecificInput']['paymentProductFilters'])) {
151
                $data['hostedCheckoutSpecificInput']['paymentProductFilters'] = [];
152
            }
153
            $data['hostedCheckoutSpecificInput']['paymentProductFilters']['exclude'] = [
154
                'products' => $this->getExcludedPaymentProducts(),
155
            ];
156
        }
157
158
        if ($this->getShowResultPage() !== null) {
159
            $data['hostedCheckoutSpecificInput']['showResultPage'] = (bool) $this->getShowResultPage();
160
        }
161
162
        if ($this->getSessionTimeout() !== null) {
163
            $data['hostedCheckoutSpecificInput']['sessionTimeout'] = (int) $this->getSessionTimeout();
164
        }
165
166
        if ($this->getNotifyUrl() !== null) {
0 ignored issues
show
introduced by
The condition $this->getNotifyUrl() !== null is always true.
Loading history...
167
            $data['feedbacks'] = [
168
                'webhookUrl' => $this->getNotifyUrl(),
169
            ];
170
        }
171
172
        return $data;
173
    }
174
175
    protected function createResponse($data)
176
    {
177
        return $this->response = new PurchaseResponse($this, json_decode($data));
178
    }
179
180
    protected function getAction()
181
    {
182
        return '/v2/'.$this->getMerchantId().'/hostedcheckouts';
183
    }
184
}
185