paylogic.php 8.3 KB

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