Payment.php 12 KB

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