Service.php 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291
  1. <?php
  2. namespace addons\epay\library;
  3. use addons\third\model\Third;
  4. use app\common\library\Auth;
  5. use Exception;
  6. use think\Session;
  7. use Yansongda\Pay\Pay;
  8. /**
  9. * 订单服务类
  10. *
  11. * @package addons\epay\library
  12. */
  13. class Service
  14. {
  15. /**
  16. * 提交订单
  17. * @param array|float $amount 订单金额
  18. * @param string $orderid 订单号
  19. * @param string $type 支付类型,可选alipay或wechat
  20. * @param string $title 订单标题
  21. * @param string $notifyurl 通知回调URL
  22. * @param string $returnurl 跳转返回URL
  23. * @param string $method 支付方法
  24. * @return Response|RedirectResponse|Collection
  25. * @throws Exception
  26. */
  27. public static function submitOrder($amount, $orderid = null, $type = null, $title = null, $notifyurl = null, $returnurl = null, $method = null, $openid = '')
  28. {
  29. if (!is_array($amount)) {
  30. $params = [
  31. 'amount' => $amount,
  32. 'orderid' => $orderid,
  33. 'type' => $type,
  34. 'title' => $title,
  35. 'notifyurl' => $notifyurl,
  36. 'returnurl' => $returnurl,
  37. 'method' => $method,
  38. 'openid' => $openid,
  39. ];
  40. } else {
  41. $params = $amount;
  42. }
  43. $type = isset($params['type']) && in_array($params['type'], ['alipay', 'wechat']) ? $params['type'] : 'wechat';
  44. $method = isset($params['method']) ? $params['method'] : 'web';
  45. $orderid = isset($params['orderid']) ? $params['orderid'] : date("YmdHis") . mt_rand(100000, 999999);
  46. $amount = isset($params['amount']) ? $params['amount'] : 1;
  47. $title = isset($params['title']) ? $params['title'] : "支付";
  48. $auth_code = isset($params['auth_code']) ? $params['auth_code'] : '';
  49. $openid = isset($params['openid']) ? $params['openid'] : '';
  50. $request = request();
  51. $notifyurl = isset($params['notifyurl']) ? $params['notifyurl'] : $request->root(true) . '/addons/epay/index/' . $type . 'notify';
  52. $returnurl = isset($params['returnurl']) ? $params['returnurl'] : $request->root(true) . '/addons/epay/index/' . $type . 'return/out_trade_no/' . $orderid;
  53. $html = '';
  54. $config = Service::getConfig($type);
  55. $config['notify_url'] = $notifyurl;
  56. $config['return_url'] = $returnurl;
  57. $isWechat = strpos($request->server('HTTP_USER_AGENT'), 'MicroMessenger') !== false;
  58. $result = null;
  59. if ($type == 'alipay') {
  60. //如果是PC支付,判断当前环境,进行跳转
  61. if ($method == 'web') {
  62. //如果是微信环境或后台配置PC使用扫码支付
  63. if ($isWechat || $config['scanpay']) {
  64. Session::set("alipayorderdata", $params);
  65. $url = addon_url('epay/api/alipay', [], true, true);
  66. return RedirectResponse::create($url);
  67. } elseif ($request->isMobile()) {
  68. $method = 'wap';
  69. }
  70. }
  71. //创建支付对象
  72. $pay = Pay::alipay($config);
  73. $params = [
  74. 'out_trade_no' => $orderid,//你的订单号
  75. 'total_amount' => $amount,//单位元
  76. 'subject' => $title,
  77. ];
  78. switch ($method) {
  79. case 'web':
  80. //电脑支付
  81. $result = $pay->web($params);
  82. break;
  83. case 'wap':
  84. //手机网页支付
  85. $result = $pay->wap($params);
  86. break;
  87. case 'app':
  88. //APP支付
  89. $result = $pay->app($params);
  90. break;
  91. case 'scan':
  92. //扫码支付
  93. $result = $pay->scan($params);
  94. break;
  95. case 'pos':
  96. //刷卡支付必须要有auth_code
  97. $params['auth_code'] = $auth_code;
  98. $result = $pay->pos($params);
  99. break;
  100. default:
  101. }
  102. } else {
  103. //如果是PC支付,判断当前环境,进行跳转
  104. if ($method == 'web') {
  105. //如果是移动端,但不是微信环境
  106. if ($request->isMobile() && !$isWechat) {
  107. $method = 'wap';
  108. } else {
  109. Session::set("wechatorderdata", $params);
  110. $url = addon_url('epay/api/wechat', [], true, true);
  111. return RedirectResponse::create($url);
  112. }
  113. }
  114. //创建支付对象
  115. $pay = Pay::wechat($config);
  116. $params = [
  117. 'out_trade_no' => $orderid,//你的订单号
  118. 'body' => $title,
  119. 'total_fee' => $amount * 100, //单位分
  120. ];
  121. switch ($method) {
  122. //case 'web':
  123. // //电脑支付,跳转到自定义展示页面(FastAdmin独有)
  124. // $result = $pay->web($params);
  125. // break;
  126. case 'mp':
  127. //公众号支付
  128. //公众号支付必须有openid
  129. $params['openid'] = $openid;
  130. $result = $pay->mp($params);
  131. break;
  132. case 'wap':
  133. //手机网页支付,跳转
  134. $params['spbill_create_ip'] = $request->ip(0, false);
  135. $result = $pay->wap($params);
  136. break;
  137. case 'app':
  138. //APP支付,直接返回字符串
  139. $result = $pay->app($params);
  140. break;
  141. case 'scan':
  142. //扫码支付,直接返回字符串
  143. $result = $pay->scan($params);
  144. break;
  145. case 'pos':
  146. //刷卡支付,直接返回字符串
  147. //刷卡支付必须要有auth_code
  148. $params['auth_code'] = $auth_code;
  149. $result = $pay->pos($params);
  150. break;
  151. case 'miniapp':
  152. //小程序支付,直接返回字符串
  153. //小程序支付必须要有openid
  154. $params['openid'] = $openid;
  155. $result = $pay->miniapp($params);
  156. break;
  157. default:
  158. }
  159. }
  160. //使用重写的Response类、RedirectResponse、Collection类
  161. if ($result instanceof \Symfony\Component\HttpFoundation\RedirectResponse) {
  162. $result = RedirectResponse::create($result->getTargetUrl());
  163. } elseif ($result instanceof \Symfony\Component\HttpFoundation\Response) {
  164. $result = Response::create($result->getContent());
  165. } elseif ($result instanceof \Yansongda\Supports\Collection) {
  166. $result = Collection::make($result->all());
  167. }
  168. return $result;
  169. }
  170. /**
  171. * 验证回调是否成功
  172. * @param string $type 支付类型
  173. * @param array $config 配置信息
  174. * @return bool|\Yansongda\Pay\Gateways\Alipay|\Yansongda\Pay\Gateways\Wechat
  175. */
  176. public static function checkNotify($type, $config = [])
  177. {
  178. $type = strtolower($type);
  179. if (!in_array($type, ['wechat', 'alipay'])) {
  180. return false;
  181. }
  182. try {
  183. $config = self::getConfig($type);
  184. $pay = $type == 'wechat' ? Pay::wechat($config) : Pay::alipay($config);
  185. $data = $pay->verify();
  186. if ($type == 'alipay') {
  187. if (in_array($data['trade_status'], ['TRADE_SUCCESS', 'TRADE_FINISHED'])) {
  188. return $pay;
  189. }
  190. } else {
  191. return $pay;
  192. }
  193. } catch (Exception $e) {
  194. return false;
  195. }
  196. return false;
  197. }
  198. /**
  199. * 验证返回是否成功,请勿用于判断是否支付成功的逻辑验证
  200. * 已弃用
  201. *
  202. * @param string $type 支付类型
  203. * @param array $config 配置信息
  204. * @return bool
  205. * @deprecated 已弃用,请勿用于逻辑验证
  206. */
  207. public static function checkReturn($type, $config = [])
  208. {
  209. //由于PC及移动端无法获取请求的参数信息,取消return验证,均返回true
  210. return true;
  211. }
  212. /**
  213. * 获取配置
  214. * @param string $type 支付类型
  215. * @return array|mixed
  216. */
  217. public static function getConfig($type = 'wechat')
  218. {
  219. $config = get_addon_config('epay');
  220. $config = isset($config[$type]) ? $config[$type] : $config['wechat'];
  221. if ($config['log']) {
  222. $config['log'] = [
  223. 'file' => LOG_PATH . 'epaylogs' . DS . $type . '-' . date("Y-m-d") . '.log',
  224. 'level' => 'debug'
  225. ];
  226. }
  227. if (isset($config['cert_client']) && substr($config['cert_client'], 0, 8) == '/addons/') {
  228. $config['cert_client'] = ROOT_PATH . str_replace('/', DS, substr($config['cert_client'], 1));
  229. }
  230. if (isset($config['cert_key']) && substr($config['cert_key'], 0, 8) == '/addons/') {
  231. $config['cert_key'] = ROOT_PATH . str_replace('/', DS, substr($config['cert_key'], 1));
  232. }
  233. // 可选
  234. $config['http'] = [
  235. 'timeout' => 10,
  236. 'connect_timeout' => 10,
  237. // 更多配置项请参考 [Guzzle](https://guzzle-cn.readthedocs.io/zh_CN/latest/request-options.html)
  238. ];
  239. $config['notify_url'] = empty($config['notify_url']) ? addon_url('epay/api/notifyx', [], false) . '/type/' . $type : $config['notify_url'];
  240. $config['notify_url'] = !preg_match("/^(http:\/\/|https:\/\/)/i", $config['notify_url']) ? request()->root(true) . $config['notify_url'] : $config['notify_url'];
  241. $config['return_url'] = empty($config['return_url']) ? addon_url('epay/api/returnx', [], false) . '/type/' . $type : $config['return_url'];
  242. $config['return_url'] = !preg_match("/^(http:\/\/|https:\/\/)/i", $config['return_url']) ? request()->root(true) . $config['return_url'] : $config['return_url'];
  243. return $config;
  244. }
  245. /**
  246. * 获取微信Openid
  247. *
  248. * @return mixed|string
  249. */
  250. public static function getOpenid()
  251. {
  252. $config = self::getConfig('wechat');
  253. $openid = '';
  254. $auth = Auth::instance();
  255. if ($auth->isLogin()) {
  256. $third = get_addon_info('third');
  257. if ($third && $third['state']) {
  258. $thirdInfo = Third::where('user_id', $auth->id)->where('platform', 'wechat')->where('apptype', 'mp')->find();
  259. $openid = $thirdInfo ? $thirdInfo['openid'] : '';
  260. }
  261. }
  262. if (!$openid) {
  263. $openid = Session::get("openid");
  264. //如果未传openid,则去读取openid
  265. if (!$openid) {
  266. $wechat = new Wechat($config['app_id'], $config['app_secret']);
  267. $openid = $wechat->getOpenid();
  268. }
  269. }
  270. return $openid;
  271. }
  272. }