Api.php 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296
  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\admin\model\Details;
  8. use app\common\library\Auth;
  9. use think\addons\Controller;
  10. use think\Db;
  11. use think\Response;
  12. use think\Session;
  13. use Yansongda\Pay\Exceptions\GatewayException;
  14. use Yansongda\Pay\Pay;
  15. /**
  16. * API接口控制器
  17. *
  18. * @package addons\epay\controller
  19. */
  20. class Api extends Controller
  21. {
  22. protected $layout = 'default';
  23. protected $config = [];
  24. /**
  25. * 默认方法
  26. */
  27. public function index()
  28. {
  29. return;
  30. }
  31. /**
  32. * 外部提交
  33. */
  34. public function submit()
  35. {
  36. $this->request->filter('trim');
  37. $out_trade_no = $this->request->request("out_trade_no");
  38. $title = $this->request->request("title");
  39. $amount = $this->request->request('amount');
  40. $type = $this->request->request('type');
  41. $method = $this->request->request('method', 'web');
  42. $openid = $this->request->request('openid', '');
  43. $auth_code = $this->request->request('auth_code', '');
  44. $notifyurl = $this->request->request('notifyurl', '');
  45. $returnurl = $this->request->request('returnurl', '');
  46. if (!$amount || $amount < 0) {
  47. $this->error("支付金额必须大于0");
  48. }
  49. if (!$type || !in_array($type, ['alipay', 'wechat'])) {
  50. $this->error("支付类型错误");
  51. }
  52. $params = [
  53. 'type' => $type,
  54. 'out_trade_no' => $out_trade_no,
  55. 'title' => $title,
  56. 'amount' => $amount,
  57. 'method' => $method,
  58. 'openid' => $openid,
  59. 'auth_code' => $auth_code,
  60. 'notifyurl' => $notifyurl,
  61. 'returnurl' => $returnurl,
  62. ];
  63. return Service::submitOrder($params);
  64. }
  65. /**
  66. * 微信支付(公众号支付&PC扫码支付)
  67. * @return string
  68. */
  69. public function wechat()
  70. {
  71. $config = Service::getConfig('wechat');
  72. $isWechat = stripos($this->request->server('HTTP_USER_AGENT'), 'MicroMessenger') !== false;
  73. $isMobile = $this->request->isMobile();
  74. $this->view->assign("isWechat", $isWechat);
  75. $this->view->assign("isMobile", $isMobile);
  76. //发起PC支付(Scan支付)(PC扫码模式)
  77. if ($this->request->isAjax()) {
  78. $pay = Pay::wechat($config);
  79. $orderid = $this->request->post("orderid");
  80. try {
  81. $result = $pay->find($orderid);
  82. if ($result['return_code'] == 'SUCCESS' && $result['result_code'] == 'SUCCESS') {
  83. $this->success("", "", ['status' => $result['trade_state']]);
  84. } else {
  85. $this->error("查询失败");
  86. }
  87. } catch (GatewayException $e) {
  88. $this->error("查询失败");
  89. }
  90. }
  91. $orderData = Session::get("wechatorderdata");
  92. if (!$orderData) {
  93. $this->error("请求参数错误");
  94. }
  95. if ($isWechat) {
  96. //发起公众号(jsapi支付),openid必须
  97. //如果没有openid,则自动去获取openid
  98. if (!isset($orderData['openid']) || !$orderData['openid']) {
  99. $orderData['openid'] = Service::getOpenid();
  100. }
  101. $orderData['method'] = 'mp';
  102. $type = 'jsapi';
  103. $payData = Service::submitOrder($orderData);
  104. if (!isset($payData['paySign'])) {
  105. $this->error("创建订单失败,请返回重试", "");
  106. }
  107. } else {
  108. $orderData['method'] = 'scan';
  109. $type = 'pc';
  110. $payData = Service::submitOrder($orderData);
  111. if (!isset($payData['code_url'])) {
  112. $this->error("创建订单失败,请返回重试", "");
  113. }
  114. }
  115. $this->view->assign("orderData", $orderData);
  116. $this->view->assign("payData", $payData);
  117. $this->view->assign("type", $type);
  118. $this->view->assign("title", "微信支付");
  119. return $this->view->fetch();
  120. }
  121. /**
  122. * 支付宝支付(PC扫码支付)
  123. * @return string
  124. */
  125. public function alipay()
  126. {
  127. $config = Service::getConfig('alipay');
  128. $isWechat = stripos($this->request->server('HTTP_USER_AGENT'), 'MicroMessenger') !== false;
  129. $isMobile = $this->request->isMobile();
  130. $this->view->assign("isWechat", $isWechat);
  131. $this->view->assign("isMobile", $isMobile);
  132. if ($this->request->isAjax()) {
  133. $orderid = $this->request->post("orderid");
  134. $pay = Pay::alipay($config);
  135. try {
  136. $result = $pay->find($orderid);
  137. if ($result['code'] == '10000' && $result['trade_status'] == 'TRADE_SUCCESS') {
  138. $this->success("", "", ['status' => $result['trade_status']]);
  139. } else {
  140. $this->error("查询失败");
  141. }
  142. } catch (GatewayException $e) {
  143. $this->error("查询失败");
  144. }
  145. }
  146. //发起PC支付(Scan支付)(PC扫码模式)
  147. $orderData = Session::get("alipayorderdata");
  148. if (!$orderData) {
  149. $this->error("请求参数错误");
  150. }
  151. $orderData['method'] = 'scan';
  152. $payData = Service::submitOrder($orderData);
  153. if (!isset($payData['qr_code'])) {
  154. $this->error("创建订单失败,请返回重试");
  155. }
  156. $type = 'pc';
  157. $this->view->assign("orderData", $orderData);
  158. $this->view->assign("payData", $payData);
  159. $this->view->assign("type", $type);
  160. $this->view->assign("title", "支付宝支付");
  161. return $this->view->fetch();
  162. }
  163. /**
  164. *签名算法
  165. */
  166. private function signs($data)
  167. {
  168. unset($data['sign']);
  169. unset($data['type']);
  170. ksort($data);
  171. $stringA = '';
  172. foreach ($data as $key => $value) {
  173. if (!$value) continue;
  174. if ($stringA) $stringA .= '&' . $key . "=" . $value;
  175. else $stringA = $key . "=" . $value;
  176. }
  177. $wx_key = config('platform.wechat')['key'] ?? ''; //申请支付后有给予一个商户账号和密码,登陆后自己设置key
  178. $stringSignTemp = $stringA . '&key=' . $wx_key; //申请支付后有给予一个商户账号和密码,登陆后自己设置key
  179. return strtoupper(md5($stringSignTemp));
  180. }
  181. /**
  182. * 支付成功回调
  183. */
  184. public function notifyx()
  185. {
  186. $xml = file_get_contents("php://input");
  187. file_put_contents('55.txt', $xml);
  188. $notifyx_data = $this->xml_to_array($xml);
  189. $sign = $notifyx_data['sign'];
  190. trace('回调信息' . json_encode($notifyx_data), 'pay');
  191. if ($this->signs($notifyx_data) != $sign) {
  192. trace('签名错误' . json_encode($notifyx_data), 'pay');
  193. echo '签名错误';
  194. return;
  195. } else {
  196. trace('成功', 'pay');
  197. }
  198. if ($notifyx_data['return_code'] != "SUCCESS" || $notifyx_data['result_code'] != "SUCCESS") {
  199. $errorMsg = '支付回调错误:' . json_encode($notifyx_data);
  200. trace($errorMsg, 'pay');
  201. echo "fail";
  202. return;
  203. }
  204. // $type = $this->request->param('type');
  205. // if (!Service::checkNotify($type)) {
  206. // echo '签名错误';
  207. // return;
  208. // }
  209. //你可以在这里你的业务处理逻辑,比如处理你的订单状态、给会员加余额等等功能
  210. Db::startTrans();
  211. try {
  212. $re = $this->notifyxPay($notifyx_data);
  213. if ($re !== true) {
  214. exception($re);
  215. }
  216. Db::commit();
  217. } catch (\Exception $e) {
  218. Db::rollback();
  219. $errorMsg = '支付回调业务处理错误:' . ($e->getMessage());
  220. trace($errorMsg . json_encode($notifyx_data), 'pay');
  221. echo "fail";
  222. return;
  223. }
  224. //下面这句必须要执行,且在此之前不能有任何输出
  225. echo "success";
  226. return;
  227. }
  228. /**
  229. * 支付成功返回
  230. */
  231. public function returnx()
  232. {
  233. $type = $this->request->param('type');
  234. if (Service::checkReturn($type)) {
  235. echo '签名错误';
  236. return;
  237. }
  238. //你可以在这里定义你的提示信息,但切记不可在此编写逻辑
  239. $this->success("恭喜你!支付成功!", addon_url("epay/index/index"));
  240. return;
  241. }
  242. /**
  243. * 生成二维码
  244. */
  245. public function qrcode()
  246. {
  247. $text = $this->request->get('text', 'hello world');
  248. //如果有安装二维码插件,则调用插件的生成方法
  249. if (class_exists("\addons\qrcode\library\Service") && get_addon_info('qrcode')['state']) {
  250. $qrCode = \addons\qrcode\library\Service::qrcode(['text' => $text]);
  251. $response = Response::create()->header("Content-Type", "image/png");
  252. header('Content-Type: ' . $qrCode->getContentType());
  253. $response->content($qrCode->writeString());
  254. return $response;
  255. } else {
  256. $qr = QRCode::getMinimumQRCode($text);
  257. $im = $qr->createImage(8, 5);
  258. header("Content-type: image/png");
  259. imagepng($im);
  260. imagedestroy($im);
  261. return;
  262. }
  263. }
  264. }