WebPay.php 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  1. <?php
  2. namespace addons\qingdongams\controller;
  3. use addons\epay\library\Service;
  4. use addons\qingdongams\model\PersonOrder;
  5. use addons\qingdongams\model\Workorder;
  6. use think\Db;
  7. use think\Exception;
  8. use think\exception\PDOException;
  9. use think\exception\ValidateException;
  10. use think\Log;
  11. /**
  12. *发起支付
  13. * @icon fa fa-list
  14. * @remark 用于管理网站的所有分类,分类可进行无限级分类,分类类型请在常规管理->系统配置->字典配置中添加
  15. */
  16. class WebPay extends WebIndexApi
  17. {
  18. protected $model = null;
  19. protected $noNeedLogin = ['*'];
  20. protected $log;
  21. public function __construct()
  22. {
  23. parent::__construct();
  24. }
  25. /**
  26. * 微信拉起支付
  27. */
  28. public function prepay($amount, $type = 'wechat', $method = 'miniapp', $out_trade_no = '', $openid = '')
  29. {
  30. //订单标题
  31. $title = '配件支付';
  32. //
  33. $notifyurl = config('service_url') . '/index/pay/notifyx/paytype/' . $type;
  34. $returnurl = config('service_url') . '/index/pay/returnx/paytype/' . $type . '/out_trade_no/' . $out_trade_no;
  35. $params = [
  36. 'amount' => $amount,
  37. 'orderid' => $out_trade_no,
  38. 'type' => $type,
  39. 'title' => $title,
  40. 'notifyurl' => $notifyurl,
  41. 'returnurl' => $returnurl,
  42. 'method' => $method,
  43. 'openid' => $openid,
  44. ];
  45. Log::write('支付发送' . json_encode($params), '', true);
  46. $response = Service::submitOrder($params);
  47. return $response;
  48. }
  49. /**
  50. * 支付成功,
  51. */
  52. public function notifyx()
  53. {
  54. $paytype = $this->request->param('paytype');
  55. Log::write('支付返回' . json_encode($paytype), '', true);
  56. $pay = Service::checkNotify($paytype);
  57. if (!$pay) {
  58. echo '签名错误';
  59. return;
  60. }
  61. $data = $pay->verify();
  62. Log::write('支付返回' . json_encode($data), '', true);
  63. $payamount = $paytype == 'alipay' ? $data['total_amount'] : $data['total_fee'] / 100;
  64. $out_trade_no = $data['out_trade_no'];
  65. $result1 = $result2 = false;
  66. $orderModel = new PersonOrder();
  67. $info = $orderModel->where(['order_sn' => $out_trade_no])->find();
  68. if (!$info) {
  69. Log::write('未找到订单号');
  70. echo "未找到订单";
  71. exit;
  72. }
  73. $data = [
  74. 'customer_id' => $info['customer_id'] ?? '',
  75. 'person_id' => $info['person_id'],
  76. 'status' => 0,
  77. 'process' => 0,
  78. 'is_pause' => 0,
  79. 'title' => '客户端购买备件工单',
  80. 'workorder_number' => 'KQ' . date('Ymdhis') . rand(1000, 9999),
  81. 'workorder_type' => '产品发货',
  82. 'address' => $info['addrcity'],
  83. 'address_detail' => $info['address'],
  84. 'linkman' => $info['linkname'],
  85. 'linkfun' => $info['linkmobile'],
  86. 'appointment_time' => date("Y-m-d H:i:s"),
  87. 'priority' => '一般',
  88. 'desc' => $info['remark'],
  89. 'parts' => $info['parts'],
  90. 'use_parts' => $info['parts'],
  91. 'pay_service' => $info['parts'],
  92. 'total_money' => $info['amount'],
  93. 'money' => $info['amount'],
  94. 'is_pay' => 1,
  95. 'pay_type' => $info['pay_type'],
  96. 'pay_order' => $info['order_sn'],
  97. 'pay_time' => $info['paytime'],
  98. 'create_type' => 2,
  99. ];
  100. $error = '';
  101. Db::startTrans();
  102. try {
  103. //你可以在此编写订单逻辑
  104. $result1 = $orderModel->allowField(true)->isUpdate(true, ['order_sn' => $out_trade_no])->save([
  105. 'pay_type' => $paytype,
  106. 'status' => 1,
  107. 'paytime' => time(),
  108. ]);
  109. $workModel = new Workorder();
  110. $result2 = $workModel->allowField(true)->save($data);
  111. Db::commit();
  112. } catch (ValidateException $e) {
  113. Db::rollback();
  114. $error = '错误1';
  115. } catch (PDOException $e) {
  116. Db::rollback();
  117. $error = '错误2';
  118. } catch (Exception $e) {
  119. Db::rollback();
  120. $error = '错误3';
  121. }
  122. if ($result1 !== false || $result2 !== false) {
  123. return "表单失误";
  124. } else if (!$error) {
  125. return $error;
  126. }
  127. return $pay->success();
  128. }
  129. /**
  130. * 支付返回,
  131. */
  132. public function returnx()
  133. {
  134. $paytype = $this->request->param('paytype');
  135. $out_trade_no = $this->request->param('out_trade_no');
  136. $pay = Service::checkReturn($paytype);
  137. if (!$pay) {
  138. $this->error('签名错误', '');
  139. }
  140. //你可以在这里通过out_trade_no去验证订单状态
  141. //但是不可以在此编写订单逻辑!!!
  142. $this->success("请返回网站查看支付结果", addon_url("epay/index/index"));
  143. }
  144. }