paylogic.php 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  1. <?php
  2. namespace app\index\logic;
  3. use app\common\model\appointmentmodel;
  4. use app\common\model\payordermodel;
  5. use think\Db;
  6. use think\facade\Log;
  7. /**
  8. * 支付
  9. *
  10. * @author wj
  11. * @date 2022-07-22
  12. */
  13. class paylogic extends baselogic
  14. {
  15. /**
  16. * 设置请求数据规则
  17. * 20220107
  18. * wj
  19. */
  20. protected function setrules()
  21. {
  22. $list = [
  23. 'getappointmentorderforxcx' => [
  24. ['name' => 'aid', 'title' => '申请单id', 'require' => true, 'type' => 'numeric'], //以元为单位
  25. ['name' => 'openid', 'title' => 'openid', 'require' => true, 'type' => 'string'],
  26. ['name' => 'total_fee', 'title' => '支付费用', 'require' => true, 'type' => 'numeric'], //以元为单位
  27. ],
  28. ];
  29. return $list;
  30. }
  31. /**
  32. * 创建小程序申请订单
  33. *
  34. * @return void
  35. * @author wj
  36. * @date 2022-07-22
  37. */
  38. public function getappointmentorderforxcx($arr)
  39. {
  40. $result = $this->checkparam(__FUNCTION__, $arr);
  41. if (1 != $result['status']) {
  42. return $result;
  43. }
  44. $data = $result['data'];
  45. $openid = $data['openid'];
  46. $aid = $data['aid'];
  47. $totalfee = bcmul($data['total_fee'], 100, 0);
  48. $m_a = new appointmentmodel();
  49. $where = [
  50. 'id' => $aid,
  51. 'openid' => $openid,
  52. ];
  53. $ainfo = $m_a->getInfo($where);
  54. if (empty($ainfo)) {
  55. return backarr(0, "无申请数据");
  56. }
  57. $body = "核酸检测费用";
  58. $out_trade_no = createOrderNo();
  59. $notify_url = getselfurl('dev') . "index/appointment/pay_call_back";
  60. $trade_type = 'JSAPI';
  61. $orderinfo = [
  62. 'openid' => $openid,
  63. 'total_fee' => $totalfee,
  64. 'body' => $body,
  65. 'out_trade_no' => $out_trade_no,
  66. 'notify_url' => $notify_url,
  67. 'trade_type' => $trade_type,
  68. 'wid' => $ainfo['wid'],
  69. 'appointent_id' => $aid,
  70. ];
  71. return backarr(1, "success", $orderinfo);
  72. }
  73. /**
  74. * 处理支付回调
  75. *
  76. * @return void
  77. * @author wj
  78. * @date 2022-07-23
  79. */
  80. public function handleorder($arr)
  81. {
  82. Db::startTrans();
  83. try {
  84. $orderstatus = 8;
  85. $ispay = 0;
  86. if (array_key_exists('trade_state', $arr)) {
  87. $paystatus = $arr['trade_state'];
  88. } else {
  89. $paystatus = $arr['result_code'];
  90. }
  91. if ("SUCCESS" == $paystatus) {
  92. $orderstatus = 3;
  93. $ispay = 1;
  94. }
  95. $l_p = new payordermodel();
  96. $outorderno = $arr['out_trade_no'];
  97. $orderInfo = $l_p->getorderinfobyoutorderno($outorderno);
  98. if (empty($orderInfo)) {
  99. $msg = "orderNo:[" . $outorderno . "]" . "无对应订单号";
  100. throw new \Exception($msg);
  101. }
  102. if (3 == $orderInfo['orderstatus'] && 1 == $orderInfo['ispay']) {
  103. Db::rollback();
  104. $msg = "orderNo:[" . $outorderno . "]" . "已缴费";
  105. return backarr(1, $msg);
  106. }
  107. $updateData = [
  108. 'outorderno' => $orderInfo['outorderno'],
  109. 'wid' => $orderInfo['wid'],
  110. 'openid' => $orderInfo['openid'],
  111. 'orderstatus' => $orderstatus,
  112. 'ispay' => $ispay,
  113. 'is_reat_back' => 1,
  114. ];
  115. if (isset($arr['transaction_id']) && !empty($arr['transaction_id'])) {
  116. $updateData['transaction_id'] = $arr['transaction_id'];
  117. }
  118. $row = $l_p->updateinfobyid($orderInfo['id'], $updateData);
  119. if (empty($row)) {
  120. $msg = "orderNo:[" . $outorderno . "]修改失败";
  121. throw new \Exception($msg);
  122. }
  123. $ordertype = $orderInfo['order_type'];
  124. switch ($ordertype) {
  125. case 1:
  126. $appointentid = $orderInfo['appointent_id'];
  127. $m_a = new appointmentmodel();
  128. $ainfo = $m_a->getInfo(['id' => $appointentid, 'ispay' => 0]);
  129. if (empty($ainfo)) {
  130. $msg = "申请单id:[" . $appointentid . "]不存在";
  131. throw new \Exception($msg);
  132. }
  133. $code = $appointentid . "|" . date("YmdHis");
  134. $aupdateData = [
  135. 'ispay' => 1,
  136. 'payorderno' => $outorderno,
  137. 'code' => $code,
  138. ];
  139. $row = $ainfo->updateinfo(['id' => $appointentid], $aupdateData);
  140. if (empty($row)) {
  141. $msg = "申请单id:[" . $appointentid . "]修改失败";
  142. throw new \Exception($msg);
  143. }
  144. break;
  145. }
  146. Db::commit();
  147. Log::info("commit");
  148. return backarr(1, "处理完成", ['id' => $orderInfo['id']]);
  149. } catch (\Exception $e) {
  150. Db::rollback();
  151. log::info("rollback");
  152. return backarr(0, $e->getMessage());
  153. }
  154. }
  155. }