paylogic.php 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252
  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. 'handlefrontorder' => [
  29. ['name' => 'openid', 'title' => 'openid', 'require' => true, 'type' => 'string'],
  30. ['name' => 'orderstatus', 'title' => '订单状态', 'require' => true, 'type' => 'numeric'],
  31. ['name' => 'outorderno', 'title' => '商户订单号', 'require' => true, 'type' => 'numeric'],
  32. ['name' => 'ispay', 'title' => '已支付', 'require' => true, 'type' => 'numeric'],
  33. ],
  34. ];
  35. return $list;
  36. }
  37. /**
  38. * 创建小程序申请订单
  39. *
  40. * @return void
  41. * @author wj
  42. * @date 2022-07-22
  43. */
  44. public function getappointmentorderforxcx($arr)
  45. {
  46. $result = $this->checkparam(__FUNCTION__, $arr);
  47. if (1 != $result['status']) {
  48. return $result;
  49. }
  50. $data = $result['data'];
  51. $openid = $data['openid'];
  52. $aid = $data['aid'];
  53. $totalfee = bcmul($data['total_fee'], 1, 0);
  54. $m_a = new appointmentmodel();
  55. $where = [
  56. 'id' => $aid,
  57. 'openid' => $openid,
  58. ];
  59. $ainfo = $m_a->getInfo($where);
  60. if (empty($ainfo)) {
  61. return backarr(0, "无申请数据");
  62. }
  63. if (1 == $ainfo['ispay']) {
  64. return backarr(0, "申请单已支付");
  65. }
  66. if (1 == $ainfo['isuse']) {
  67. return backarr(0, "申请单已使用");
  68. }
  69. $body = "核酸检测费用";
  70. $out_trade_no = createOrderNo($aid);
  71. $notify_url = getselfurl('dev') . "index/appointment/pay_call_back";
  72. $trade_type = 'JSAPI';
  73. $orderinfo = [
  74. 'openid' => $openid,
  75. 'total_fee' => $totalfee,
  76. 'body' => $body,
  77. 'out_trade_no' => $out_trade_no,
  78. 'notify_url' => $notify_url,
  79. 'trade_type' => $trade_type,
  80. 'wid' => $ainfo['wid'],
  81. 'appointent_id' => $aid,
  82. ];
  83. return backarr(1, "success", $orderinfo);
  84. }
  85. /**
  86. * 处理后端支付回调
  87. *
  88. * @return void
  89. * @author wj
  90. * @date 2022-07-23
  91. */
  92. public function handleorder($arr)
  93. {
  94. Db::startTrans();
  95. try {
  96. $orderstatus = 8;
  97. $ispay = 0;
  98. if (array_key_exists('trade_state', $arr)) {
  99. $paystatus = $arr['trade_state'];
  100. } else {
  101. $paystatus = $arr['result_code'];
  102. }
  103. if ("SUCCESS" == $paystatus) {
  104. $orderstatus = 3;
  105. $ispay = 1;
  106. }
  107. $l_p = new payordermodel();
  108. $outorderno = $arr['out_trade_no'];
  109. $orderInfo = $l_p->getorderinfobyoutorderno($outorderno);
  110. if (empty($orderInfo)) {
  111. $msg = "orderNo:[" . $outorderno . "]" . "无对应订单号";
  112. throw new \Exception($msg);
  113. }
  114. if (3 == $orderInfo['orderstatus'] && 1 == $orderInfo['ispay']) {
  115. if (empty($orderInfo['transaction_id'])) {
  116. $updateData = ['is_reat_back' => 1];
  117. if (isset($arr['transaction_id']) && !empty($arr['transaction_id'])) {
  118. $updateData['transaction_id'] = $arr['transaction_id'];
  119. }
  120. $row = $l_p->updateinfobyid($orderInfo['id'], $updateData);
  121. if (empty($row)) {
  122. $msg = "orderNo:[" . $outorderno . "]修改失败";
  123. throw new \Exception($msg);
  124. }
  125. Db::commit();
  126. } else {
  127. Db::rollback();
  128. }
  129. $msg = "orderNo:[" . $outorderno . "]" . "已缴费";
  130. return backarr(1, $msg);
  131. }
  132. $updateData = [
  133. 'outorderno' => $orderInfo['outorderno'],
  134. 'wid' => $orderInfo['wid'],
  135. 'openid' => $orderInfo['openid'],
  136. 'orderstatus' => $orderstatus,
  137. 'ispay' => $ispay,
  138. 'is_reat_back' => 1,
  139. 'paytime' => date('Y-m-d H:i:s'),
  140. ];
  141. if (isset($arr['transaction_id']) && !empty($arr['transaction_id'])) {
  142. $updateData['transaction_id'] = $arr['transaction_id'];
  143. }
  144. $row = $l_p->updateinfobyid($orderInfo['id'], $updateData);
  145. if (empty($row)) {
  146. $msg = "orderNo:[" . $outorderno . "]修改失败";
  147. throw new \Exception($msg);
  148. }
  149. $ordertype = $orderInfo['order_type'];
  150. switch ($ordertype) {
  151. case 1:
  152. $appointentid = $orderInfo['appointent_id'];
  153. if (1 === $ispay) {
  154. $this->updateappointment($appointentid, $outorderno);
  155. }
  156. break;
  157. }
  158. if (3 == $orderInfo['orderstatus'] && 1 == $orderInfo['ispay']) {
  159. Db::rollback();
  160. $msg = "orderNo:[" . $outorderno . "]" . "已缴费";
  161. return backarr(1, $msg);
  162. }
  163. Db::commit();
  164. Log::info("commit");
  165. return backarr(1, "处理完成", ['id' => $orderInfo['id']]);
  166. } catch (\Exception $e) {
  167. Db::rollback();
  168. log::info("rollback");
  169. return backarr(0, $e->getMessage());
  170. }
  171. }
  172. /**
  173. * 处理前端支付回调
  174. *
  175. * @return void
  176. * @author wj
  177. * @date 2022-07-29
  178. */
  179. public function handlefrontorder($arr)
  180. {
  181. $result = $this->checkparam(__FUNCTION__, $arr);
  182. if (1 != $result['status']) {
  183. return $result;
  184. }
  185. $data = $result['data'];
  186. $openid = $data['openid'];
  187. $ispay = $data['ispay'];
  188. $orderstatus = $data['orderstatus'];
  189. $outorderno = $data['outorderno'];
  190. Db::startTrans();
  191. try {
  192. $m_po = new payordermodel;
  193. $oupdateData = [
  194. 'ispay' => $ispay,
  195. 'orderstatus' => $orderstatus,
  196. 'paytime' => date('Y-m-d H:i:s'),
  197. 'is_front_back' => 1,
  198. ];
  199. $row = $m_po->updorederstatusbyorderno($outorderno, $openid, $oupdateData);
  200. if (empty($row)) {
  201. throw new \Exception("订单修改失败");
  202. }
  203. $orderInfo = $m_po->getorderinfobyoutorderno($outorderno);
  204. $ordertype = $orderInfo['order_type'];
  205. switch ($ordertype) {
  206. case 1:
  207. $appointentid = $orderInfo['appointent_id'];
  208. if (1 === $ispay) {
  209. $this->updateappointment($appointentid, $outorderno);
  210. }
  211. break;
  212. }
  213. Db::commit();
  214. Log::info("commit");
  215. return backarr(1, "处理完成", ['id' => $orderInfo['id']]);
  216. } catch (\Exception $e) {
  217. Db::rollback();
  218. log::info("rollback");
  219. return backarr(0, $e->getMessage());
  220. }
  221. }
  222. /**
  223. * 改申请单
  224. *
  225. * @return void
  226. * @author wj
  227. * @date 2022-07-29
  228. */
  229. private function updateappointment($appointentid, $outorderno)
  230. {
  231. $m_a = new appointmentmodel();
  232. $ainfo = $m_a->getInfo(['id' => $appointentid, 'ispay' => 0]);
  233. if (empty($ainfo)) {
  234. $msg = "申请单id:[" . $appointentid . "]不存在";
  235. throw new \Exception($msg);
  236. }
  237. $code = $appointentid . "|" . date("YmdHis");
  238. $aupdateData = [
  239. 'ispay' => 1,
  240. 'payorderno' => $outorderno,
  241. 'code' => $code,
  242. ];
  243. $row = $ainfo->updateinfo(['id' => $appointentid], $aupdateData);
  244. if (empty($row)) {
  245. $msg = "申请单id:[" . $appointentid . "]修改失败";
  246. throw new \Exception($msg);
  247. }
  248. }
  249. }