BxPaymentProviderStripe.php 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588
  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. require_once('BxPaymentProviderStripeBasic.php');
  12. class BxPaymentProviderStripe extends BxPaymentProviderStripeBasic implements iBxBaseModPaymentProvider
  13. {
  14. function __construct($aConfig)
  15. {
  16. parent::__construct($aConfig);
  17. $this->_aIncludeJs = array(
  18. 'https://checkout.stripe.com/checkout.js',
  19. 'stripe.js'
  20. );
  21. }
  22. public function initializeCheckout($iPendingId, $aCartInfo, $sRedirect = '')
  23. {
  24. $sToken = bx_process_input(bx_get('token'));
  25. if(empty($aCartInfo['items']) || !is_array($aCartInfo['items']))
  26. return $this->_sLangsPrefix . 'err_empty_items';
  27. $aClient = $this->_oModule->getProfileInfo();
  28. $aVendor = $this->_oModule->getProfileInfo($aCartInfo['vendor_id']);
  29. $aPending = $this->_oModule->_oDb->getOrderPending(array('type' => 'id', 'id' => $iPendingId));
  30. if(!empty($aPending['order']) || !empty($aPending['error_code']) || !empty($aPending['error_msg']) || (int)$aPending['processed'] != 0)
  31. return $this->_sLangsPrefix . 'err_already_processed';
  32. switch($aPending['type']) {
  33. case BX_PAYMENT_TYPE_SINGLE:
  34. $aCartInfo['items_title'] = '';
  35. foreach($aCartInfo['items'] as $aItem)
  36. $aCartInfo['items_title'] .= ' ' . $aItem['title'] . ',';
  37. $aCartInfo['items_title'] = trim($aCartInfo['items_title'], ', ');
  38. $mixedResult = $this->_createCharge($sToken, $iPendingId, $aClient, $aCartInfo);
  39. if($mixedResult === false)
  40. return $this->_sLangsPrefix . 'err_cannot_perform';
  41. header("Location: " . $this->getReturnDataUrl($aVendor['id'], array(
  42. 'order_id' => $mixedResult['order'],
  43. 'customer_id' => $mixedResult['customer'],
  44. 'pending_id' => $aPending['id'],
  45. 'redirect' => $sRedirect
  46. )));
  47. exit;
  48. case BX_PAYMENT_TYPE_RECURRING:
  49. $mixedResult = $this->_createSubscription($sToken, $iPendingId, $aClient, $aCartInfo);
  50. if($mixedResult === false)
  51. return $this->_sLangsPrefix . 'err_cannot_perform';
  52. return array(
  53. 'code' => 0,
  54. 'eval' => $this->_oModule->_oConfig->getJsObject('cart') . '.onSubscribeSubmit(oData);',
  55. 'redirect' => $this->getReturnDataUrl($aVendor['id'], array(
  56. 'order_id' => $mixedResult['order'],
  57. 'customer_id' => $mixedResult['customer'],
  58. 'pending_id' => $aPending['id'],
  59. 'redirect' => $sRedirect
  60. ))
  61. );
  62. }
  63. }
  64. public function finalizeCheckout(&$aData)
  65. {
  66. $sOrderId = bx_process_input($aData['order_id']);
  67. $sCustomerId = bx_process_input($aData['customer_id']);
  68. $iPendingId = bx_process_input($aData['pending_id'], BX_DATA_INT);
  69. if(empty($iPendingId))
  70. return array('code' => 1, 'message' => $this->_sLangsPrefix . 'err_wrong_data');
  71. $sRedirect = bx_process_input($aData['redirect']);
  72. $aPending = $this->_oModule->_oDb->getOrderPending(array('type' => 'id', 'id' => $iPendingId));
  73. if(!empty($aPending['order']) || !empty($aPending['error_code']) || !empty($aPending['error_msg']) || (int)$aPending['processed'] != 0)
  74. return array('code' => 3, 'message' => $this->_sLangsPrefix . 'err_already_processed');
  75. $aResult = array(
  76. 'code' => BX_PAYMENT_RESULT_SUCCESS,
  77. 'message' => '',
  78. 'pending_id' => $iPendingId,
  79. 'customer_id' => '',
  80. 'subscription_id' => '',
  81. 'client_name' => '',
  82. 'client_email' => '',
  83. 'paid' => false,
  84. 'trial' => false,
  85. 'redirect' => $sRedirect
  86. );
  87. switch($aPending['type']) {
  88. case BX_PAYMENT_TYPE_SINGLE:
  89. $oCustomer = $this->_retrieveCustomer(BX_PAYMENT_TYPE_SINGLE, $sCustomerId);
  90. $oCharge = $this->_retrieveCharge($sOrderId);
  91. if($oCustomer === false || $oCharge === false)
  92. return array('code' => 4, 'message' => $this->_sLangsPrefix . 'err_cannot_perform');
  93. $aCustomer = $oCustomer->jsonSerialize();
  94. $aCharge = $oCharge->jsonSerialize();
  95. if(empty($aCustomer) || !is_array($aCustomer) || empty($aCharge) || !is_array($aCharge))
  96. return array('code' => 4, 'message' => $this->_sLangsPrefix . 'err_cannot_perform');
  97. $aResult = array_merge($aResult, array(
  98. 'message' => $this->_sLangsPrefix . 'strp_msg_charged',
  99. 'client_email' => $aCustomer['email'],
  100. 'paid' => (bool)$aCharge['paid']
  101. ));
  102. break;
  103. case BX_PAYMENT_TYPE_RECURRING:
  104. $oCustomer = $this->_retrieveCustomer(BX_PAYMENT_TYPE_RECURRING, $sCustomerId);
  105. $oSubscription = $this->_retrieveSubscription($sCustomerId, $sOrderId);
  106. if($oCustomer === false || $oSubscription === false)
  107. return array('code' => 4, 'message' => $this->_sLangsPrefix . 'err_cannot_perform');
  108. $aCustomer = $oCustomer->jsonSerialize();
  109. $aSubscription = $oSubscription->jsonSerialize();
  110. if(empty($aCustomer) || !is_array($aCustomer) || empty($aSubscription) || !is_array($aSubscription))
  111. return array('code' => 4, 'message' => $this->_sLangsPrefix . 'err_cannot_perform');
  112. $aResult = array_merge($aResult, array(
  113. 'message' => $this->_sLangsPrefix . 'strp_msg_subscribed',
  114. 'customer_id' => $sCustomerId,
  115. 'subscription_id' => $sOrderId,
  116. 'client_email' => $aCustomer['email'],
  117. 'trial' => $aSubscription['status'] == 'trialing'
  118. ));
  119. break;
  120. }
  121. //--- Update pending transaction ---//
  122. $this->_oModule->_oDb->updateOrderPending($iPendingId, array(
  123. 'order' => $sOrderId,
  124. 'error_code' => $aResult['code'],
  125. 'error_msg' => _t($aResult['message'])
  126. ));
  127. return $aResult;
  128. }
  129. public function getCheckoutParamsSingle($aParams, &$oGrid)
  130. {
  131. if(bx_get('token') !== false)
  132. $aParams['token'] = bx_process_input(bx_get('token'));
  133. return $aParams;
  134. }
  135. public function getVerificationCodeCharge($iVendorId, $iCustomerId, $fAmount, $sCurrency)
  136. {
  137. $sCode = $this->_getVerificationCodeCharge($iVendorId, $iCustomerId, $fAmount, $sCurrency);
  138. /**
  139. * @hooks
  140. * @hookdef hook-bx_payment-stripe_get_code_charge 'bx_payment', 'stripe_get_code_charge' - hook to override verification code for 'charge' action
  141. * - $unit_name - equals `bx_payment`
  142. * - $action - equals `stripe_get_code_charge`
  143. * - $object_id - not used
  144. * - $sender_id - not used
  145. * - $extra_params - array of additional params with the following array keys:
  146. * - `provider` - [object] an instance of provider, @see BxBaseModPaymentProvider
  147. * - `vendor_id` - [int] vendor (seller) profile id
  148. * - `customer_id` - [int] customer (buyer) profile id
  149. * - `amount` - [float] charge amount
  150. * - `currency` - [string] charge currency code
  151. * - `override_result` - [string] by ref, verification code, can be overridden in hook processing
  152. * @hook @ref hook-bx_payment-stripe_get_code_charge
  153. */
  154. bx_alert($this->_oModule->_oConfig->getName(), $this->_sName . '_get_code_charge', 0, false, [
  155. 'provider' => $this,
  156. 'vendor_id' => $iVendorId,
  157. 'customer_id' => $iCustomerId,
  158. 'amount' => $fAmount,
  159. 'currency' => $sCurrency,
  160. 'override_result' => &$sCode
  161. ]);
  162. return $sCode;
  163. }
  164. public function checkVerificationCodeCharge($iVendorId, $iCustomerId, $aResult)
  165. {
  166. $bCheckResult = $this->_checkVerificationCodeCharge($iVendorId, $iCustomerId, $aResult);
  167. /**
  168. * @hooks
  169. * @hookdef hook-bx_payment-stripe_verify_charge 'bx_payment', 'stripe_verify_charge' - hook to override code verification for 'charge' action
  170. * - $unit_name - equals `bx_payment`
  171. * - $action - equals `stripe_verify_charge`
  172. * - $object_id - not used
  173. * - $sender_id - not used
  174. * - $extra_params - array of additional params with the following array keys:
  175. * - `provider` - [object] an instance of provider, @see BxBaseModPaymentProvider
  176. * - `vendor_id` - [int] vendor (seller) profile id
  177. * - `customer_id` - [int] customer (buyer) profile id
  178. * - `result` - [array] results array received from payment provider
  179. * - `override_result` - [boolean] by ref, is verification passed or not, can be overridden in hook processing
  180. * @hook @ref hook-bx_payment-stripe_verify_charge
  181. */
  182. bx_alert($this->_oModule->_oConfig->getName(), $this->_sName . '_verify_charge', 0, false, array(
  183. 'provider' => $this,
  184. 'vendor_id' => $iVendorId,
  185. 'customer_id' => $iCustomerId,
  186. 'result' => $aResult,
  187. 'override_result' => &$bCheckResult
  188. ));
  189. return $bCheckResult;
  190. }
  191. public function getVerificationCodeSubscription($iVendorId, $iCustomerId, $sSubscription, $sCurrency)
  192. {
  193. $sCode = $this->_getVerificationCodeSubscription($iVendorId, $iCustomerId, $sSubscription, $sCurrency);
  194. /**
  195. * @hooks
  196. * @hookdef hook-bx_payment-stripe_get_code_subscription 'bx_payment', 'stripe_get_code_subscription' - hook to override verification code for 'subscribe' action
  197. * - $unit_name - equals `bx_payment`
  198. * - $action - equals `stripe_get_code_subscription`
  199. * - $object_id - not used
  200. * - $sender_id - not used
  201. * - $extra_params - array of additional params with the following array keys:
  202. * - `provider` - [object] an instance of provider, @see BxBaseModPaymentProvider
  203. * - `vendor_id` - [int] vendor (seller) profile id
  204. * - `customer_id` - [int] customer (buyer) profile id
  205. * - `subscription` - [string] unique subscription id
  206. * - `currency` - [string] charge currency code
  207. * - `override_result` - [string] by ref, verification code, can be overridden in hook processing
  208. * @hook @ref hook-bx_payment-stripe_get_code_subscription
  209. */
  210. bx_alert($this->_oModule->_oConfig->getName(), $this->_sName . '_get_code_subscription', 0, false, [
  211. 'provider' => $this,
  212. 'vendor_id' => $iVendorId,
  213. 'customer_id' => $iCustomerId,
  214. 'subscription' => $sSubscription,
  215. 'currency' => $sCurrency,
  216. 'override_result' => &$sCode
  217. ]);
  218. return $sCode;
  219. }
  220. public function checkVerificationCodeSubscription($iVendorId, $iCustomerId, $aResult)
  221. {
  222. $bCheckResult = $this->_checkVerificationCodeSubscription($iVendorId, $iCustomerId, $aResult);
  223. /**
  224. * @hooks
  225. * @hookdef hook-bx_payment-stripe_verify_subscription 'bx_payment', 'stripe_verify_subscription' - hook to override code verification for 'subscribe' action
  226. * It's equivalent to @ref hook-bx_payment-stripe_verify_charge
  227. * @hook @ref hook-bx_payment-stripe_verify_subscription
  228. */
  229. bx_alert($this->_oModule->_oConfig->getName(), $this->_sName . '_verify_subscription', 0, false, array(
  230. 'provider' => $this,
  231. 'vendor_id' => $iVendorId,
  232. 'customer_id' => $iCustomerId,
  233. 'result' => $aResult,
  234. 'override_result' => &$bCheckResult
  235. ));
  236. return $bCheckResult;
  237. }
  238. protected function _createToken($aCard)
  239. {
  240. try {
  241. $oToken = Stripe\Token::create(array('card' => $aCard));
  242. }
  243. catch (Stripe\Error\Base $oException) {
  244. return $this->_processException('Create Token Error: ', $oException);
  245. }
  246. return $oToken->jsonSerialize();
  247. }
  248. protected function _createCustomer($sType, $sToken, $aClient)
  249. {
  250. $oCustomer = null;
  251. $aCustomer = array(
  252. 'card' => $sToken,
  253. 'email' => !empty($aClient['email']) ? $aClient['email'] : ''
  254. );
  255. /**
  256. * @hooks
  257. * @hookdef hook-bx_payment-stripe_create_customer 'bx_payment', 'stripe_create_customer' - hook to override customer data redurned by payment provider
  258. * - $unit_name - equals `bx_payment`
  259. * - $action - equals `stripe_create_customer`
  260. * - $object_id - not used
  261. * - $sender_id - client (buyer) profile id
  262. * - $extra_params - array of additional params with the following array keys:
  263. * - `type` - [string] payment type: single or recurring
  264. * - `customer_object` - [object] by ref, an instance of customer, redurned by payment provider, can be overridden in hook processing
  265. * - `customer_params` - [array] by ref, array with customer parameters, can be overridden in hook processing
  266. * @hook @ref hook-bx_payment-stripe_create_customer
  267. */
  268. bx_alert($this->_oModule->_oConfig->getName(), $this->_sName . '_create_customer', 0, $aClient['id'], [
  269. 'type' => $sType,
  270. 'customer_object' => &$oCustomer,
  271. 'customer_params' => &$aCustomer,
  272. 'customer_object_ref' => &$oCustomer,
  273. 'customer_params_ref' => &$aCustomer,
  274. ]);
  275. try {
  276. $this->_oCustomer = !empty($oCustomer) ? $oCustomer : \Stripe\Customer::create($aCustomer);
  277. }
  278. catch (Exception $oException) {
  279. return $this->_processException('Create Customer Error: ', $oException);
  280. }
  281. return $this->_oCustomer->jsonSerialize();
  282. }
  283. protected function _createCharge($sToken, $iPendingId, &$aClient, &$aCartInfo)
  284. {
  285. if(empty($this->_oCustomer))
  286. $this->_createCustomer(BX_PAYMENT_TYPE_SINGLE, $sToken, $aClient);
  287. if(empty($this->_oCustomer))
  288. return false;
  289. $fAmount = 100 * (float)$aCartInfo['items_price'];
  290. $oCharge = null;
  291. $aCharge = array(
  292. 'customer' => $this->_oCustomer->id,
  293. 'amount' => $fAmount,
  294. 'currency' => $aCartInfo['vendor_currency_code'],
  295. 'description' => $aCartInfo['items_title'],
  296. 'metadata' => array(
  297. 'vendor' => $aCartInfo['vendor_id'],
  298. 'client' => $aClient['id'],
  299. 'product' => $iPendingId,
  300. 'verification' => $this->getVerificationCodeCharge($aCartInfo['vendor_id'], $aClient['id'], $fAmount, $aCartInfo['vendor_currency_code'])
  301. )
  302. );
  303. /**
  304. * @hooks
  305. * @hookdef hook-bx_payment-stripe_create_charge 'bx_payment', 'stripe_create_charge' - hook to override charge data redurned by payment provider
  306. * - $unit_name - equals `bx_payment`
  307. * - $action - equals `stripe_create_charge`
  308. * - $object_id - pending transaction id
  309. * - $sender_id - not used
  310. * - $extra_params - array of additional params with the following array keys:
  311. * - `charge_object` - [object] by ref, an instance of charge, redurned by payment provider, can be overridden in hook processing
  312. * - `charge_params` - [array] by ref, array with charge parameters, can be overridden in hook processing
  313. * @hook @ref hook-bx_payment-stripe_create_charge
  314. */
  315. bx_alert($this->_oModule->_oConfig->getName(), $this->_sName . '_create_charge', $iPendingId, false, [
  316. 'charge_object' => &$oCharge,
  317. 'charge_params' => &$aCharge,
  318. 'charge_params_ref' => &$aCharge,
  319. ]);
  320. try {
  321. if(empty($oCharge))
  322. $oCharge = \Stripe\Charge::create($aCharge);
  323. }
  324. catch (Exception $oException) {
  325. return $this->_processException('Create Charge Error: ', $oException);
  326. }
  327. $aResult = $oCharge->jsonSerialize();
  328. if(empty($aResult) || !is_array($aResult) || empty($aResult['paid']))
  329. return false;
  330. $aMetadata = $aResult['metadata'];
  331. if(!$this->checkVerificationCodeCharge($aCartInfo['vendor_id'], $aClient['id'], $aResult))
  332. return false;
  333. return array(
  334. 'pending' => $iPendingId,
  335. 'amount' =>(float)$aResult['amount'] / 100,
  336. 'customer' => $this->_oCustomer->id,
  337. 'order' => $aResult['id']
  338. );
  339. }
  340. protected function _createSubscription($sToken, $iPendingId, &$aClient, &$aCartInfo)
  341. {
  342. if(empty($this->_oCustomer))
  343. $this->_createCustomer(BX_PAYMENT_TYPE_RECURRING, $sToken, $aClient);
  344. if(empty($this->_oCustomer))
  345. return false;
  346. $aItem = array_shift($aCartInfo['items']);
  347. if(empty($aItem) || !is_array($aItem))
  348. return false;
  349. $iTrial = $this->_oModule->_oConfig->getTrial(BX_PAYMENT_TYPE_RECURRING, $aItem);
  350. $bTrial = !empty($iTrial);
  351. $oSubscription = null;
  352. $aSubscription = array(
  353. 'plan' => $aItem['name'],
  354. 'metadata' => array(
  355. 'vendor' => $aCartInfo['vendor_id'],
  356. 'client' => $aClient['id'],
  357. 'product' => $iPendingId,
  358. 'verification' => $this->getVerificationCodeSubscription($aCartInfo['vendor_id'], $aClient['id'], $aItem['name'], $aCartInfo['vendor_currency_code'])
  359. )
  360. );
  361. /**
  362. * @hooks
  363. * @hookdef hook-bx_payment-stripe_create_subscription 'bx_payment', 'stripe_create_subscription' - hook to override subscription data redurned by payment provider
  364. * - $unit_name - equals `bx_payment`
  365. * - $action - equals `stripe_create_subscription`
  366. * - $object_id - pending transaction id
  367. * - $sender_id - not used
  368. * - $extra_params - array of additional params with the following array keys:
  369. * - `customer` - [object] by ref, an instance of customer, can be overridden in hook processing
  370. * - `subscription_object` - [object] by ref, an instance of subscription, redurned by payment provider, can be overridden in hook processing
  371. * - `subscription_params` - [array] by ref, array with subscription parameters, can be overridden in hook processing
  372. * @hook @ref hook-bx_payment-stripe_create_subscription
  373. */
  374. bx_alert($this->_oModule->_oConfig->getName(), $this->_sName . '_create_subscription', $iPendingId, false, [
  375. 'customer' => &$this->_oCustomer,
  376. 'subscription_object' => &$oSubscription,
  377. 'subscription_params' => &$aSubscription,
  378. 'subscription_object_ref' => &$oSubscription,
  379. 'subscription_params_ref' => &$aSubscription,
  380. ]);
  381. try {
  382. if(empty($oSubscription))
  383. $oSubscription = \Stripe\Subscription::create(['customer' => $this->_oCustomer->id, 'items' => [['price' => $aItem['name']]], 'metadata' => $aSubscription['metadata']]);
  384. }
  385. catch (Exception $oException) {
  386. return $this->_processException('Create Subscription Error: ', $oException);
  387. }
  388. $aResult = $oSubscription->jsonSerialize();
  389. if(empty($aResult) || !is_array($aResult) || (!$bTrial && $aResult['status'] != 'active') || ($bTrial && !in_array($aResult['status'], array('active', 'trialing'))))
  390. return false;
  391. if(!$this->checkVerificationCodeSubscription($aCartInfo['vendor_id'], $aClient['id'], $aResult))
  392. return false;
  393. return array(
  394. 'pending' => $iPendingId,
  395. 'amount' =>(float)$aCartInfo['items_price'],
  396. 'customer' => $this->_oCustomer->id,
  397. 'order' => $aResult['id'],
  398. 'trial' => $bTrial
  399. );
  400. }
  401. protected function _createCard($sType, $sCustomerId, $sToken)
  402. {
  403. try {
  404. $oCard = $this->_retrieveCustomer($sType, $sCustomerId)->sources->create(array(
  405. 'source' => $sToken
  406. ));
  407. }
  408. catch (Stripe\Error\Base $oException) {
  409. return $this->_processException('Create Card Error: ', $oException);
  410. }
  411. return $oCard->jsonSerialize();
  412. }
  413. protected function _retrieveCard($sCustomerId, $sCardId = '') {
  414. try {
  415. $oCustomer = \Stripe\Customer::retrieve($sCustomerId);
  416. $oCard = $oCustomer->sources->retrieve(!empty($sCardId) ? $sCardId : $oCustomer->default_source);
  417. }
  418. catch (Exception $oException) {
  419. return $this->_processException('Retrieve Card Error: ', $oException);
  420. }
  421. return $oCard;
  422. }
  423. protected function _retrieveCoupon($sId) {
  424. try {
  425. $oCoupon = \Stripe\Coupon::retrieve($sId);
  426. }
  427. catch (Exception $oException) {
  428. return $this->_processException('Retrieve Coupon Error: ', $oException);
  429. }
  430. return $oCoupon;
  431. }
  432. protected function _getButtonJs($sType, $iClientId, $iVendorId, $aParams = array())
  433. {
  434. $sClientEmail = '';
  435. if(!empty($iClientId) && ($oClient = BxDolProfile::getInstance($iClientId)) !== false)
  436. $sClientEmail = $oClient->getAccountObject()->getEmail();
  437. $sPublicKey = '';
  438. /**
  439. * @hooks
  440. * @hookdef hook-bx_payment-stripe_get_button 'bx_payment', 'stripe_get_button' - hook to override checkout/subscibe button
  441. * - $unit_name - equals `bx_payment`
  442. * - $action - equals `stripe_get_button`
  443. * - $object_id - not used
  444. * - $sender_id - client (buyer) profile id
  445. * - $extra_params - array of additional params with the following array keys:
  446. * - `type` - [string] by ref, payment type ('single' or 'recurring'), can be overridden in hook processing
  447. * - `public_key` - [string] by ref, Stripe public key, can be overridden in hook processing
  448. * @hook @ref hook-bx_payment-stripe_get_button
  449. */
  450. bx_alert($this->_oModule->_oConfig->getName(), $this->_sName . '_get_button', 0, $iClientId, [
  451. 'type' => &$sType,
  452. 'public_key' => &$sPublicKey,
  453. 'type_ref' => &$sType,
  454. 'public_key_ref' => &$sPublicKey,
  455. ]);
  456. $sJsMethod = '';
  457. $sJsObject = $this->getJsObject($aParams);
  458. switch($sType) {
  459. case BX_PAYMENT_TYPE_SINGLE:
  460. $sJsMethod = $sJsObject . '.checkout(this)';
  461. break;
  462. case BX_PAYMENT_TYPE_RECURRING:
  463. $sJsMethod = $sJsObject . '.subscribe(this)';
  464. break;
  465. }
  466. return array($this->_oModule->_oTemplate->getJsCode($this->_sName, array_merge(array(
  467. 'js_object' => $sJsObject,
  468. 'sProvider' => $this->_sName,
  469. 'sPublicKey' => !empty($sPublicKey) ? $sPublicKey : $this->_getPublicKey(),
  470. 'sVendorName' => '',
  471. 'sVendorCurrency' => '',
  472. 'sVendorIcon' => '',
  473. 'sClientEmail' => $sClientEmail,
  474. ), $aParams)), $sJsMethod);
  475. }
  476. protected function _getVerificationCodeCharge($iVendorId, $iCustomerId, $fAmount, $sCurrency)
  477. {
  478. return md5(implode('#-#', array(
  479. (int)$iVendorId,
  480. (int)$iCustomerId,
  481. (float)$fAmount,
  482. strtoupper($sCurrency)
  483. )));
  484. }
  485. protected function _checkVerificationCodeCharge($iVendorId, $iCustomerId, $aResult)
  486. {
  487. return !empty($aResult['metadata']['verification']) && $aResult['metadata']['verification'] == $this->getVerificationCodeCharge($iVendorId, $iCustomerId, $aResult['amount'], $aResult['currency']);
  488. }
  489. protected function _getVerificationCodeSubscription($iVendorId, $iCustomerId, $sSubscription, $sCurrency)
  490. {
  491. return md5(implode('#-#', array(
  492. (int)$iVendorId,
  493. (int)$iCustomerId,
  494. strtoupper($sSubscription),
  495. strtoupper($sCurrency)
  496. )));
  497. }
  498. protected function _checkVerificationCodeSubscription($iVendorId, $iCustomerId, $aResult)
  499. {
  500. return !empty($aResult['metadata']['verification']) && $aResult['metadata']['verification'] == $this->getVerificationCodeSubscription($iVendorId, $iCustomerId, $aResult['plan']['id'], $aResult['plan']['currency']);
  501. }
  502. }
  503. /** @} */