Payment.php 11 KB

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