Payment.php 12 KB

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