Service.php 12 KB

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