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
8 
10 
15 {
19  protected $_transKey = null;
20 
27  protected function _getTransactionKey()
28  {
29  return $this->_transKey;
30  }
31 
39  protected function _setTransactionKey($transKey)
40  {
41  $this->_transKey = $transKey;
42  return $this;
43  }
44 
56  public function generateRequestSign(
57  $merchantApiLoginId,
58  $merchantTransactionKey,
59  $amount,
60  $currencyCode,
61  $fpSequence,
62  $fpTimestamp
63  ) {
64  return hash_hmac(
65  "md5",
66  $merchantApiLoginId . "^" . $fpSequence . "^" . $fpTimestamp . "^" . $amount . "^" . $currencyCode,
67  $merchantTransactionKey
68  );
69  }
70 
77  public function setConstantData(\Magento\Authorizenet\Model\Directpost $paymentMethod)
78  {
79  $this->setXVersion('3.1')->setXDelimData('FALSE')->setXRelayResponse('TRUE');
80 
81  $this->setXTestRequest($paymentMethod->getConfigData('test') ? 'TRUE' : 'FALSE');
82 
83  $this->setXLogin($paymentMethod->getConfigData('login'))
85  ->setXRelayUrl($paymentMethod->getRelayUrl());
86 
87  $this->_setTransactionKey($paymentMethod->getConfigData('trans_key'));
88  return $this;
89  }
90 
98  public function setDataFromOrder(
99  \Magento\Sales\Model\Order $order,
100  \Magento\Authorizenet\Model\Directpost $paymentMethod
101  ) {
102  $payment = $order->getPayment();
103 
104  $this->setXType($payment->getAnetTransType());
105  $this->setXFpSequence($order->getQuoteId());
106  $this->setXInvoiceNum($order->getIncrementId());
107  $this->setXAmount($payment->getBaseAmountAuthorized());
108  $this->setXCurrencyCode($order->getBaseCurrencyCode());
109  $this->setXTax(
110  sprintf('%.2F', $order->getBaseTaxAmount())
111  )->setXFreight(
112  sprintf('%.2F', $order->getBaseShippingAmount())
113  );
114 
115  //need to use (string) because NULL values IE6-8 decodes as "null" in JSON in JavaScript,
116  //but we need "" for null values.
117  $billing = $order->getBillingAddress();
118  if (!empty($billing)) {
119  $this->setXFirstName((string)$billing->getFirstname())
120  ->setXLastName((string)$billing->getLastname())
121  ->setXCompany((string)$billing->getCompany())
122  ->setXAddress((string)$billing->getStreetLine(1))
123  ->setXCity((string)$billing->getCity())
124  ->setXState((string)$billing->getRegion())
125  ->setXZip((string)$billing->getPostcode())
126  ->setXCountry((string)$billing->getCountryId())
127  ->setXPhone((string)$billing->getTelephone())
128  ->setXFax((string)$billing->getFax())
129  ->setXCustId((string)$billing->getCustomerId())
130  ->setXCustomerIp((string)$order->getRemoteIp())
131  ->setXCustomerTaxId((string)$billing->getTaxId())
132  ->setXEmail((string)$order->getCustomerEmail())
133  ->setXEmailCustomer((string)$paymentMethod->getConfigData('email_customer'))
134  ->setXMerchantEmail((string)$paymentMethod->getConfigData('merchant_email'));
135  }
136 
137  $shipping = $order->getShippingAddress();
138  if (!empty($shipping)) {
139  $this->setXShipToFirstName(
140  (string)$shipping->getFirstname()
141  )->setXShipToLastName(
142  (string)$shipping->getLastname()
143  )->setXShipToCompany(
144  (string)$shipping->getCompany()
145  )->setXShipToAddress(
146  (string)$shipping->getStreetLine(1)
147  )->setXShipToCity(
148  (string)$shipping->getCity()
149  )->setXShipToState(
150  (string)$shipping->getRegion()
151  )->setXShipToZip(
152  (string)$shipping->getPostcode()
153  )->setXShipToCountry(
154  (string)$shipping->getCountryId()
155  );
156  }
157 
158  $this->setXPoNum((string)$payment->getPoNumber());
159 
160  return $this;
161  }
162 
169  public function signRequestData()
170  {
171  $fpTimestamp = time();
172  $hash = $this->generateRequestSign(
173  $this->getXLogin(),
174  $this->_getTransactionKey(),
175  $this->getXAmount(),
176  $this->getXCurrencyCode(),
177  $this->getXFpSequence(),
178  $fpTimestamp
179  );
180  $this->setXFpTimestamp($fpTimestamp);
181  $this->setXFpHash($hash);
182  return $this;
183  }
184 }
generateRequestSign( $merchantApiLoginId, $merchantTransactionKey, $amount, $currencyCode, $fpSequence, $fpTimestamp)
Definition: Request.php:56
$order
Definition: order.php:55
setDataFromOrder(\Magento\Sales\Model\Order $order, \Magento\Authorizenet\Model\Directpost $paymentMethod)
Definition: Request.php:98
$amount
Definition: order.php:14
$payment
Definition: order.php:17
setConstantData(\Magento\Authorizenet\Model\Directpost $paymentMethod)
Definition: Request.php:77