DirectPayment.php 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193
  1. <?php
  2. /**
  3. *
  4. * PHP Pro Bid $Id$ SjeaaEQ8lXI22RXtJnThXE9bPS4xV+bZ6YEIN9k5inM=
  5. *
  6. * @link http://www.phpprobid.com
  7. * @copyright Copyright (c) 2015 Online Ventures Software & CodeCube SRL
  8. * @license http://www.phpprobid.com/license Commercial License
  9. *
  10. * @version 7.6
  11. */
  12. /**
  13. * sale direct payment class
  14. */
  15. namespace Ppb\Service\Fees;
  16. use Ppb\Service,
  17. Cube\Controller\Front,
  18. Ppb\Db\Table\Row\Sale as SaleModel;
  19. class DirectPayment extends Service\Fees
  20. {
  21. /**
  22. *
  23. * sale object
  24. *
  25. * @var \Ppb\Db\Table\Row\Sale
  26. */
  27. protected $_sale;
  28. /**
  29. *
  30. * total amount to be paid
  31. *
  32. * @var float
  33. */
  34. protected $_totalAmount;
  35. /**
  36. *
  37. * payment redirect path
  38. *
  39. * @var array
  40. */
  41. protected $_redirect = array(
  42. 'module' => 'members',
  43. 'controller' => 'invoices',
  44. 'action' => 'browse',
  45. 'params' => array(
  46. 'type' => 'bought',
  47. ),
  48. );
  49. /**
  50. *
  51. * class constructor
  52. *
  53. * @param \Ppb\Db\Table\Row\Sale $sale
  54. * @param integer|string|\Ppb\Db\Table\Row\User $user the user that will be paying
  55. */
  56. public function __construct(SaleModel $sale = null, $user = null)
  57. {
  58. parent::__construct();
  59. if ($sale !== null) {
  60. $this->setSale($sale);
  61. }
  62. if ($user !== null) {
  63. $this->setUser($user);
  64. }
  65. }
  66. /**
  67. *
  68. * set sale model
  69. * also, based on the sale model, set the total amount that will be used to calculate the fees against
  70. *
  71. * @param \Ppb\Db\Table\Row\Sale $sale
  72. *
  73. * @return $this
  74. */
  75. public function setSale(SaleModel $sale)
  76. {
  77. $this->_sale = $sale;
  78. $this->_totalAmount = $sale->calculateTotal();
  79. return $this;
  80. }
  81. /**
  82. *
  83. * get sale model
  84. *
  85. * @return \Ppb\Db\Table\Row\Sale
  86. */
  87. public function getSale()
  88. {
  89. return $this->_sale;
  90. }
  91. /**
  92. *
  93. * get redirect array, but attach sale_id variable if it is set
  94. *
  95. * @return array
  96. */
  97. public function getRedirect()
  98. {
  99. $redirect = $this->_redirect;
  100. if (!empty($this->_transactionDetails['data']['sale_id'])) {
  101. $redirect['params']['sale_id'] = $this->_transactionDetails['data']['sale_id'];
  102. }
  103. return $redirect;
  104. }
  105. /**
  106. *
  107. * mark the sale as paid with direct payment
  108. * if ipn returns false, mark the listing as unpaid
  109. *
  110. * @param bool $ipn true if payment is completed, false otherwise
  111. * @param array $post array keys: {sale_id}
  112. *
  113. * @return \Ppb\Service\Fees\SaleTransaction
  114. */
  115. public function callback($ipn, array $post)
  116. {
  117. $salesService = new Service\Sales();
  118. /** @var \Ppb\Db\Table\Row\Sale $sale */
  119. $sale = $salesService->findBy('id', $post['sale_id']);
  120. $flag = ($ipn) ? SaleModel::PAYMENT_PAID_DIRECT_PAYMENT : 0;
  121. $sale->save(array(
  122. 'flag_payment' => $flag,
  123. ));
  124. if ($ipn) {
  125. $sale->setExpiresFlag(true);
  126. $user = Front::getInstance()->getBootstrap()->getResource('user');
  127. $type = null;
  128. if (!empty($user['id'])) {
  129. $type = ($sale['buyer_id'] == $user['id']) ? 'bought' : 'sold';
  130. }
  131. $this->setRedirect(array(
  132. 'module' => 'members',
  133. 'controller' => 'invoices',
  134. 'action' => 'browse',
  135. 'params' => array(
  136. 'type' => $type,
  137. 'sale_id' => $post['sale_id'],
  138. ),
  139. ));
  140. }
  141. /** @var \Ppb\Db\Table\Row\User $seller */
  142. $seller = $sale->findParentRow('\Ppb\Db\Table\Users', 'Seller');
  143. $automaticDigitalDownloads = $seller->getGlobalSettings('automatic_digital_downloads');
  144. if ($flag == 0 || $automaticDigitalDownloads != -1) {
  145. $downloadsFlag = ($flag) ? 1 : 0;
  146. $sale->findDependentRowset('\Ppb\Db\Table\SalesListings')->save(array(
  147. 'downloads_active' => $downloadsFlag,
  148. ));
  149. }
  150. return $this;
  151. }
  152. /**
  153. *
  154. * get total amount to be paid resulted from the calculate() method
  155. *
  156. * @return float
  157. */
  158. public function getTotalAmount()
  159. {
  160. return $this->_totalAmount;
  161. }
  162. }