Confirmpay.php 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  1. <?php
  2. namespace app\admin\controller\base\pay;
  3. /**
  4. * @title 支付成功后的业务处理控制器
  5. * @desc
  6. * @author Rock
  7. * @version 1.0
  8. * @icon fa fa-leaf
  9. */
  10. use think\facade\Db;
  11. use app\admin\controller\Base;
  12. class Confirmpay extends Base
  13. {
  14. protected $noNeedLogin = [];
  15. protected $payrecordModel = null;
  16. protected $orderModel = null; //购票订单模型
  17. protected $routeShift = null; //班次模型,修改座位数
  18. protected $StationServiceIface = null;//站务系统接口
  19. protected $TransportIface = null;//定制客运接口
  20. protected $wxpay = null;//微信支付
  21. protected $passengerModel = null;
  22. public function initialize()
  23. {
  24. parent::initialize();
  25. $this->payrecordModel = new \app\common\model\base\pay\Payrecord;//支付记录模型
  26. $this->orderModel = new \app\common\model\ticket\Order;//购票订单模型
  27. $this->routeShift = new \app\common\model\service\RouteShift;
  28. $this->StationServiceIface = new \app\common\iface\Stationservice;
  29. $this->TransportIface = new \app\common\iface\Transport;
  30. $this->wxpay = new \weixin\Wxpay;
  31. $this->passengerModel = new \app\common\model\ticket\Passenger;
  32. }
  33. /**
  34. * 支付成功后的业务处理
  35. */
  36. protected function ConfirmPay($poid,$tran_id='',$trade_type='')
  37. {
  38. try{
  39. $where = [];
  40. $where[] = ['poid','=',$poid];
  41. $where[] = ['status','=',0];
  42. $payInfo = $this->payrecordModel->where($where)->find();
  43. if(empty($payInfo)){
  44. return false;
  45. }
  46. $uid = $payInfo->uid;
  47. //更新支付订单状态
  48. $payInfo->paytime = time();
  49. $payInfo->tran_id = $tran_id;
  50. $payInfo->status = 1;
  51. $payInfo->save();
  52. $this->payrecordModel::startTrans();
  53. switch($payInfo->ptype){
  54. case 1://站务系统订单支付
  55. //修改订单状态
  56. $orderInfo = $this->orderModel->where("orderNo",$payInfo->oid)->find();
  57. $orderInfo->status = 2;
  58. $orderInfo->pay_at = date('Y-m-d H:i:s');
  59. $orderInfo->save();
  60. //修改车票状态
  61. $this->passengerModel->where('orderNo',$payInfo->oid)->update(['status'=>2]);
  62. //向站务系统提交支付成功回调
  63. $noticeRes = $this->StationServiceIface->orderNotice($orderInfo->origin,$orderInfo->contactId,$payInfo->oid);
  64. $this->payrecordModel::commit();
  65. return true;
  66. break;
  67. case 2://定制客运订单支付
  68. $orderInfo = $this->orderModel->where("orderNo",$payInfo->oid)->find();
  69. $oldOrderNo = cache('confirmpay_orderNo')??'';
  70. //向定制客运接口提交订单
  71. if($oldOrderNo!=$payInfo->oid){//避免重复向定制客运提交订单
  72. $res = $this->CommitTransportOrder($orderInfo);
  73. if('OK'==$res['status']){
  74. //修改订单状态
  75. $orderInfo->status = 2;
  76. $orderInfo->pay_at = date('Y-m-d H:i:s');
  77. $orderInfo->save();
  78. //修改车票状态
  79. $this->passengerModel->where('orderNo',$payInfo->oid)->update(['status'=>2]);
  80. $this->payrecordModel::commit();
  81. return true;
  82. }else{
  83. //接口下单失败则立即退款
  84. $res = $this->wxpay->Refund($tran_id,$payInfo->amount,$payInfo->amount,$res['msg']);
  85. WLog('ConfirmPay',"定制客运下单失败,进行退款".json_encode($res,JSON_UNESCAPED_UNICODE));
  86. $this->payrecordModel::rollback();
  87. return false;
  88. }
  89. cache('confirmpay_orderNo',$payInfo->oid,3600);
  90. }
  91. return true;
  92. break;
  93. default:
  94. $this->payrecordModel::rollback();
  95. return false;
  96. break;
  97. }
  98. }catch(\Exception $e){
  99. WLog('ConfirmPay',$e->getMessage());
  100. $this->payrecordModel::rollback();
  101. return false;
  102. }
  103. }
  104. /**
  105. * 提交定制客运接口订单
  106. */
  107. private function CommitTransportOrder($orderInfo)
  108. {
  109. $param = [
  110. 'mainlineid' => $orderInfo->routeId,
  111. 'DailyVehicleID' => $orderInfo->shiftId,
  112. 'ticketcount' => $orderInfo->count,
  113. 'getonstationID' => $orderInfo->startStationId,
  114. 'getoffstationID' => $orderInfo->endStationId,
  115. 'EstimateDepartTime'=> $orderInfo->beginDateTime,
  116. 'contactphone' => $orderInfo->buyer->mobile,
  117. 'contacts' => $this->getContacts($orderInfo->passenger),
  118. 'payNo' => $orderInfo->orderNo,
  119. 'amount' => $orderInfo->amount,
  120. 'actmoney' => $orderInfo->actmoney,
  121. ];
  122. $res = $this->TransportIface->commitOrder($param);
  123. WLog('ConfirmPay','TransportIface:'.json_encode($res,JSON_UNESCAPED_UNICODE));
  124. return $res;
  125. }
  126. /**获取乘车人信息 */
  127. private function getContacts($passenger)
  128. {
  129. $contactAry = [];
  130. foreach($passenger as $contact){
  131. $contactAry[] = implode(',',[$contact->username,$contact->mobile,'1023001',$contact->cardNo]);
  132. }
  133. return implode('#',$contactAry);
  134. }
  135. }