Api.php 6.7 KB

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