Api.php 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243
  1. <?php
  2. namespace addons\epay\controller;
  3. use addons\epay\library\QRCode;
  4. use addons\epay\library\Service;
  5. use addons\epay\library\Wechat;
  6. use addons\third\model\Third;
  7. use app\common\library\Auth;
  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');
  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. * @return string
  66. */
  67. public function wechat()
  68. {
  69. $config = Service::getConfig('wechat');
  70. $isWechat = stripos($this->request->server('HTTP_USER_AGENT'), 'MicroMessenger') !== false;
  71. $isMobile = $this->request->isMobile();
  72. $this->view->assign("isWechat", $isWechat);
  73. $this->view->assign("isMobile", $isMobile);
  74. //发起PC支付(Scan支付)(PC扫码模式)
  75. if ($this->request->isAjax()) {
  76. $pay = Pay::wechat($config);
  77. $orderid = $this->request->post("orderid");
  78. try {
  79. $result = $pay->find($orderid);
  80. if ($result['return_code'] == 'SUCCESS' && $result['result_code'] == 'SUCCESS') {
  81. $this->success("", "", ['status' => $result['trade_state']]);
  82. } else {
  83. $this->error("查询失败");
  84. }
  85. } catch (GatewayException $e) {
  86. $this->error("查询失败");
  87. }
  88. }
  89. $orderData = Session::get("wechatorderdata");
  90. if (!$orderData) {
  91. $this->error("请求参数错误");
  92. }
  93. if ($isWechat) {
  94. //发起公众号(jsapi支付),openid必须
  95. //如果没有openid,则自动去获取openid
  96. if (!isset($orderData['openid']) || !$orderData['openid']) {
  97. $orderData['openid'] = Service::getOpenid();
  98. }
  99. $orderData['method'] = 'mp';
  100. $type = 'jsapi';
  101. $payData = Service::submitOrder($orderData);
  102. if (!isset($payData['paySign'])) {
  103. $this->error("创建订单失败,请返回重试", "");
  104. }
  105. } else {
  106. $orderData['method'] = 'scan';
  107. $type = 'pc';
  108. $payData = Service::submitOrder($orderData);
  109. if (!isset($payData['code_url'])) {
  110. $this->error("创建订单失败,请返回重试", "");
  111. }
  112. }
  113. $this->view->assign("orderData", $orderData);
  114. $this->view->assign("payData", $payData);
  115. $this->view->assign("type", $type);
  116. $this->view->assign("title", "微信支付");
  117. return $this->view->fetch();
  118. }
  119. /**
  120. * 支付宝支付(PC扫码支付)
  121. * @return string
  122. */
  123. public function alipay()
  124. {
  125. $config = Service::getConfig('alipay');
  126. $isWechat = stripos($this->request->server('HTTP_USER_AGENT'), 'MicroMessenger') !== false;
  127. $isMobile = $this->request->isMobile();
  128. $this->view->assign("isWechat", $isWechat);
  129. $this->view->assign("isMobile", $isMobile);
  130. if ($this->request->isAjax()) {
  131. $orderid = $this->request->post("orderid");
  132. $pay = Pay::alipay($config);
  133. try {
  134. $result = $pay->find($orderid);
  135. if ($result['code'] == '10000' && $result['trade_status'] == 'TRADE_SUCCESS') {
  136. $this->success("", "", ['status' => $result['trade_status']]);
  137. } else {
  138. $this->error("查询失败");
  139. }
  140. } catch (GatewayException $e) {
  141. $this->error("查询失败");
  142. }
  143. }
  144. //发起PC支付(Scan支付)(PC扫码模式)
  145. $orderData = Session::get("alipayorderdata");
  146. if (!$orderData) {
  147. $this->error("请求参数错误");
  148. }
  149. $orderData['method'] = 'scan';
  150. $payData = Service::submitOrder($orderData);
  151. if (!isset($payData['qr_code'])) {
  152. $this->error("创建订单失败,请返回重试");
  153. }
  154. $type = 'pc';
  155. $this->view->assign("orderData", $orderData);
  156. $this->view->assign("payData", $payData);
  157. $this->view->assign("type", $type);
  158. $this->view->assign("title", "支付宝支付");
  159. return $this->view->fetch();
  160. }
  161. /**
  162. * 支付成功回调
  163. */
  164. public function notifyx()
  165. {
  166. $type = $this->request->param('type');
  167. if (!Service::checkNotify($type)) {
  168. echo '签名错误';
  169. return;
  170. }
  171. //你可以在这里你的业务处理逻辑,比如处理你的订单状态、给会员加余额等等功能
  172. //下面这句必须要执行,且在此之前不能有任何输出
  173. echo "success";
  174. return;
  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. /**
  191. * 生成二维码
  192. */
  193. public function qrcode()
  194. {
  195. $text = $this->request->get('text', 'hello world');
  196. //如果有安装二维码插件,则调用插件的生成方法
  197. if (class_exists("\addons\qrcode\library\Service") && get_addon_info('qrcode')['state']) {
  198. $qrCode = \addons\qrcode\library\Service::qrcode(['text' => $text]);
  199. $response = Response::create()->header("Content-Type", "image/png");
  200. header('Content-Type: ' . $qrCode->getContentType());
  201. $response->content($qrCode->writeString());
  202. return $response;
  203. } else {
  204. $qr = QRCode::getMinimumQRCode($text);
  205. $im = $qr->createImage(8, 5);
  206. header("Content-type: image/png");
  207. imagepng($im);
  208. imagedestroy($im);
  209. return;
  210. }
  211. }
  212. }