Api.php 7.4 KB

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