Service.php 12 KB

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