BxPaymentProvider2Checkout.php 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  1. <?php defined('BX_DOL') or die('hack attempt');
  2. /**
  3. * Copyright (c) UNA, Inc - https://una.io
  4. * MIT License - https://opensource.org/licenses/MIT
  5. *
  6. * @defgroup Payment Payment
  7. * @ingroup UnaModules
  8. *
  9. * @{
  10. */
  11. define('TUCO_MODE_LIVE', 1);
  12. define('TUCO_MODE_TEST', 2);
  13. define('TUCO_PAYMENT_METHOD_CC', 'CC');
  14. define('TUCO_PAYMENT_METHOD_CK', 'CK');
  15. define('TUCO_PAYMENT_METHOD_AL', 'AL');
  16. define('TUCO_PAYMENT_METHOD_PPI', 'PPI');
  17. class BxPaymentProvider2Checkout extends BxBaseModPaymentProvider implements iBxBaseModPaymentProvider
  18. {
  19. protected $_sDataReturnUrl;
  20. function __construct($aConfig)
  21. {
  22. $this->MODULE = 'bx_payment';
  23. parent::__construct($aConfig);
  24. $this->_bRedirectOnResult = true;
  25. $this->_sDataReturnUrl = $this->_oModule->_oConfig->getUrl('URL_RETURN_DATA') . $this->_sName . '/';
  26. }
  27. public function initializeCheckout($iPendingId, $aCartInfo)
  28. {
  29. $bTest = (int)$this->getOption('mode') == TUCO_MODE_TEST;
  30. $aFormData = array(
  31. 'sid' => $this->getOption('account_id'),
  32. 'mode' => '2CO',
  33. 'demo' => $bTest ? 'Y' : '',
  34. 'merchant_order_id' => $iPendingId,
  35. 'total' => sprintf("%.2f", (float)$aCartInfo['items_price']),
  36. 'currency_code' => $aCartInfo['vendor_currency_code'],
  37. 'pay_method' => $this->getOption('payment_method'),
  38. 'x_receipt_link_url' => $this->_sDataReturnUrl . $aCartInfo['vendor_id']
  39. );
  40. $iIndex = 0;
  41. $sPriceKey = $this->_oModule->_oConfig->getKey('KEY_ARRAY_PRICE_SINGLE');
  42. foreach($aCartInfo['items'] as $aItem) {
  43. $aFormData['li_' . $iIndex . '_type'] = 'product';
  44. $aFormData['li_' . $iIndex . '_name'] = $aItem['title'];
  45. $aFormData['li_' . $iIndex . '_price'] = $aItem[$sPriceKey];
  46. $aFormData['li_' . $iIndex . '_quantity'] = $aItem['quantity'];
  47. $aFormData['li_' . $iIndex . '_tangible'] = 'N';
  48. $iIndex++;
  49. }
  50. $sActionURL = 'https://' . ($bTest ? 'sandbox' : 'www') . '.2checkout.com/checkout/purchase';
  51. $this->_oModule->_oTemplate->displayPageCodeRedirect($sActionURL, $aFormData);
  52. exit;
  53. }
  54. public function finalizeCheckout(&$aData)
  55. {
  56. return $this->_registerCheckout($aData);
  57. }
  58. /**
  59. *
  60. * @param $aData - data from payment provider.
  61. * @param $bSubscription - Is not needed. May be used in the future for subscriptions.
  62. * @param $iPendingId - Is not needed. May be used in the future for subscriptions.
  63. * @return array with results.
  64. */
  65. protected function _registerCheckout(&$aData, $bSubscription = false, $iPendingId = 0)
  66. {
  67. if(empty($this->_aOptions) && isset($aData['merchant_order_id']))
  68. $this->_aOptions = $this->getOptionsByPending($aData['merchant_order_id']);
  69. if(empty($this->_aOptions))
  70. return array('code' => 2, 'message' => _t('_payment_2co_err_no_vendor_given'));
  71. $aResult = $this->_validateCheckout($aData);
  72. if(empty($aResult['pending_id']))
  73. return $aResult;
  74. $aPending = $this->_oModule->_oDb->getOrderPending(array('type' => 'id', 'id' => (int)$aResult['pending_id']));
  75. if(!empty($aPending['order']) || !empty($aPending['error_code']) || !empty($aPending['error_msg']) || (int)$aPending['processed'] != 0)
  76. return array('code' => 6, 'message' => _t('_payment_2co_err_already_processed'));
  77. $this->_oModule->_oDb->updateOrderPending((int)$aResult['pending_id'], array(
  78. 'order' => $aData['order_number'],
  79. 'error_code' => $aResult['code'],
  80. 'error_msg' => $aResult['message']
  81. ));
  82. return $aResult;
  83. }
  84. protected function _validateCheckout(&$aData)
  85. {
  86. if(empty($aData['order_number']) || empty($aData['total']) || empty($aData['key']) || empty($aData['merchant_order_id']))
  87. return array('code' => 3, 'message' => _t('_payment_2co_err_no_data_given'));
  88. $sOrder = bx_process_input($aData['order_number'], BX_TAGS_STRIP);
  89. $sAmount = bx_process_input($aData['total'], BX_TAGS_STRIP);
  90. $iPendingId = (int)$aData['merchant_order_id'];
  91. if($aData['key'] != $this->_generateKey($sOrder, $sAmount))
  92. return array('code' => 4, 'message' => _t('_payment_2co_err_wrong_key'), 'pending_id' => $iPendingId);
  93. $aPending = $this->_oModule->_oDb->getOrderPending(array('type' => 'id', 'id' => $iPendingId));
  94. if((float)$sAmount != (float)$aPending['amount'])
  95. return array('code' => 5, 'message' => _t('_payment_2co_err_wrong_payment'), 'pending_id' => $iPendingId);
  96. return array(
  97. 'code' => BX_PAYMENT_RESULT_SUCCESS,
  98. 'message' => _t('_payment_2co_msg_verified'),
  99. 'pending_id' => $iPendingId,
  100. 'client_name' => _t('_payment_txt_buyer_name_mask', bx_process_input($aData['first_name']), bx_process_input($aData['last_name'])),
  101. 'client_email' => bx_process_input($aData['email']),
  102. 'paid' => true
  103. );
  104. }
  105. protected function _generateKey($sOrder, $sAmount)
  106. {
  107. if((int)$this->getOption('mode') == TUCO_MODE_TEST)
  108. $sOrder = '1';
  109. $sKey = $this->getOption('secret_word') . $this->getOption('account_id') . $sOrder . $sAmount;
  110. return strtoupper(md5($sKey));
  111. }
  112. }
  113. /** @} */