Api.php 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234
  1. <?php
  2. namespace addons\epay\controller;
  3. use addons\epay\library\Service;
  4. use addons\epay\library\Wechat;
  5. use addons\third\model\Third;
  6. use app\common\library\Auth;
  7. use Exception;
  8. use think\addons\Controller;
  9. use think\Response;
  10. use think\Session;
  11. use Yansongda\Pay\Exceptions\GatewayException;
  12. use Yansongda\Pay\Pay;
  13. /**
  14. * API接口控制器
  15. *
  16. * @package addons\epay\controller
  17. */
  18. class Api extends Controller
  19. {
  20. protected $layout = 'default';
  21. protected $config = [];
  22. /**
  23. * 默认方法
  24. */
  25. public function index()
  26. {
  27. return;
  28. }
  29. /**
  30. * 外部提交
  31. */
  32. public function submit()
  33. {
  34. $this->request->filter('trim');
  35. $out_trade_no = $this->request->request("out_trade_no");
  36. $title = $this->request->request("title");
  37. $amount = $this->request->request('amount');
  38. $type = $this->request->request('type', $this->request->request('paytype'));
  39. $method = $this->request->request('method', 'web');
  40. $openid = $this->request->request('openid', '');
  41. $auth_code = $this->request->request('auth_code', '');
  42. $notifyurl = $this->request->request('notifyurl', '');
  43. $returnurl = $this->request->request('returnurl', '');
  44. if (!$amount || $amount < 0) {
  45. $this->error("支付金额必须大于0");
  46. }
  47. if (!$type || !in_array($type, ['alipay', 'wechat'])) {
  48. $this->error("支付类型错误");
  49. }
  50. $params = [
  51. 'type' => $type,
  52. 'out_trade_no' => $out_trade_no,
  53. 'title' => $title,
  54. 'amount' => $amount,
  55. 'method' => $method,
  56. 'openid' => $openid,
  57. 'auth_code' => $auth_code,
  58. 'notifyurl' => $notifyurl,
  59. 'returnurl' => $returnurl,
  60. ];
  61. return Service::submitOrder($params);
  62. }
  63. /**
  64. * 微信支付(公众号支付&PC扫码支付)
  65. */
  66. public function wechat()
  67. {
  68. $config = Service::getConfig('wechat');
  69. $isWechat = stripos($this->request->server('HTTP_USER_AGENT'), 'MicroMessenger') !== false;
  70. $isMobile = $this->request->isMobile();
  71. $this->view->assign("isWechat", $isWechat);
  72. $this->view->assign("isMobile", $isMobile);
  73. //发起PC支付(Scan支付)(PC扫码模式)
  74. if ($this->request->isAjax()) {
  75. $pay = Pay::wechat($config);
  76. $orderid = $this->request->post("orderid");
  77. try {
  78. $result = Service::isVersionV3() ? $pay->find(['out_trade_no' => $orderid]) : $pay->find($orderid, 'scan');
  79. $this->success("", "", ['status' => $result['trade_state'] ?? 'NOTPAY']);
  80. } catch (GatewayException $e) {
  81. $this->error("查询失败(1001)");
  82. }
  83. }
  84. $orderData = Session::get("wechatorderdata");
  85. if (!$orderData) {
  86. $this->error("请求参数错误");
  87. }
  88. if ($isWechat && $isMobile) {
  89. //发起公众号(jsapi支付),openid必须
  90. //如果没有openid,则自动去获取openid
  91. if (!isset($orderData['openid']) || !$orderData['openid']) {
  92. $orderData['openid'] = Service::getOpenid();
  93. }
  94. $orderData['method'] = 'mp';
  95. $type = 'jsapi';
  96. $payData = Service::submitOrder($orderData);
  97. if (!isset($payData['paySign'])) {
  98. $this->error("创建订单失败,请返回重试", "");
  99. }
  100. } else {
  101. $orderData['method'] = 'scan';
  102. $type = 'pc';
  103. $payData = Service::submitOrder($orderData);
  104. if (!isset($payData['code_url'])) {
  105. $this->error("创建订单失败,请返回重试", "");
  106. }
  107. }
  108. $this->view->assign("orderData", $orderData);
  109. $this->view->assign("payData", $payData);
  110. $this->view->assign("type", $type);
  111. $this->view->assign("title", "微信支付");
  112. return $this->view->fetch();
  113. }
  114. /**
  115. * 支付宝支付(PC扫码支付)
  116. */
  117. public function alipay()
  118. {
  119. $config = Service::getConfig('alipay');
  120. $isWechat = stripos($this->request->server('HTTP_USER_AGENT'), 'MicroMessenger') !== false;
  121. $isMobile = $this->request->isMobile();
  122. $this->view->assign("isWechat", $isWechat);
  123. $this->view->assign("isMobile", $isMobile);
  124. if ($this->request->isAjax()) {
  125. $orderid = $this->request->post("orderid");
  126. $pay = Pay::alipay($config);
  127. try {
  128. $result = $pay->find(['out_trade_no' => $orderid]);
  129. if ($result['code'] == '10000' && $result['trade_status'] == 'TRADE_SUCCESS') {
  130. $this->success("", "", ['status' => $result['trade_status']]);
  131. } else {
  132. $this->error("查询失败");
  133. }
  134. } catch (GatewayException $e) {
  135. $this->error("查询失败(1001)");
  136. }
  137. }
  138. //发起PC支付(Scan支付)(PC扫码模式)
  139. $orderData = Session::get("alipayorderdata");
  140. if (!$orderData) {
  141. $this->error("请求参数错误");
  142. }
  143. $orderData['method'] = 'scan';
  144. $payData = Service::submitOrder($orderData);
  145. if (!isset($payData['qr_code'])) {
  146. $this->error("创建订单失败,请返回重试");
  147. }
  148. $type = 'pc';
  149. $this->view->assign("orderData", $orderData);
  150. $this->view->assign("payData", $payData);
  151. $this->view->assign("type", $type);
  152. $this->view->assign("title", "支付宝支付");
  153. return $this->view->fetch();
  154. }
  155. /**
  156. * 支付成功回调
  157. */
  158. public function notifyx()
  159. {
  160. $paytype = $this->request->param('paytype');
  161. $pay = Service::checkNotify($paytype);
  162. if (!$pay) {
  163. return json(['code' => 'FAIL', 'message' => '失败'], 500, ['Content-Type' => 'application/json']);
  164. }
  165. // 获取回调数据,V3和V2的回调接收不同
  166. $data = Service::isVersionV3() ? $pay->callback() : $pay->verify();
  167. try {
  168. //微信支付V3返回和V2不同
  169. if (Service::isVersionV3() && $paytype === 'wechat') {
  170. $data = $data['resource']['ciphertext'];
  171. $data['total_fee'] = $data['amount']['total'];
  172. }
  173. \think\Log::record($data);
  174. //获取支付金额、订单号
  175. $payamount = $paytype == 'alipay' ? $data['total_amount'] : $data['total_fee'] / 100;
  176. $out_trade_no = $data['out_trade_no'];
  177. \think\Log::record("回调成功,订单号:{$out_trade_no},金额:{$payamount}");
  178. //你可以在此编写订单逻辑
  179. } catch (Exception $e) {
  180. \think\Log::record("回调逻辑处理错误:" . $e->getMessage(), "error");
  181. }
  182. //下面这句必须要执行,且在此之前不能有任何输出
  183. if (Service::isVersionV3()) {
  184. return $pay->success()->getBody()->getContents();
  185. } else {
  186. return $pay->success()->send();
  187. }
  188. }
  189. /**
  190. * 支付成功返回
  191. */
  192. public function returnx()
  193. {
  194. $paytype = $this->request->param('paytype');
  195. if (Service::checkReturn($paytype)) {
  196. echo '签名错误';
  197. return;
  198. }
  199. //你可以在这里定义你的提示信息,但切记不可在此编写逻辑
  200. $this->success("恭喜你!支付成功!", addon_url("epay/index/index"));
  201. }
  202. }