Payment.php 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311
  1. <?php
  2. namespace app\common\logic;
  3. use think\facade\Db;
  4. /**
  5. * ============================================================================
  6. *
  7. * ============================================================================
  8. *
  9. * ----------------------------------------------------------------------------
  10. *
  11. * ============================================================================
  12. * 逻辑层模型
  13. */
  14. class Payment
  15. {
  16. /**
  17. * 取得实物订单所需支付金额等信息
  18. * @param int $pay_sn
  19. * @param int $member_id
  20. * @return array
  21. */
  22. public function getRealOrderInfo($pay_sn, $member_id = null)
  23. {
  24. //验证订单信息
  25. $order_model = model('order');
  26. $condition = array();
  27. $condition[] = array('pay_sn', '=', $pay_sn);
  28. if (!empty($member_id)) {
  29. $condition[] = array('buyer_id', '=', $member_id);
  30. }
  31. $order_pay_info = $order_model->getOrderpayInfo($condition);
  32. if (empty($order_pay_info)) {
  33. return ds_callback(false, '该支付单不存在');
  34. }
  35. $order_pay_info['subject'] = '实物订单_' . $order_pay_info['pay_sn'];
  36. $order_pay_info['order_type'] = 'real_order';
  37. $condition = array();
  38. $condition[] = array('pay_sn', '=', $pay_sn);
  39. $order_list = $order_model->getNormalOrderList($condition);
  40. //计算本次需要在线支付的订单总金额
  41. $pay_amount = 0;
  42. if (!empty($order_list)) {
  43. foreach ($order_list as $order_info) {
  44. $payed_amount = floatval($order_info['rcb_amount']) + floatval($order_info['pd_amount']);
  45. if ($order_info['payment_code'] != 'offline' and $order_info['order_state'] > 0) {
  46. if ($order_info['order_state'] == ORDER_STATE_NEW) {
  47. }
  48. $pay_amount += floatval($order_info['order_amount']) - $payed_amount;
  49. } else {
  50. }
  51. }
  52. }
  53. $order_pay_info['api_pay_amount'] = $pay_amount;
  54. $order_pay_info['order_list'] = $order_list;
  55. return ds_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)
  64. {
  65. //验证订单信息
  66. $vrorder_model = model('vrorder');
  67. $condition = array();
  68. $condition[] = array('order_sn', '=', $order_sn);
  69. if (!empty($member_id)) {
  70. $condition[] = array('buyer_id', '=', $member_id);
  71. }
  72. $order_info = $vrorder_model->getVrorderInfo($condition);
  73. if (empty($order_info)) {
  74. return ds_callback(false, '该订单不存在');
  75. }
  76. $order_info['subject'] = '虚拟订单_' . $order_sn;
  77. $order_info['order_type'] = 'vr_order';
  78. $order_info['pay_sn'] = $order_sn;
  79. //修复 第三方支付时 充值卡没算在内BUG
  80. $pay_amount = ds_price_format(floatval($order_info['order_amount']) - floatval($order_info['pd_amount']) - floatval($order_info['rcb_amount']));
  81. $order_info['api_pay_amount'] = $pay_amount;
  82. return ds_callback(true, '', $order_info);
  83. }
  84. /**
  85. * 取得店铺入驻所需支付金额等信息
  86. * @param int $order_sn
  87. * @param int $member_id
  88. * @return array
  89. */
  90. public function getSjOrderInfo($order_sn)
  91. {
  92. if (empty($order_sn)) {
  93. return ds_callback(false, '该店铺入驻不存在');
  94. }
  95. //验证订单信息
  96. $storejoinin_model = model('storejoinin');
  97. $order_info = $storejoinin_model->getOneStorejoinin(array('pay_sn' => $order_sn));
  98. if (empty($order_info)) {
  99. return ds_callback(false, '该店铺入驻不存在');
  100. }
  101. $order_info['subject'] = '店铺入驻_' . $order_sn;
  102. $order_info['order_type'] = 'sj_order';
  103. $order_info['pay_sn'] = $order_sn;
  104. //修复 第三方支付时 充值卡没算在内BUG
  105. $pay_amount = ds_price_format(floatval($order_info['paying_amount']) - floatval($order_info['rcb_amount']) - floatval($order_info['pd_amount']));
  106. $order_info['api_pay_amount'] = $pay_amount;
  107. return ds_callback(true, '', $order_info);
  108. }
  109. /**
  110. * 取得充值单所需支付金额等信息
  111. * @param int $pdr_sn
  112. * @param int $member_id
  113. * @return array
  114. */
  115. public function getPdOrderInfo($pdr_sn, $member_id = null)
  116. {
  117. $predeposit_model = model('predeposit');
  118. $condition = array();
  119. $condition[] = array('pdr_sn', '=', $pdr_sn);
  120. if (!empty($member_id)) {
  121. $condition[] = array('pdr_member_id', '=', $member_id);
  122. }
  123. $order_info = $predeposit_model->getPdRechargeInfo($condition);
  124. if (empty($order_info)) {
  125. return ds_callback(false, '该订单不存在');
  126. }
  127. $order_info['subject'] = '预存款充值_' . $order_info['pdr_sn'];
  128. $order_info['order_type'] = 'pd_order';
  129. $order_info['pay_sn'] = $order_info['pdr_sn'];
  130. $order_info['api_pay_amount'] = $order_info['pdr_amount'];
  131. return ds_callback(true, '', $order_info);
  132. }
  133. /**
  134. * 取得所使用支付方式信息
  135. * @param unknown $payment_code
  136. */
  137. public function getPaymentInfo($payment_code)
  138. {
  139. if (in_array($payment_code, array('offline', 'predeposit')) || empty($payment_code)) {
  140. return ds_callback(false, '系统不支持选定的支付方式');
  141. }
  142. $payment_model = model('payment');
  143. $condition = array();
  144. $condition[] = array('payment_code', '=', $payment_code);
  145. $payment_info = $payment_model->getPaymentOpenInfo($condition);
  146. if (empty($payment_info)) {
  147. return ds_callback(false, '系统不支持选定的支付方式');
  148. }
  149. $inc_file = PLUGINS_PATH . DIRECTORY_SEPARATOR . 'payments' . DIRECTORY_SEPARATOR . $payment_info['payment_code'] . DIRECTORY_SEPARATOR . $payment_info['payment_code'] . '.php';
  150. if (!file_exists($inc_file)) {
  151. return ds_callback(false, '系统不支持选定的支付方式');
  152. }
  153. require_once $inc_file;
  154. $payment_info['payment_config'] = unserialize($payment_info['payment_config']);
  155. return ds_callback(true, '', $payment_info);
  156. }
  157. /**
  158. * 支付成功后修改实物订单状态
  159. */
  160. public function updateRealOrder($out_trade_no, $payment_code, $order_list, $trade_no)
  161. {
  162. $post['payment_code'] = $payment_code;
  163. $post['trade_no'] = $trade_no;
  164. try {
  165. Db::startTrans();
  166. model('order', 'logic')->changeOrderReceivePay($order_list, 'system', '系统', $post);
  167. } catch (\Exception $e) {
  168. Db::rollback();
  169. return ds_callback(false, $e->getMessage());
  170. }
  171. Db::commit();
  172. return ds_callback(true, '操作成功');
  173. }
  174. /**
  175. * 支付成功后修改店铺入驻状态
  176. */
  177. public function updateSjOrder($out_trade_no, $payment_code, $order_info, $trade_no)
  178. {
  179. return model('store')->setStoreOpen($order_info, array('joinin_state' => STORE_JOIN_STATE_FINAL, 'payment_code' => $payment_code, 'trade_sn' => $trade_no));
  180. }
  181. /**
  182. * 支付成功后修改虚拟订单状态
  183. */
  184. public function updateVrOrder($out_trade_no, $payment_code, $order_info, $trade_no)
  185. {
  186. $post['payment_code'] = $payment_code;
  187. $post['trade_no'] = $trade_no;
  188. return model('vrorder', 'logic')->changeOrderStatePay($order_info, 'system', $post);
  189. }
  190. /**
  191. * 支付成功后修改充值订单状态
  192. * @param unknown $out_trade_no
  193. * @param unknown $trade_no
  194. * @param unknown $payment_code
  195. * @throws Exception
  196. * @return multitype:unknown
  197. */
  198. public function updatePdOrder($out_trade_no, $payment_code, $recharge_info, $trade_no)
  199. {
  200. $condition = array();
  201. $condition[] = array('pdr_sn', '=', $recharge_info['pdr_sn']);
  202. $condition[] = array('pdr_payment_state', '=', 0);
  203. $update = array();
  204. $update['pdr_payment_state'] = 1;
  205. $update['pdr_paymenttime'] = TIMESTAMP;
  206. $update['pdr_payment_code'] = $payment_code;
  207. $update['pdr_trade_sn'] = $trade_no;
  208. $predeposit_model = model('predeposit');
  209. try {
  210. Db::startTrans();
  211. $pdnum = $predeposit_model->getPdRechargeCount(array(
  212. 'pdr_sn' => $recharge_info['pdr_sn'], 'pdr_payment_state' => 1
  213. ));
  214. if (intval($pdnum) > 0) {
  215. throw new \think\Exception('订单已经处理', 10006);
  216. }
  217. //更改充值状态
  218. $state = $predeposit_model->editPdRecharge($update, $condition);
  219. if (!$state) {
  220. throw new \think\Exception('更新充值状态失败', 10006);
  221. }
  222. //变更会员预存款
  223. $data = array();
  224. $data['member_id'] = $recharge_info['pdr_member_id'];
  225. $data['member_name'] = $recharge_info['pdr_member_name'];
  226. $data['amount'] = $recharge_info['pdr_amount'];
  227. $data['pdr_sn'] = $recharge_info['pdr_sn'];
  228. $predeposit_model->changePd('recharge', $data);
  229. Db::commit();
  230. return ds_callback(true);
  231. } catch (\Exception $e) {
  232. Db::rollback();
  233. return ds_callback(false, $e->getMessage());
  234. }
  235. }
  236. /**
  237. *
  238. * @param type $out_trade_no #商城内部订单号
  239. * @param type $trade_no #支付交易流水号
  240. * @param type $order_type #订单ID
  241. * @param type $payment_code #支付方式代号
  242. */
  243. public function updateOrder($out_trade_no, $trade_no, $order_type, $payment_code)
  244. {
  245. $out_trade_no = current(explode('_', $out_trade_no));
  246. if ($order_type == 'real_order') {
  247. $order = $this->getRealOrderInfo($out_trade_no);
  248. if (intval($order['data']['api_paystate'])) {
  249. //订单已支付
  250. return true;
  251. }
  252. $order_list = $order['data']['order_list'];
  253. $result = $this->updateRealOrder($out_trade_no, $payment_code, $order_list, $trade_no);
  254. } elseif ($order_type == 'vr_order') {
  255. $order = $this->getVrOrderInfo($out_trade_no);
  256. if ($order['data']['order_state'] != ORDER_STATE_NEW) {
  257. //订单已支付
  258. return true;
  259. }
  260. $result = $this->updateVrOrder($out_trade_no, $payment_code, $order['data'], $trade_no);
  261. } elseif ($order_type == 'sj_order') {
  262. $order = $this->getSjOrderInfo($out_trade_no);
  263. if ($order['data']['joinin_state'] != STORE_JOIN_STATE_VERIFY_SUCCESS) {
  264. //订单已支付
  265. return true;
  266. }
  267. $result = $this->updateSjOrder($out_trade_no, $payment_code, $order['data'], $trade_no);
  268. } elseif ($order_type == 'pd_order') {
  269. $order = $this->getPdOrderInfo($out_trade_no);
  270. if ($order['data']['pdr_payment_state'] == 1) {
  271. //订单已支付
  272. return true;
  273. }
  274. $result = $this->updatePdOrder($out_trade_no, $payment_code, $order['data'], $trade_no);
  275. }
  276. return $result['code'] ? TRUE : FALSE;
  277. }
  278. }