123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217 |
- <?php
- namespace app\common\model;
- use app\common\util\AppMsg;
- use app\hander\HelpHander;
- use think\Db;
- use think\Exception;
- use think\Log;
- use tools\Hwsms;
- class PhOrderPay extends Base
- {
- // 用户生成支付单
- public function addSave($userId,$orgId,$orderId,$money){
- $data = [
- 'sn' => get_unique_id('PH'),
- 'order_id' => $orderId,
- 'money' => $money,
- 'type' => 2,
- 'cate' => 1,
- 'status' => 0,
- 'create_time' => date('Y-m-d H:i:s')
- ];
- $order = Db::name('ph_orders')
- ->where('id',$orderId)
- ->where('status','in',[0,1])
- ->where('form',1)
- ->where('user_id',$userId)
- ->where('org_id',$orgId)
- ->find();
- if(!$order){
- HelpHander::error('订单不存在');
- }
- $payId = Db::name('ph_order_pay')->insertGetId($data);
- if(!$payId){
- HelpHander::error('操作失败');
- }
- return $payId;
- }
- // 用户支付成功预收款,改变状态
- public function paySuccess($payId,$type=2){
- Db::startTrans();
- try{
- $pay = Db::name('ph_order_pay')
- ->where('id',$payId)
- ->find();
- if(!$pay){
- \exception('支付单不存在');
- }
- $order = Db::name('ph_orders')
- ->where('id',$pay['order_id'])
- ->lock(true)
- ->find();
- if(!$order){
- \exception('订单不存在');
- }
- $res = Db::name('ph_order_pay')->where('id',$payId)->update([
- 'status' => 1,
- 'type' => $type,
- 'pay_time' => date('Y-m-d H:i:s')
- ]);
- if(!$res){
- \exception('操作失败');
- }
- $ret = Db::name('ph_orders')->where('id',$pay['order_id'])->update([
- 'pre_money' => $order['pre_money'] + $pay['money']
- ]);
- if(!$ret){
- \exception('操作失败');
- }
- Db::commit();
- }catch (Exception $e){
- Db::rollback();
- $this->error = $e->getMessage();
- return false;
- }
- return true;
- }
- // 调度生成支付单
- public function addSaveDispatch($orgId,$orderId,$money,$remark=''){
- $data = [
- 'sn' => get_unique_id('PH'),
- 'order_id' => $orderId,
- 'money' => $money,
- 'type' => 1,
- 'cate' => 2,
- 'status' => 1,
- 'create_time' => date('Y-m-d H:i:s'),
- 'pay_time' => date('Y-m-d H:i:s'),
- 'remark' => $remark
- ];
- Db::startTrans();
- try{
- $order = Db::name('ph_orders')
- ->where('id',$orderId)
- ->where('status','in',[0,1])
- ->where('org_id',$orgId)
- ->lock(true)
- ->find();
- if(!$order){
- \exception('订单不存在');
- }
- $payId = Db::name('ph_order_pay')->insertGetId($data);
- if(!$payId){
- \exception('操作失败');
- }
- $ret = Db::name('ph_orders')->where('id',$orderId)->update([
- 'pre_money' => $order['pre_money'] + $money
- ]);
- if(!$ret){
- \exception('操作失败');
- }
- Db::commit();
- }catch (Exception $e){
- Db::rollback();
- $this->error = $e->getMessage();
- return false;
- }
- return true;
- }
- public function refundOrder($userId){
- $data = [
- 'type' => input('type/d',1),
- 'money' => input('money/f',0),
- 'pay_id' => input('id/d',0),
- 'user_id' => $userId,
- 'remark' => input('remark','','trim'),
- 'create_time' => date('Y-m-d H:i:s'),
- ];
- if(!in_array($data['type'],[1,2])){
- $this->error = '类型不正确';
- return false;
- }
- if($data['money'] <= 0){
- $this->error = '金额必须大于0';
- return false;
- }
- $pay = Db::name('ph_order_pay')->where('id',$data['pay_id'])->find();
- if(!$pay||$pay['status'] != 1){
- $this->error = '订单不存在';
- return false;
- }
- $data['sn'] = get_unique_id('TPH');
- $data['order_id'] = $pay['order_id'];
- if($data['money'] > ($pay['money'] - $pay['money2'])){
- $this->error = '退款金额过大';
- return false;
- }
- if($pay['type'] == 1 && $data['type'] == 2){
- $this->error = '线下的记录不能线上退款';
- return false;
- }
- Db::startTrans();
- try{
- $ret = Db::name('ph_order_refund')->insert($data);
- if(!$ret){
- \exception('操作失败');
- }
- $ret2 = Db::name('ph_order_pay')->where('id',$data['pay_id'])->update(['money2' => $pay['money2'] + $data['money']]);
- if(!$ret2){
- \exception('操作失败');
- }
- $order = Db::name('ph_orders')
- ->where('id',$pay['order_id'])
- ->lock(true)
- ->find();
- if(!$order){
- \exception('订单不存在');
- }
- $ret = Db::name('ph_orders')->where('id',$pay['order_id'])->update([
- 'pre_money' => $order['pre_money'] - $data['money']
- ]);
- if(!$ret){
- \exception('操作失败');
- }
- if($data['type'] == 2){ // 线上,微信退款
- $config = config('app.wx_config');
- $app = \EasyWeChat\Factory::payment($config);
- $ret = $app->refund->byOutTradeNumber($pay['sn'], $data['sn'], $pay['money']*100, $data['money']*100);
- if($ret['return_code'] != 'SUCCESS' || $ret['result_code'] != 'SUCCESS'){
- \exception('退款申请提交失败');
- }
- }
- Db::commit();
- }catch (\Exception $e){
- Db::rollback();
- trace($e->getMessage());
- $this->error = $e->getMessage();
- return false;
- }
- return true;
- }
- }
|