Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
Request.php
Go to the documentation of this file.
1 <?php
7 
11 use Magento\Payment\Helper\Formatter;
15 
21 class Request extends DataObject
22 {
23  use Formatter;
24 
30  protected $order;
31 
37  protected $paymentMethod;
38 
44  protected $buttonVarFormat = 'L_BUTTONVAR%d';
45 
51  protected $notButtonVars = ['METHOD', 'BUTTONCODE', 'BUTTONTYPE'];
52 
58  protected $customerAddress = null;
59 
65  protected $taxData;
66 
72  protected $localeResolver;
73 
80  public function __construct(
83  Data $taxData,
84  array $data = []
85  ) {
86  $this->customerAddress = $customerAddress;
87  $this->localeResolver = $localeResolver;
88  $this->taxData = $taxData;
89  parent::__construct($data);
90  }
91 
97  public function getRequestData()
98  {
99  $requestData = [];
100  if (!empty($this->_data)) {
101  // insert params to request as additional button variables,
102  // except special params from _notButtonVars list
103  $i = 0;
104  foreach ($this->_data as $key => $value) {
105  if (in_array($key, $this->notButtonVars)) {
106  $requestData[$key] = $value;
107  } else {
108  $varKey = sprintf($this->buttonVarFormat, $i);
109  $requestData[$varKey] = $key . '=' . $value;
110  $i++;
111  }
112  }
113  }
114 
115  return $requestData;
116  }
117 
125  {
126  $this->paymentMethod = $paymentMethod;
128  $this->addData($requestData);
129 
130  return $this;
131  }
132 
139  public function setOrder(Order $order)
140  {
141  $this->order = $order;
142  $requestData = $this->getOrderData($order);
143  $this->addData($requestData);
144 
145  return $this;
146  }
147 
155  public function setAmount(Order $order)
156  {
157  $this->addData($this->getAmountData($order));
158  return $this;
159  }
160 
167  protected function getAmountData(Order $order)
168  {
169  // if tax is included - need add to request only total amount
170  if ($this->taxData->getConfig()->priceIncludesTax()) {
171  return $this->getTaxableAmount($order);
172  } else {
173  return $this->getNonTaxableAmount($order);
174  }
175  }
176 
182  private function getNonTaxableAmount(Order $order)
183  {
184  // PayPal denied transaction with 0 amount
185  $subtotal = $order->getBaseSubtotal() ? : $order->getPayment()->getBaseAmountAuthorized();
186 
187  return [
188  'subtotal' => $this->formatPrice($subtotal),
189  'total' => $this->formatPrice($order->getPayment()->getBaseAmountAuthorized()),
190  'tax' => $this->formatPrice($order->getBaseTaxAmount()),
191  'shipping' => $this->formatPrice($order->getBaseShippingAmount()),
192  'discount' => $this->formatPrice(abs($order->getBaseDiscountAmount()))
193  ];
194  }
195 
201  private function getTaxableAmount(Order $order)
202  {
203  $amount = $this->formatPrice($order->getPayment()->getBaseAmountAuthorized());
204 
205  return [
206  'amount' => $amount,
207  'subtotal' => $amount // subtotal always is required
208  ];
209  }
210 
218  {
219  $request = [
220  'paymentaction' => strtolower($paymentMethod->getConfigData('payment_action')),
221  'notify_url' => $paymentMethod->getNotifyUrl(),
222  'cancel_return' => $paymentMethod->getCancelUrl(),
223  'return' => $paymentMethod->getReturnUrl(),
224  'lc' => \Locale::getRegion($this->localeResolver->getLocale()),
225  'template' => 'mobile-iframe',
226  'showBillingAddress' => 'false',
227  'showShippingAddress' => 'true',
228  'showBillingEmail' => 'false',
229  'showBillingPhone' => 'false',
230  'showCustomerName' => 'false',
231  'showCardInfo' => 'true',
232  'showHostedThankyouPage' => 'false',
233  ];
234 
235  return $request;
236  }
237 
244  protected function getOrderData(Order $order)
245  {
246  $request = [
247  'invoice' => $order->getIncrementId(),
248  'address_override' => 'true',
249  'currency_code' => $order->getBaseCurrencyCode(),
250  'buyer_email' => $order->getCustomerEmail(),
251  ];
252 
253  // append to request billing address data
254  if ($billingAddress = $order->getBillingAddress()) {
255  $request = array_merge($request, $this->getAddress($billingAddress, 'billing'));
256  }
257 
258  // append to request shipping address data
259  if ($shippingAddress = $order->getShippingAddress()) {
260  $request = array_merge($request, $this->getAddress($shippingAddress));
261  }
262 
263  return $request;
264  }
265 
273  protected function getAddress(DataObject $address, $type = '')
274  {
275  $type = !empty($type) ? $type . '_' : '';
276  $request = [
277  $type . 'first_name' => $address->getFirstname(),
278  $type . 'last_name' => $address->getLastname(),
279  $type . 'city' => $address->getCity(),
280  $type . 'state' => $this->getRegion($address),
281  $type . 'zip' => $address->getPostcode(),
282  $type . 'country' => $address->getCountryId(),
283  ];
284 
285  $streets = $this->getAddressStreets($address);
286  $request[$type . 'address1'] = $streets[0];
287  $request[$type . 'address2'] = $streets[1];
288 
289  return $request;
290  }
291 
298  protected function getRegion(DataObject $address)
299  {
300  // get region code, otherwise - region, otherwise - city
301  return $address->getRegionCode() ?: ($address->getRegion() ?: $address->getCity());
302  }
303 
311  {
312  $street1 = '';
313  $street2 = '';
314  $data = $this->customerAddress->convertStreetLines($address->getStreet(), 2);
315  if (!empty($data[0])) {
316  $street1 = $data[0];
317  }
318  if (!empty($data[1])) {
319  $street2 = $data[1];
320  }
321  return [$street1, $street2];
322  }
323 }
$billingAddress
Definition: order.php:25
$shippingAddress
Definition: order.php:40
$address
Definition: customer.php:38
$amount
Definition: order.php:14
$type
Definition: item.phtml:13
getAddress(DataObject $address, $type='')
Definition: Request.php:273
__construct(Resolver $localeResolver, Address $customerAddress, Data $taxData, array $data=[])
Definition: Request.php:80
$value
Definition: gender.phtml:16
getRegion(DataObject $address)
Definition: Request.php:298
getAddressStreets(DataObject $address)
Definition: Request.php:310
$i
Definition: gallery.phtml:31
getPaymentData(Hostedpro $paymentMethod)
Definition: Request.php:217