payment.logic.php 8.8 KB

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