Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
PayflowNvp.php
Go to the documentation of this file.
1 <?php
7 
10 
17 {
21  const TRXTYPE_AUTH_ONLY = 'A';
22 
23  const TRXTYPE_SALE = 'S';
24 
25  const TRXTYPE_CREDIT = 'C';
26 
28 
29  const TRXTYPE_DELAYED_VOID = 'V';
30 
36  const TENDER_CC = 'C';
37 
38  const TENDER_PAYPAL = 'P';
39 
45  const EXPRESS_SET = 'S';
46 
47  const EXPRESS_GET = 'G';
48 
49  const EXPRESS_DO_PAYMENT = 'D';
50 
57 
58  const RESPONSE_CODE_FRAUD = 126;
59 
63  protected $_captureTypeComplete = 'Y';
64 
70  protected $_captureTypeNotcomplete = 'N';
71 
77  protected $_globalMap = [
78  // each call
79  'PARTNER' => 'partner',
80  'VENDOR' => 'vendor',
81  'USER' => 'user',
82  'PWD' => 'password',
83  'BUTTONSOURCE' => 'build_notation_code',
84  'TENDER' => 'tender',
85 
86  // commands
87  'RETURNURL' => 'return_url',
88  'CANCELURL' => 'cancel_url',
89  'INVNUM' => 'inv_num',
90  'TOKEN' => 'token',
91  'CORRELATIONID' => 'correlation_id',
92  'CUSTIP' => 'ip_address',
93  'NOTIFYURL' => 'notify_url',
94  'NOTE' => 'note',
95 
96  // style settings
97  'PAGESTYLE' => 'page_style',
98  'HDRIMG' => 'hdrimg',
99  'HDRBORDERCOLOR' => 'hdrbordercolor',
100  'HDRBACKCOLOR' => 'hdrbackcolor',
101  'PAYFLOWCOLOR' => 'payflowcolor',
102  'LOCALECODE' => 'locale_code',
103 
104  // transaction info
105  //We need to store paypal trx id for correct IPN working
106  'PPREF' => 'paypal_transaction_id',
107  'PAYMENTINFO_0_TRANSACTIONID' => 'paypal_transaction_id',
108  'TRANSACTIONID' => 'paypal_transaction_id',
109  'REFUNDTRANSACTIONID' => 'paypal_transaction_id',
110 
111  'PNREF' => 'transaction_id',
112  'ORIGID' => 'authorization_id',
113  'CAPTURECOMPLETE' => 'complete_type',
114  'AMT' => 'amount',
115  'AVSADDR' => 'address_verification',
116  'AVSZIP' => 'postcode_verification',
117 
118  // payment/billing info
119  'CURRENCY' => 'currency_code',
120  'PAYMENTSTATUS' => 'payment_status',
121  'PENDINGREASON' => 'pending_reason',
122  'PAYERID' => 'payer_id',
123  'PAYERSTATUS' => 'payer_status',
124  'EMAIL' => 'email',
125 
126  // backwards compatibility
127  'FIRSTNAME' => 'firstname',
128  'LASTNAME' => 'lastname',
129 
130  // paypal direct credit card information
131  'ACCT' => 'credit_card_number',
132  'EXPDATE' => 'credit_card_expiration_date',
133  'CVV2' => 'credit_card_cvv2',
134  'CARDSTART' => 'maestro_solo_issue_date',
135  'CARDISSUE' => 'maestro_solo_issue_number',
136  'CVV2MATCH' => 'cvv2_check_result',
137 
138  'USERSELECTEDFUNDINGSOURCE' => 'funding_source',
139  ];
140 
146  protected $_debugReplacePrivateDataKeys = ['ACCT', 'EXPDATE', 'CVV2', 'PARTNER', 'USER', 'VENDOR', 'PWD'];
147 
154  'ACCT',
155  'EXPDATE',
156  'CVV2',
157  'CURRENCY',
158  'EMAIL',
159  'TENDER',
160  'NOTIFYURL',
161  'AMT',
162  'CUSTIP',
163  'INVNUM',
164  'CARDISSUE',
165  'CARDSTART',
166  'AUTHSTATUS3DS',
167  'MPIVENDOR3DS',
168  'CAVV',
169  'ECI',
170  'XID',
171  'TAXAMT',
172  'FREIGHTAMT',
173  ];
174 
181  'PNREF',
182  'PPREF',
183  'CORRELATIONID',
184  'CVV2MATCH',
185  'AVSADDR',
186  'AVSZIP',
187  'PENDINGREASON',
188  ];
189 
195  protected $_doCaptureRequest = ['ORIGID', 'CAPTURECOMPLETE', 'AMT', 'TENDER', 'NOTE', 'INVNUM'];
196 
202  protected $_doCaptureResponse = ['PNREF', 'PPREF'];
203 
209  protected $_doVoidRequest = ['ORIGID', 'NOTE', 'TENDER'];
210 
216  protected $_eachCallRequest = ['PARTNER', 'USER', 'VENDOR', 'PWD', 'BUTTONSOURCE'];
217 
223  protected $_refundTransactionRequest = ['ORIGID', 'TENDER'];
224 
230  protected $_refundTransactionResponse = ['PNREF', 'PPREF'];
231 
238  'TENDER',
239  'AMT',
240  'CURRENCY',
241  'RETURNURL',
242  'CANCELURL',
243  'INVNUM',
244  'PAGESTYLE',
245  'HDRIMG',
246  'HDRBORDERCOLOR',
247  'HDRBACKCOLOR',
248  'PAYFLOWCOLOR',
249  'LOCALECODE',
250  'USERSELECTEDFUNDINGSOURCE',
251  ];
252 
258  protected $_setExpressCheckoutResponse = ['REPMSG', 'TOKEN'];
259 
265  protected $_getExpressCheckoutDetailsRequest = ['TENDER', 'TOKEN'];
266 
273  'TENDER',
274  'TOKEN',
275  'PAYERID',
276  'AMT',
277  'CURRENCY',
278  'CUSTIP',
279  'BUTTONSOURCE',
280  'NOTIFYURL',
281  ];
282 
289  'PNREF',
290  'PPREF',
291  'REPMSG',
292  'AMT',
293  'PENDINGREASON',
294  'CVV2MATCH',
295  'AVSADDR',
296  'AVSZIP',
297  'CORRELATIONID',
298  ];
299 
305  protected $_getTransactionDetailsRequest = ['ORIGID', 'TENDER'];
306 
313  'PAYERID',
314  'FIRSTNAME',
315  'LASTNAME',
316  'TRANSACTIONID',
317  'PARENTTRANSACTIONID',
318  'CURRENCYCODE',
319  'AMT',
320  'PAYMENTSTATUS',
321  ];
322 
328  protected $_shippingAddressMap = [
329  'SHIPTOCOUNTRY' => 'country_id',
330  'SHIPTOSTATE' => 'region',
331  'SHIPTOCITY' => 'city',
332  'SHIPTOSTREET' => 'street',
333  'SHIPTOSTREET2' => 'street2',
334  'SHIPTOZIP' => 'postcode',
335  'SHIPTOPHONENUM' => 'telephone',
336  ];
337 
343  protected $_billingAddressMap = [
344  'BUSINESS' => 'company',
345  'NOTETEXT' => 'customer_notes',
346  'EMAIL' => 'email',
347  'FIRSTNAME' => 'firstname',
348  'LASTNAME' => 'lastname',
349  'MIDDLENAME' => 'middlename',
350  'SALUTATION' => 'prefix',
351  'SUFFIX' => 'suffix',
352  'COUNTRYCODE' => 'country_id',
353  'STATE' => 'region',
354  'CITY' => 'city',
355  'STREET' => 'street',
356  'STREET2' => 'street2',
357  'ZIP' => 'postcode',
358  'PHONENUM' => 'telephone',
359  ];
360 
366  protected $_billingAddressMapRequest = ['country_id' => 'COUNTRY'];
367 
373  protected $_lineItemTotalExportMap = [Cart::AMOUNT_TAX => 'TAXAMT', Cart::AMOUNT_SHIPPING => 'FREIGHTAMT'];
374 
381  'amount' => 'PAYMENTREQUEST_%d_ITEMAMT',
382  Cart::AMOUNT_TAX => 'TAXAMT',
383  Cart::AMOUNT_SHIPPING => 'FREIGHTAMT',
384  ];
385 
392  'name' => 'L_NAME%d',
393  'qty' => 'L_QTY%d',
394  'amount' => 'L_COST%d',
395  ];
396 
403  'PAYERID',
404  'CORRELATIONID',
405  'ADDRESSID',
406  'ADDRESSSTATUS',
407  'PAYMENTSTATUS',
408  'PENDINGREASON',
409  'PROTECTIONELIGIBILITY',
410  'EMAIL',
411  ];
412 
419  self::DO_DIRECT_PAYMENT => ['RESULT', 'PNREF', 'PPREF'],
420  ];
421 
425  protected $mathRandom;
426 
430  protected $nvpFactory;
431 
447  public function __construct(
448  \Magento\Customer\Helper\Address $customerAddress,
449  \Psr\Log\LoggerInterface $logger,
451  \Magento\Framework\Locale\ResolverInterface $localeResolver,
452  \Magento\Directory\Model\RegionFactory $regionFactory,
453  \Magento\Directory\Model\CountryFactory $countryFactory,
454  \Magento\Paypal\Model\Api\ProcessableExceptionFactory $processableExceptionFactory,
455  \Magento\Framework\Exception\LocalizedExceptionFactory $frameworkExceptionFactory,
456  \Magento\Framework\HTTP\Adapter\CurlFactory $curlFactory,
457  \Magento\Framework\Math\Random $mathRandom,
458  NvpFactory $nvpFactory,
459  array $data = []
460  ) {
461  parent::__construct(
463  $logger,
465  $localeResolver,
466  $regionFactory,
467  $countryFactory,
468  $processableExceptionFactory,
469  $frameworkExceptionFactory,
470  $curlFactory,
471  $data
472  );
473  $this->mathRandom = $mathRandom;
474  $this->nvpFactory = $nvpFactory;
475  }
476 
482  public function getApiEndpoint()
483  {
484  return sprintf(
485  'https://%spayflowpro.paypal.com/transaction',
486  $this->_config->getValue('sandboxFlag') ? 'pilot-' : ''
487  );
488  }
489 
495  public function getPartner()
496  {
497  return $this->_getDataOrConfig('partner');
498  }
499 
505  public function getUser()
506  {
507  return $this->_getDataOrConfig('user');
508  }
509 
515  public function getPassword()
516  {
517  return $this->_getDataOrConfig('pwd');
518  }
519 
525  public function getVendor()
526  {
527  return $this->_getDataOrConfig('vendor');
528  }
529 
535  public function getTender()
536  {
537  if ($this->_config->getMethodCode() == \Magento\Paypal\Model\Config::METHOD_WPP_PE_EXPRESS) {
538  return self::TENDER_PAYPAL;
539  }
540  return self::TENDER_CC;
541  }
542 
548  public function getPaypalTransactionId()
549  {
550  if ($this->getData('paypal_transaction_id')) {
551  return $this->getData('paypal_transaction_id');
552  }
553  return $this->getTransactionId();
554  }
555 
563  protected function _addMethodToRequest($methodName, $request)
564  {
565  $request['TRXTYPE'] = $this->_mapPaypalMethodName($methodName);
566  if ($this->_getPayflowActionName($methodName) !== null) {
567  $request['ACTION'] = $this->_getPayflowActionName($methodName);
568  }
569  return $request;
570  }
571 
578  protected function _getPayflowActionName($methodName)
579  {
580  switch ($methodName) {
581  case \Magento\Paypal\Model\Api\Nvp::SET_EXPRESS_CHECKOUT:
582  return self::EXPRESS_SET;
583  case \Magento\Paypal\Model\Api\Nvp::GET_EXPRESS_CHECKOUT_DETAILS:
584  return self::EXPRESS_GET;
585  case \Magento\Paypal\Model\Api\Nvp::DO_EXPRESS_CHECKOUT_PAYMENT:
587  }
588  return null;
589  }
590 
597  protected function _mapPaypalMethodName($methodName)
598  {
599  switch ($methodName) {
600  case \Magento\Paypal\Model\Api\Nvp::DO_EXPRESS_CHECKOUT_PAYMENT:
601  case \Magento\Paypal\Model\Api\Nvp::GET_EXPRESS_CHECKOUT_DETAILS:
602  case \Magento\Paypal\Model\Api\Nvp::SET_EXPRESS_CHECKOUT:
603  case \Magento\Paypal\Model\Api\Nvp::DO_DIRECT_PAYMENT:
604  return $this->_config->getValue('payment_action') ==
606  case \Magento\Paypal\Model\Api\Nvp::DO_CAPTURE:
608  case \Magento\Paypal\Model\Api\Nvp::DO_VOID:
610  case \Magento\Paypal\Model\Api\Nvp::REFUND_TRANSACTION:
611  return self::TRXTYPE_CREDIT;
612  }
613  }
614 
621  protected function _isCallSuccessful($response)
622  {
623  $this->_callWarnings = [];
624  if ($response['RESULT'] == self::RESPONSE_CODE_APPROVED) {
625  // collect warnings
626  if (!empty($response['RESPMSG']) && strtoupper($response['RESPMSG']) != 'APPROVED') {
627  $this->_callWarnings[] = $response['RESPMSG'];
628  }
629  return true;
630  }
631  return false;
632  }
633 
641  protected function _handleCallErrors($response)
642  {
643  if ($response['RESULT'] != self::RESPONSE_CODE_APPROVED) {
644  $message = $response['RESPMSG'];
645  $e = new \Exception(sprintf('PayPal gateway errors: %s.', $message));
646  $this->_logger->critical($e);
647  throw new \Magento\Framework\Exception\LocalizedException(
648  __('The PayPal gateway rejected the request. %1', $message)
649  );
650  }
651  }
652 
659  protected function _buildQuery($request)
660  {
661  $result = '';
662  foreach ($request as $k => $v) {
663  $result .= '&' . $k . '=' . $v;
664  }
665  return trim($result, '&');
666  }
667 
673  protected function getRequestId()
674  {
675  return $this->mathRandom->getUniqueHash();
676  }
677 
683  public function callGetTransactionDetails()
684  {
685  }
686 
695  protected function _importFraudFiltersResult(array $from, array $collectedWarnings)
696  {
697  if ($from['RESULT'] != self::RESPONSE_CODE_FRAUD) {
698  return;
699  }
700  $this->setIsPaymentPending(true);
701  }
702 
711  protected function _prepareEachCallRequest($methodName)
712  {
714  }
715 
723  protected function _prepareExpressCheckoutCallRequest(&$requestFields)
724  {
725  return $requestFields;
726  }
727 
735  protected function _postProcessResponse($response)
736  {
737  foreach ($response as $key => $value) {
738  $pos = strpos($key, '[');
739 
740  if ($pos === false) {
741  continue;
742  }
743 
744  unset($response[$key]);
745 
746  if ($pos !== 0) {
747  $modifiedKey = substr($key, 0, $pos);
748  $response[$modifiedKey] = $value;
749  }
750  }
751 
752  return $response;
753  }
754 
762  protected function _exportLineItems(array &$request, $i = 0)
763  {
764  $requestBefore = $request;
765  $result = parent::_exportLineItems($request, $i);
766  if ($this->getIsLineItemsEnabled() && $this->_cart->hasNegativeItemAmount()) {
767  $this->_lineItemTotalExportMap = [
768  Cart::AMOUNT_TAX => 'TAXAMT',
769  Cart::AMOUNT_SHIPPING => 'FREIGHTAMT',
770  'amount' => 'PAYMENTREQUEST_0_ITEMAMT',
771  ];
772  $request = $requestBefore;
773  $result = parent::_exportLineItems($request, $i);
775  $paypalNvp = $this->nvpFactory->create();
776  $this->_doCaptureResponse = $paypalNvp->_doCaptureResponse;
777  $this->_refundTransactionResponse = $paypalNvp->_refundTransactionResponse;
778  $this->_getTransactionDetailsResponse = $paypalNvp->_getTransactionDetailsResponse;
779  $this->_paymentInformationResponse = $paypalNvp->_paymentInformationResponse;
780  $this->_headers[] = 'PAYPAL-NVP: Y';
781  $this->_setSpecificForNegativeLineItems();
782  }
783  return $result;
784  }
785 
790  protected function _setSpecificForNegativeLineItems()
791  {
793  $paypalNvp = $this->nvpFactory->create();
794  $this->_setExpressCheckoutResponse = $paypalNvp->_setExpressCheckoutResponse;
795  $index = array_search('PPREF', $this->_doExpressCheckoutPaymentResponse);
796  if (false !== $index) {
797  unset($this->_doExpressCheckoutPaymentResponse[$index]);
798  }
799  $this->_doExpressCheckoutPaymentResponse[] = 'PAYMENTINFO_0_TRANSACTIONID';
800  $this->_requiredResponseParams[self::DO_EXPRESS_CHECKOUT_PAYMENT][] = 'PAYMENTINFO_0_TRANSACTIONID';
801  }
802 }
__construct(\Magento\Customer\Helper\Address $customerAddress, \Psr\Log\LoggerInterface $logger, Logger $customLogger, \Magento\Framework\Locale\ResolverInterface $localeResolver, \Magento\Directory\Model\RegionFactory $regionFactory, \Magento\Directory\Model\CountryFactory $countryFactory, \Magento\Paypal\Model\Api\ProcessableExceptionFactory $processableExceptionFactory, \Magento\Framework\Exception\LocalizedExceptionFactory $frameworkExceptionFactory, \Magento\Framework\HTTP\Adapter\CurlFactory $curlFactory, \Magento\Framework\Math\Random $mathRandom, NvpFactory $nvpFactory, array $data=[])
Definition: PayflowNvp.php:447
$response
Definition: 404.php:11
_exportLineItems(array &$request, $i=0)
Definition: Nvp.php:1450
getData($key='', $index=null)
Definition: DataObject.php:119
__()
Definition: __.php:13
$message
$logger
$value
Definition: gender.phtml:16
_importFraudFiltersResult(array $from, array $collectedWarnings)
Definition: PayflowNvp.php:695
$pos
Definition: list.phtml:42
const DO_EXPRESS_CHECKOUT_PAYMENT
Definition: Nvp.php:40
_addMethodToRequest($methodName, $request)
Definition: PayflowNvp.php:563
$i
Definition: gallery.phtml:31
$index
Definition: list.phtml:44
_prepareExpressCheckoutCallRequest(&$requestFields)
Definition: PayflowNvp.php:723
_getDataOrConfig($key, $default=null)
$customerAddress