0
0

PhOrderPay.php 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217
  1. <?php
  2. namespace app\common\model;
  3. use app\common\util\AppMsg;
  4. use app\hander\HelpHander;
  5. use think\Db;
  6. use think\Exception;
  7. use think\Log;
  8. use tools\Hwsms;
  9. class PhOrderPay extends Base
  10. {
  11. // 用户生成支付单
  12. public function addSave($userId,$orgId,$orderId,$money){
  13. $data = [
  14. 'sn' => get_unique_id('PH'),
  15. 'order_id' => $orderId,
  16. 'money' => $money,
  17. 'type' => 2,
  18. 'cate' => 1,
  19. 'status' => 0,
  20. 'create_time' => date('Y-m-d H:i:s')
  21. ];
  22. $order = Db::name('ph_orders')
  23. ->where('id',$orderId)
  24. ->where('status','in',[0,1])
  25. ->where('form',1)
  26. ->where('user_id',$userId)
  27. ->where('org_id',$orgId)
  28. ->find();
  29. if(!$order){
  30. HelpHander::error('订单不存在');
  31. }
  32. $payId = Db::name('ph_order_pay')->insertGetId($data);
  33. if(!$payId){
  34. HelpHander::error('操作失败');
  35. }
  36. return $payId;
  37. }
  38. // 用户支付成功预收款,改变状态
  39. public function paySuccess($payId,$type=2){
  40. Db::startTrans();
  41. try{
  42. $pay = Db::name('ph_order_pay')
  43. ->where('id',$payId)
  44. ->find();
  45. if(!$pay){
  46. \exception('支付单不存在');
  47. }
  48. $order = Db::name('ph_orders')
  49. ->where('id',$pay['order_id'])
  50. ->lock(true)
  51. ->find();
  52. if(!$order){
  53. \exception('订单不存在');
  54. }
  55. $res = Db::name('ph_order_pay')->where('id',$payId)->update([
  56. 'status' => 1,
  57. 'type' => $type,
  58. 'pay_time' => date('Y-m-d H:i:s')
  59. ]);
  60. if(!$res){
  61. \exception('操作失败');
  62. }
  63. $ret = Db::name('ph_orders')->where('id',$pay['order_id'])->update([
  64. 'pre_money' => $order['pre_money'] + $pay['money']
  65. ]);
  66. if(!$ret){
  67. \exception('操作失败');
  68. }
  69. Db::commit();
  70. }catch (Exception $e){
  71. Db::rollback();
  72. $this->error = $e->getMessage();
  73. return false;
  74. }
  75. return true;
  76. }
  77. // 调度生成支付单
  78. public function addSaveDispatch($orgId,$orderId,$money,$remark=''){
  79. $data = [
  80. 'sn' => get_unique_id('PH'),
  81. 'order_id' => $orderId,
  82. 'money' => $money,
  83. 'type' => 1,
  84. 'cate' => 2,
  85. 'status' => 1,
  86. 'create_time' => date('Y-m-d H:i:s'),
  87. 'pay_time' => date('Y-m-d H:i:s'),
  88. 'remark' => $remark
  89. ];
  90. Db::startTrans();
  91. try{
  92. $order = Db::name('ph_orders')
  93. ->where('id',$orderId)
  94. ->where('status','in',[0,1])
  95. ->where('org_id',$orgId)
  96. ->lock(true)
  97. ->find();
  98. if(!$order){
  99. \exception('订单不存在');
  100. }
  101. $payId = Db::name('ph_order_pay')->insertGetId($data);
  102. if(!$payId){
  103. \exception('操作失败');
  104. }
  105. $ret = Db::name('ph_orders')->where('id',$orderId)->update([
  106. 'pre_money' => $order['pre_money'] + $money
  107. ]);
  108. if(!$ret){
  109. \exception('操作失败');
  110. }
  111. Db::commit();
  112. }catch (Exception $e){
  113. Db::rollback();
  114. $this->error = $e->getMessage();
  115. return false;
  116. }
  117. return true;
  118. }
  119. public function refundOrder($userId){
  120. $data = [
  121. 'type' => input('type/d',1),
  122. 'money' => input('money/f',0),
  123. 'pay_id' => input('id/d',0),
  124. 'user_id' => $userId,
  125. 'remark' => input('remark','','trim'),
  126. 'create_time' => date('Y-m-d H:i:s'),
  127. ];
  128. if(!in_array($data['type'],[1,2])){
  129. $this->error = '类型不正确';
  130. return false;
  131. }
  132. if($data['money'] <= 0){
  133. $this->error = '金额必须大于0';
  134. return false;
  135. }
  136. $pay = Db::name('ph_order_pay')->where('id',$data['pay_id'])->find();
  137. if(!$pay||$pay['status'] != 1){
  138. $this->error = '订单不存在';
  139. return false;
  140. }
  141. $data['sn'] = get_unique_id('TPH');
  142. $data['order_id'] = $pay['order_id'];
  143. if($data['money'] > ($pay['money'] - $pay['money2'])){
  144. $this->error = '退款金额过大';
  145. return false;
  146. }
  147. if($pay['type'] == 1 && $data['type'] == 2){
  148. $this->error = '线下的记录不能线上退款';
  149. return false;
  150. }
  151. Db::startTrans();
  152. try{
  153. $ret = Db::name('ph_order_refund')->insert($data);
  154. if(!$ret){
  155. \exception('操作失败');
  156. }
  157. $ret2 = Db::name('ph_order_pay')->where('id',$data['pay_id'])->update(['money2' => $pay['money2'] + $data['money']]);
  158. if(!$ret2){
  159. \exception('操作失败');
  160. }
  161. $order = Db::name('ph_orders')
  162. ->where('id',$pay['order_id'])
  163. ->lock(true)
  164. ->find();
  165. if(!$order){
  166. \exception('订单不存在');
  167. }
  168. $ret = Db::name('ph_orders')->where('id',$pay['order_id'])->update([
  169. 'pre_money' => $order['pre_money'] - $data['money']
  170. ]);
  171. if(!$ret){
  172. \exception('操作失败');
  173. }
  174. if($data['type'] == 2){ // 线上,微信退款
  175. $config = config('app.wx_config');
  176. $app = \EasyWeChat\Factory::payment($config);
  177. $ret = $app->refund->byOutTradeNumber($pay['sn'], $data['sn'], $pay['money']*100, $data['money']*100);
  178. if($ret['return_code'] != 'SUCCESS' || $ret['result_code'] != 'SUCCESS'){
  179. \exception('退款申请提交失败');
  180. }
  181. }
  182. Db::commit();
  183. }catch (\Exception $e){
  184. Db::rollback();
  185. trace($e->getMessage());
  186. $this->error = $e->getMessage();
  187. return false;
  188. }
  189. return true;
  190. }
  191. }