paylogic.php 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227
  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. Db::rollback();
  110. $msg = "orderNo:[" . $outorderno . "]" . "已缴费";
  111. return backarr(1, $msg);
  112. }
  113. $updateData = [
  114. 'outorderno' => $orderInfo['outorderno'],
  115. 'wid' => $orderInfo['wid'],
  116. 'openid' => $orderInfo['openid'],
  117. 'orderstatus' => $orderstatus,
  118. 'ispay' => $ispay,
  119. 'is_reat_back' => 1,
  120. ];
  121. if (isset($arr['transaction_id']) && !empty($arr['transaction_id'])) {
  122. $updateData['transaction_id'] = $arr['transaction_id'];
  123. }
  124. $row = $l_p->updateinfobyid($orderInfo['id'], $updateData);
  125. if (empty($row)) {
  126. $msg = "orderNo:[" . $outorderno . "]修改失败";
  127. throw new \Exception($msg);
  128. }
  129. $ordertype = $orderInfo['order_type'];
  130. switch ($ordertype) {
  131. case 1:
  132. $appointentid = $orderInfo['appointent_id'];
  133. if (1 === $ispay) {
  134. $this->updateappointment($appointentid, $outorderno);
  135. }
  136. break;
  137. }
  138. Db::commit();
  139. Log::info("commit");
  140. return backarr(1, "处理完成", ['id' => $orderInfo['id']]);
  141. } catch (\Exception $e) {
  142. Db::rollback();
  143. log::info("rollback");
  144. return backarr(0, $e->getMessage());
  145. }
  146. }
  147. /**
  148. * 处理前端支付回调
  149. *
  150. * @return void
  151. * @author wj
  152. * @date 2022-07-29
  153. */
  154. public function handlefrontorder($arr)
  155. {
  156. $result = $this->checkparam(__FUNCTION__, $arr);
  157. if (1 != $result['status']) {
  158. return $result;
  159. }
  160. $data = $result['data'];
  161. $openid = $data['openid'];
  162. $ispay = $data['ispay'];
  163. $orderstatus = $data['orderstatus'];
  164. $outorderno = $data['outorderno'];
  165. Db::startTrans();
  166. try {
  167. $m_po = new payordermodel;
  168. $oupdateData = [
  169. 'ispay' => $ispay,
  170. 'orderstatus' => $orderstatus,
  171. 'paytime' => date('Y-m-d H:i:s'),
  172. 'is_front_back' => 1,
  173. ];
  174. $row = $m_po->updorederstatusbyorderno($outorderno, $openid, $oupdateData);
  175. if (empty($row)) {
  176. throw new \Exception("订单修改失败");
  177. }
  178. $orderInfo = $m_po->getorderinfobyoutorderno($outorderno);
  179. $ordertype = $orderInfo['order_type'];
  180. switch ($ordertype) {
  181. case 1:
  182. $appointentid = $orderInfo['appointent_id'];
  183. if (1 === $ispay) {
  184. $this->updateappointment($appointentid, $outorderno);
  185. }
  186. break;
  187. }
  188. Db::commit();
  189. Log::info("commit");
  190. return backarr(1, "处理完成", ['id' => $orderInfo['id']]);
  191. } catch (\Exception $e) {
  192. Db::rollback();
  193. log::info("rollback");
  194. return backarr(0, $e->getMessage());
  195. }
  196. }
  197. /**
  198. * 改申请单
  199. *
  200. * @return void
  201. * @author wj
  202. * @date 2022-07-29
  203. */
  204. private function updateappointment($appointentid, $outorderno)
  205. {
  206. $m_a = new appointmentmodel();
  207. $ainfo = $m_a->getInfo(['id' => $appointentid, 'ispay' => 0]);
  208. if (empty($ainfo)) {
  209. $msg = "申请单id:[" . $appointentid . "]不存在";
  210. throw new \Exception($msg);
  211. }
  212. $code = $appointentid . "|" . date("YmdHis");
  213. $aupdateData = [
  214. 'ispay' => 1,
  215. 'payorderno' => $outorderno,
  216. 'code' => $code,
  217. ];
  218. $row = $ainfo->updateinfo(['id' => $appointentid], $aupdateData);
  219. if (empty($row)) {
  220. $msg = "申请单id:[" . $appointentid . "]修改失败";
  221. throw new \Exception($msg);
  222. }
  223. }
  224. }