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 | 'tokenize' => (bool) $this->getCreateToken(), |
||
118 | 'transactionChannel' => $this->getTransactionChannel() ?? 'ECOMMERCE', |
||
119 | ], |
||
120 | 'hostedCheckoutSpecificInput' => [ |
||
121 | // if adding locale, validate locale against known formats |
||
122 | // @see https://docs.direct.worldline-solutions.com/en/integration/basic-integration-methods/hosted-checkout-page#chooselanguageversion |
||
123 | // 'locale' => 'en_UK', |
||
124 | 'returnUrl' => $this->getReturnUrl(), |
||
125 | ], |
||
126 | 'order' => [ |
||
127 | 'amountOfMoney' => [ |
||
128 | 'amount' => $this->getAmountInteger(), |
||
129 | 'currencyCode' => $this->getCurrency(), |
||
130 | ], |
||
131 | 'references' => [ |
||
132 | 'descriptor' => $this->getMerchantName(), |
||
133 | 'merchantReference' => $this->getTransactionId(), |
||
134 | ], |
||
135 | 'shoppingCart' => [ |
||
136 | 'items' => $formattedItems, |
||
137 | ], |
||
138 | ], |
||
139 | ]; |
||
140 | |||
141 | if ($this->getAvailablePaymentProducts() !== null) { |
||
142 | if (!isset($data['hostedCheckoutSpecificInput']['paymentProductFilters'])) { |
||
143 | $data['hostedCheckoutSpecificInput']['paymentProductFilters'] = []; |
||
144 | } |
||
145 | $data['hostedCheckoutSpecificInput']['paymentProductFilters']['restrictTo'] = [ |
||
146 | 'products' => $this->getAvailablePaymentProducts(), |
||
147 | ]; |
||
148 | } |
||
149 | |||
150 | if ($this->getExcludedPaymentProducts() !== null) { |
||
151 | if (!isset($data['hostedCheckoutSpecificInput']['paymentProductFilters'])) { |
||
152 | $data['hostedCheckoutSpecificInput']['paymentProductFilters'] = []; |
||
153 | } |
||
154 | $data['hostedCheckoutSpecificInput']['paymentProductFilters']['exclude'] = [ |
||
155 | 'products' => $this->getExcludedPaymentProducts(), |
||
156 | ]; |
||
157 | } |
||
158 | |||
159 | if ($this->getShowResultPage() !== null) { |
||
160 | $data['hostedCheckoutSpecificInput']['showResultPage'] = (bool) $this->getShowResultPage(); |
||
161 | } |
||
162 | |||
163 | if ($this->getSessionTimeout() !== null) { |
||
164 | $data['hostedCheckoutSpecificInput']['sessionTimeout'] = (int) $this->getSessionTimeout(); |
||
165 | } |
||
166 | |||
167 | if ($this->getToken() !== null || $this->getCardReference() !== null) { |
||
0 ignored issues
–
show
introduced
by
![]() |
|||
168 | $data['cardPaymentMethodSpecificInput']['token'] = $this->getToken() ?? $this->getCardReference(); |
||
169 | } |
||
170 | |||
171 | if ($this->getNotifyUrl() !== null) { |
||
0 ignored issues
–
show
|
|||
172 | $data['feedbacks'] = [ |
||
173 | 'webhookUrl' => $this->getNotifyUrl(), |
||
174 | ]; |
||
175 | } |
||
176 | |||
177 | return $data; |
||
178 | } |
||
179 | |||
180 | protected function createResponse($data) |
||
181 | { |
||
182 | return $this->response = new PurchaseResponse($this, json_decode($data)); |
||
183 | } |
||
184 | |||
185 | protected function getAction() |
||
186 | { |
||
187 | return '/v2/'.$this->getMerchantId().'/hostedcheckouts'; |
||
188 | } |
||
189 | } |
||
190 |