Api.php 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229
  1. <?php
  2. namespace addons\epay\controller;
  3. use addons\epay\library\Service;
  4. use addons\epay\library\Wechat;
  5. use Endroid\QrCode\QrCode;
  6. use think\addons\Controller;
  7. use think\Response;
  8. use think\Session;
  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. $this->error();
  25. }
  26. /**
  27. * 外部提交
  28. */
  29. public function submit()
  30. {
  31. $out_trade_no = $this->request->request("out_trade_no");
  32. $title = $this->request->request("title");
  33. $amount = $this->request->request('amount');
  34. $type = $this->request->request('type');
  35. $method = $this->request->request('method', 'web');
  36. $openid = $this->request->request('openid', '');
  37. $auth_code = $this->request->request('auth_code', '');
  38. $notifyurl = $this->request->request('notifyurl', '');
  39. $returnurl = $this->request->request('returnurl', '');
  40. if (!$amount || $amount < 0) {
  41. $this->error("支付金额必须大于0");
  42. }
  43. if (!$type || !in_array($type, ['alipay', 'wechat'])) {
  44. $this->error("支付类型错误");
  45. }
  46. $params = [
  47. 'type' => $type,
  48. 'out_trade_no' => $out_trade_no,
  49. 'title' => $title,
  50. 'amount' => $amount,
  51. 'method' => $method,
  52. 'openid' => $openid,
  53. 'auth_code' => $auth_code,
  54. 'notifyurl' => $notifyurl,
  55. 'returnurl' => $returnurl,
  56. ];
  57. return Service::submitOrder($params);
  58. }
  59. /**
  60. * 微信支付
  61. * @return string
  62. */
  63. public function wechat()
  64. {
  65. $config = Service::getConfig('wechat');
  66. $isWechat = stripos($this->request->server('HTTP_USER_AGENT'), 'MicroMessenger') !== false;
  67. $isMobile = $this->request->isMobile();
  68. $this->view->assign("isWechat", $isWechat);
  69. $this->view->assign("isMobile", $isMobile);
  70. if ($isWechat) {
  71. //发起公众号(jsapi支付)
  72. $orderData = Session::get("wechatorderdata");
  73. $openid = Session::get('openid');
  74. //如果没有openid
  75. if (!$openid) {
  76. $wechat = new Wechat($config['wechat']['app_id'], $config['wechat']['app_secret']);
  77. $openid = $wechat->getOpenid();
  78. }
  79. $orderData['method'] = 'mp';
  80. $orderData['openid'] = $openid;
  81. $payData = Service::submitOrder($orderData);
  82. $payData = json_decode($payData, true);
  83. if (!isset($payData['appId'])) {
  84. $this->error("创建订单失败,请返回重试");
  85. }
  86. $type = 'jsapi';
  87. $this->view->assign("orderData", $orderData);
  88. $this->view->assign("payData", $payData);
  89. } else {
  90. //发起PC支付(Native支付)
  91. $body = $this->request->request("body");
  92. $code_url = $this->request->request("code_url");
  93. $out_trade_no = $this->request->request("out_trade_no");
  94. $return_url = $this->request->request("return_url");
  95. $total_fee = $this->request->request("total_fee");
  96. $sign = $this->request->request("sign");
  97. $data = [
  98. 'body' => $body,
  99. 'code_url' => $code_url,
  100. 'out_trade_no' => $out_trade_no,
  101. 'return_url' => $return_url,
  102. 'total_fee' => $total_fee,
  103. ];
  104. if ($sign != md5(implode('', $data) . $config['wechat']['appid'])) {
  105. $this->error("签名不正确");
  106. }
  107. if ($this->request->isAjax()) {
  108. $pay = new Pay($config);
  109. $result = $pay->driver('wechat')->gateway('scan')->find($out_trade_no);
  110. if ($result['return_code'] == 'SUCCESS' && $result['result_code'] == 'SUCCESS') {
  111. $this->success("", "", ['trade_state' => $result['trade_state']]);
  112. } else {
  113. $this->error("查询失败");
  114. }
  115. }
  116. $data['sign'] = $sign;
  117. $type = 'pc';
  118. $this->view->assign("data", $data);
  119. }
  120. $this->view->assign("type", $type);
  121. $this->view->assign("title", "微信支付");
  122. return $this->view->fetch();
  123. }
  124. /**
  125. * 支付成功回调
  126. */
  127. public function notifyx()
  128. {
  129. $type = $this->request->param('type');
  130. $data = $this->request->request('', null, 'trim');
  131. $config = Service::getConfig($type);
  132. $pay = new Pay($config);
  133. if (!$pay->driver($type)->gateway()->verify($data)) {
  134. echo '签名错误';
  135. return;
  136. }
  137. //你可以在这里你的业务处理逻辑,比如处理你的订单状态、给会员加余额等等功能
  138. //下面这句必须要执行,且在此之前不能有任何输出
  139. echo "success";
  140. return;
  141. }
  142. /**
  143. * 支付成功返回
  144. */
  145. public function returnx()
  146. {
  147. $type = $this->request->param('type');
  148. $data = $this->request->request('', null, 'trim');
  149. $config = Service::getConfig($type);
  150. $pay = new Pay($config);
  151. if ($type == 'alipay' && !$pay->driver($type)->gateway()->verify($data)) {
  152. echo '签名错误';
  153. return;
  154. }
  155. //你可以在这里定义你的提示信息,但切记不可在此编写逻辑
  156. $this->success("恭喜你!支付成功!", addon_url("epay/index/index"));
  157. return;
  158. }
  159. /**
  160. * 生成二维码
  161. * @return Response
  162. */
  163. public function qrcode()
  164. {
  165. $text = $this->request->get('text', 'hello world');
  166. $size = $this->request->get('size', 250);
  167. $padding = $this->request->get('padding', 15);
  168. $errorcorrection = $this->request->get('errorcorrection', 'medium');
  169. $foreground = $this->request->get('foreground', "#ffffff");
  170. $background = $this->request->get('background', "#000000");
  171. $logo = $this->request->get('logo');
  172. $logosize = $this->request->get('logosize');
  173. $label = $this->request->get('label');
  174. $labelfontsize = $this->request->get('labelfontsize');
  175. $labelhalign = $this->request->get('labelhalign');
  176. $labelvalign = $this->request->get('labelvalign');
  177. // 前景色
  178. list($r, $g, $b) = sscanf($foreground, "#%02x%02x%02x");
  179. $foregroundcolor = ['r' => $r, 'g' => $g, 'b' => $b];
  180. // 背景色
  181. list($r, $g, $b) = sscanf($background, "#%02x%02x%02x");
  182. $backgroundcolor = ['r' => $r, 'g' => $g, 'b' => $b];
  183. $qrCode = new QrCode();
  184. $qrCode
  185. ->setText($text)
  186. ->setSize($size)
  187. ->setPadding($padding)
  188. ->setErrorCorrection($errorcorrection)
  189. ->setForegroundColor($foregroundcolor)
  190. ->setBackgroundColor($backgroundcolor)
  191. ->setLogoSize($logosize)
  192. ->setLabelFontPath(ROOT_PATH . 'public/assets/fonts/Times New Roman.ttf')
  193. ->setLabel($label)
  194. ->setLabelFontSize($labelfontsize)
  195. ->setLabelHalign($labelhalign)
  196. ->setLabelValign($labelvalign)
  197. ->setImageType(QrCode::IMAGE_TYPE_PNG);
  198. //也可以直接使用render方法输出结果
  199. //$qrCode->render();
  200. return new Response($qrCode->get(), 200, ['Content-Type' => $qrCode->getContentType()]);
  201. }
  202. }