payment.logic.php 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218
  1. <?php
  2. /**
  3. * 支付行为
  4. *
  5. */
  6. defined('InShopNC') or exit('Access Invalid!');
  7. require_once (BASE_ROOT_PATH . '/helper/bonus_helper.php');
  8. class paymentLogic
  9. {
  10. /**
  11. * 取得实物订单所需支付金额等信息, 移动支付时, 在使用红包时需要计算运费
  12. * @param int $pay_sn
  13. * @param int $member_id
  14. * @return array
  15. */
  16. public function getRealOrderInfo($pay_sn, $member_id = null,$master = false)
  17. {
  18. //验证订单信息
  19. $model_order = Model('order');
  20. $condition = [];
  21. $condition['pay_sn'] = $pay_sn;
  22. if (!empty($member_id)) {
  23. $condition['buyer_id'] = $member_id;
  24. }
  25. $order_pay_info = $model_order->getOrderPayInfo($condition,$master);
  26. if (empty($order_pay_info)) {
  27. return callback(false, '该支付单不存在');
  28. }
  29. $order_pay_info['order_type'] = 'real_order';
  30. $order_list = $model_order->getNormalOrderListEx(['pay_sn' => $pay_sn],$master);
  31. //计算本次需要在线支付的订单总金额
  32. $pay_amount = 0;
  33. if (!empty($order_list))
  34. {
  35. foreach ($order_list as $order_info)
  36. {
  37. $bonus_amount = unserialize($order_info['bonus_amount']);
  38. $user_bonus = $bonus_amount['user_bonus'];
  39. $room_id = $bonus_amount['room_id'];
  40. $room_bonus = $bonus_amount['room_bonus'];
  41. $bonus_total = $user_bonus;
  42. if($room_id > 0) {
  43. $bonus_total += $room_bonus;
  44. }
  45. $pay_amount += ncPriceFormat(floatval($order_info['order_amount']) - floatval($order_info['pd_amount']) - floatval($bonus_total));
  46. $order_sn = $order_info['order_sn'];
  47. }
  48. $subject = "实物订单_{$order_sn}";
  49. } else {
  50. $subject = "实物订单_{$pay_sn}";
  51. }
  52. $order_pay_info['subject'] = $subject;
  53. $order_pay_info['api_pay_amount'] = $pay_amount;
  54. $order_pay_info['order_list'] = $order_list;
  55. return callback(true, '', $order_pay_info);
  56. }
  57. /**
  58. * 取得虚拟订单所需支付金额等信息, 移动支付时, 在使用红包时需要计算运费
  59. * @param int $order_sn
  60. * @param int $member_id
  61. * @return array
  62. */
  63. public function getVrOrderInfo($order_sn, $member_id = null,$master = false)
  64. {
  65. //验证订单信息
  66. $model_order = Model('vr_order');
  67. $condition = array();
  68. $condition['order_sn'] = $order_sn;
  69. if (!empty($member_id)) {
  70. $condition['buyer_id'] = $member_id;
  71. }
  72. $order_info = $model_order->getOrderInfo($condition,'*',$master);
  73. if (empty($order_info)) {
  74. return callback(false, '该订单不存在');
  75. }
  76. $order_info['subject'] = '虚拟订单_' . $order_sn;
  77. $order_info['order_type'] = 'vr_order';
  78. $order_info['pay_sn'] = $order_sn;
  79. //计算本次需要在线支付的订单总金额
  80. $pay_amount = ncPriceFormat(floatval($order_info['order_amount']) - floatval($order_info['pd_amount']));
  81. $order_info['api_pay_amount'] = $pay_amount;
  82. return callback(true, '', $order_info);
  83. }
  84. /**
  85. * 取得充值单所需支付金额等信息
  86. * @param int $pdr_sn
  87. * @param int $member_id
  88. * @return array
  89. */
  90. public function getPdOrderInfo($pdr_sn, $member_id = null)
  91. {
  92. $model_pd = Model('predeposit');
  93. $condition = array();
  94. $condition['pdr_sn'] = $pdr_sn;
  95. if (!empty($member_id)) {
  96. $condition['pdr_member_id'] = $member_id;
  97. }
  98. $order_info = $model_pd->getPdRechargeInfo($condition);
  99. if (empty($order_info)) {
  100. return callback(false, '该订单不存在');
  101. }
  102. $order_info['subject'] = '预存款充值_' . $order_info['pdr_sn'];
  103. $order_info['order_type'] = 'pd_order';
  104. $order_info['pay_sn'] = $order_info['pdr_sn'];
  105. $order_info['api_pay_amount'] = $order_info['pdr_amount'];
  106. return callback(true, '', $order_info);
  107. }
  108. /**
  109. * 取得所使用支付方式信息
  110. * @param unknown $payment_code
  111. */
  112. public function getPaymentInfo($payment_code)
  113. {
  114. if (in_array($payment_code, array('offline', 'predeposit')) || empty($payment_code)) {
  115. return callback(false, '系统不支持选定的支付方式');
  116. }
  117. $model_payment = Model('payment');
  118. $condition = array();
  119. $condition['payment_code'] = $payment_code;
  120. $payment_info = $model_payment->getPaymentOpenInfo($condition);
  121. if (empty($payment_info)) {
  122. return callback(false, '系统不支持选定的支付方式');
  123. }
  124. $inc_file = BASE_PATH . DS . 'api' . DS . 'payment' . DS . $payment_info['payment_code'] . DS . $payment_info['payment_code'] . '.php';
  125. if (!file_exists($inc_file)) {
  126. return callback(false, '系统不支持选定的支付方式');
  127. }
  128. require_once($inc_file);
  129. $payment_info['payment_config'] = unserialize($payment_info['payment_config']);
  130. return callback(true, '', $payment_info);
  131. }
  132. /**
  133. * 支付成功后修改实物订单状态
  134. */
  135. public function updateRealOrder($out_trade_no, $payment_code, $order_list, $trade_no)
  136. {
  137. $post['payment_code'] = $payment_code;
  138. $post['trade_no'] = $trade_no;
  139. return Logic('order')->changeOrderReceivePay($order_list, 'system', '系统', $post);
  140. }
  141. /**
  142. * 支付成功后修改虚拟订单状态
  143. */
  144. public function updateVrOrder($out_trade_no, $payment_code, $order_info, $trade_no)
  145. {
  146. $post['payment_code'] = $payment_code;
  147. $post['trade_no'] = $trade_no;
  148. return Logic('vr_order')->changeOrderStatePay($order_info, 'system', $post);
  149. }
  150. /**
  151. * 支付成功后修改充值订单状态
  152. * @param unknown $out_trade_no
  153. * @param unknown $trade_no
  154. * @param unknown $payment_info
  155. * @throws Exception
  156. * @return multitype:unknown
  157. */
  158. public function updatePdOrder($out_trade_no, $trade_no, $payment_info, $recharge_info)
  159. {
  160. $condition = array();
  161. $condition['pdr_sn'] = $recharge_info['pdr_sn'];
  162. $condition['pdr_payment_state'] = 0;
  163. $update = array();
  164. $update['pdr_payment_state'] = 1;
  165. $update['pdr_payment_time'] = time();
  166. $update['pdr_payment_code'] = $payment_info['payment_code'];
  167. $update['pdr_payment_name'] = $payment_info['payment_name'];
  168. $update['pdr_trade_sn'] = $trade_no;
  169. $model_pd = Model('predeposit');
  170. try {
  171. $trans = new trans_wapper($model_pd,__METHOD__);
  172. $pdnum = $model_pd->getPdRechargeCount(array('pdr_sn' => $recharge_info['pdr_sn'], 'pdr_payment_state' => 1));
  173. if (intval($pdnum) > 0) {
  174. throw new Exception('订单已经处理');
  175. }
  176. //更改充值状态
  177. $state = $model_pd->editPdRecharge($update, $condition);
  178. if (!$state) {
  179. throw new Exception('更新充值状态失败');
  180. }
  181. //变更会员预存款
  182. $data = array();
  183. $data['member_id'] = $recharge_info['pdr_member_id'];
  184. $data['member_name'] = $recharge_info['pdr_member_name'];
  185. $data['amount'] = $recharge_info['pdr_amount'];
  186. $data['pdr_sn'] = $recharge_info['pdr_sn'];
  187. $model_pd->changePd('recharge', $data);
  188. $trans->commit();
  189. return callback(true);
  190. } catch (Exception $e) {
  191. $trans->rollback();
  192. return callback(false, $e->getMessage());
  193. }
  194. }
  195. }