Service.php 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466
  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\Hook;
  7. use think\Session;
  8. use Yansongda\Pay\Pay;
  9. use Yansongda\Supports\Str;
  10. /**
  11. * 订单服务类
  12. *
  13. * @package addons\epay\library
  14. */
  15. class Service
  16. {
  17. public const SDK_VERSION_V2 = 'v2';
  18. public const SDK_VERSION_V3 = 'v3';
  19. /**
  20. * 提交订单
  21. * @param array|float $amount 订单金额
  22. * @param string $orderid 订单号
  23. * @param string $type 支付类型,可选alipay或wechat
  24. * @param string $title 订单标题
  25. * @param string $notifyurl 通知回调URL
  26. * @param string $returnurl 跳转返回URL
  27. * @param string $method 支付方法
  28. * @param string $openid Openid
  29. * @param array $custom 自定义微信支付宝相关配置
  30. * @return Response|RedirectResponse|Collection
  31. * @throws Exception
  32. */
  33. public static function submitOrder($amount, $orderid = null, $type = null, $title = null, $notifyurl = null, $returnurl = null, $method = null, $openid = '', $custom = [])
  34. {
  35. $version = self::getSdkVersion();
  36. $request = request();
  37. $addonConfig = get_addon_config('epay');
  38. if (!is_array($amount)) {
  39. $params = [
  40. 'amount' => $amount,
  41. 'orderid' => $orderid,
  42. 'type' => $type,
  43. 'title' => $title,
  44. 'notifyurl' => $notifyurl,
  45. 'returnurl' => $returnurl,
  46. 'method' => $method,
  47. 'openid' => $openid,
  48. 'custom' => $custom,
  49. ];
  50. } else {
  51. $params = $amount;
  52. }
  53. $type = isset($params['type']) && in_array($params['type'], ['alipay', 'wechat']) ? $params['type'] : 'wechat';
  54. $method = $params['method'] ?? 'web';
  55. $orderid = $params['orderid'] ?? date("YmdHis") . mt_rand(100000, 999999);
  56. $amount = $params['amount'] ?? 1;
  57. $title = $params['title'] ?? "支付";
  58. $auth_code = $params['auth_code'] ?? '';
  59. $openid = $params['openid'] ?? '';
  60. //自定义微信支付宝相关配置
  61. $custom = $params['custom'] ?? [];
  62. //未定义则使用默认回调和跳转
  63. $notifyurl = !empty($params['notifyurl']) ? $params['notifyurl'] : $request->root(true) . '/addons/epay/index/notifyx/paytype/' . $type;
  64. $returnurl = !empty($params['returnurl']) ? $params['returnurl'] : $request->root(true) . '/addons/epay/index/returnx/paytype/' . $type . '/out_trade_no/' . $orderid;
  65. $html = '';
  66. $config = Service::getConfig($type, array_merge($custom, ['notify_url' => $notifyurl, 'return_url' => $returnurl]));
  67. //判断是否移动端或微信内浏览器
  68. $isMobile = $request->isMobile();
  69. $isWechat = strpos($request->server('HTTP_USER_AGENT'), 'MicroMessenger') !== false;
  70. $result = null;
  71. if ($type == 'alipay') {
  72. //如果是PC支付,判断当前环境,进行跳转
  73. if ($method == 'web') {
  74. //如果是微信环境或后台配置PC使用扫码支付
  75. if ($isWechat || $addonConfig['alipay']['scanpay']) {
  76. Session::set("alipayorderdata", $params);
  77. $url = addon_url('epay/api/alipay', [], true, true);
  78. return new RedirectResponse($url);
  79. } elseif ($isMobile) {
  80. $method = 'wap';
  81. }
  82. }
  83. //创建支付对象
  84. $pay = Pay::alipay($config);
  85. $params = [
  86. 'out_trade_no' => $orderid,//你的订单号
  87. 'total_amount' => $amount,//单位元
  88. 'subject' => $title,
  89. ];
  90. switch ($method) {
  91. case 'web':
  92. //电脑支付
  93. $result = $pay->web($params);
  94. break;
  95. case 'wap':
  96. //手机网页支付
  97. $result = $pay->wap($params);
  98. break;
  99. case 'app':
  100. //APP支付
  101. $result = $pay->app($params);
  102. break;
  103. case 'scan':
  104. //扫码支付
  105. $result = $pay->scan($params);
  106. break;
  107. case 'pos':
  108. //刷卡支付必须要有auth_code
  109. $params['auth_code'] = $auth_code;
  110. $result = $pay->pos($params);
  111. break;
  112. case 'mini':
  113. case 'miniapp':
  114. //小程序支付
  115. //小程序支付,直接返回字符串
  116. //小程序支付必须要有buyer_id,这里使用openid
  117. $params['buyer_id'] = $openid;
  118. $result = $pay->mini($params);
  119. break;
  120. default:
  121. }
  122. } else {
  123. //如果是PC支付,判断当前环境,进行跳转
  124. if ($method == 'web') {
  125. //如果是移动端,但不是微信环境
  126. if ($isMobile && !$isWechat) {
  127. $method = 'wap';
  128. } else {
  129. Session::set("wechatorderdata", $params);
  130. $url = addon_url('epay/api/wechat', [], true, true);
  131. return new RedirectResponse($url);
  132. }
  133. }
  134. //单位分
  135. $total_fee = function_exists('bcmul') ? bcmul($amount, 100) : $amount * 100;
  136. $total_fee = (int)$total_fee;
  137. $ip = $request->ip();
  138. //微信服务商模式时需传递sub_openid参数
  139. $openidName = $addonConfig['wechat']['mode'] == 'service' ? 'sub_openid' : 'openid';
  140. //创建支付对象
  141. $pay = Pay::wechat($config);
  142. if (self::isVersionV3()) {
  143. //V3支付
  144. $params = [
  145. 'out_trade_no' => $orderid,
  146. 'description' => $title,
  147. 'amount' => [
  148. 'total' => $total_fee,
  149. ]
  150. ];
  151. switch ($method) {
  152. case 'mp':
  153. //公众号支付
  154. //公众号支付必须有openid
  155. $params['payer'] = [$openidName => $openid];
  156. $result = $pay->mp($params);
  157. break;
  158. case 'wap':
  159. //手机网页支付,跳转
  160. $params['scene_info'] = [
  161. 'payer_client_ip' => $ip,
  162. 'h5_info' => [
  163. 'type' => 'Wap',
  164. ]
  165. ];
  166. $result = $pay->wap($params);
  167. break;
  168. case 'app':
  169. //APP支付,直接返回字符串
  170. $result = $pay->app($params);
  171. break;
  172. case 'scan':
  173. //扫码支付,直接返回字符串
  174. $result = $pay->scan($params);
  175. break;
  176. case 'pos':
  177. //刷卡支付,直接返回字符串
  178. //刷卡支付必须要有auth_code
  179. $params['auth_code'] = $auth_code;
  180. $result = $pay->pos($params);
  181. break;
  182. case 'mini':
  183. case 'miniapp':
  184. //小程序支付,直接返回字符串
  185. //小程序支付必须要有openid
  186. $params['payer'] = [$openidName => $openid];
  187. $result = $pay->mini($params);
  188. break;
  189. default:
  190. }
  191. } else {
  192. //V2支付
  193. $params = [
  194. 'out_trade_no' => $orderid,
  195. 'body' => $title,
  196. 'total_fee' => $total_fee,
  197. ];
  198. switch ($method) {
  199. case 'mp':
  200. //公众号支付
  201. //公众号支付必须有openid
  202. $params[$openidName] = $openid;
  203. $result = $pay->mp($params);
  204. break;
  205. case 'wap':
  206. //手机网页支付,跳转
  207. $params['spbill_create_ip'] = $ip;
  208. $result = $pay->wap($params);
  209. break;
  210. case 'app':
  211. //APP支付,直接返回字符串
  212. $result = $pay->app($params);
  213. break;
  214. case 'scan':
  215. //扫码支付,直接返回字符串
  216. $result = $pay->scan($params);
  217. break;
  218. case 'pos':
  219. //刷卡支付,直接返回字符串
  220. //刷卡支付必须要有auth_code
  221. $params['auth_code'] = $auth_code;
  222. $result = $pay->pos($params);
  223. break;
  224. case 'mini':
  225. case 'miniapp':
  226. //小程序支付,直接返回字符串
  227. //小程序支付必须要有openid
  228. $params[$openidName] = $openid;
  229. $result = $pay->miniapp($params);
  230. break;
  231. default:
  232. }
  233. }
  234. }
  235. //使用重写的Response类、RedirectResponse、Collection类
  236. if ($result instanceof \Symfony\Component\HttpFoundation\RedirectResponse) {
  237. $result = new RedirectResponse($result->getTargetUrl());
  238. } elseif ($result instanceof \Symfony\Component\HttpFoundation\Response) {
  239. $result = new Response($result->getContent());
  240. } elseif ($result instanceof \Yansongda\Supports\Collection) {
  241. $result = Collection::make($result->all());
  242. } elseif ($result instanceof \GuzzleHttp\Psr7\Response) {
  243. $result = new Response($result->getBody());
  244. }
  245. return $result;
  246. }
  247. /**
  248. * 验证回调是否成功
  249. * @param string $type 支付类型
  250. * @param array $custom 自定义配置信息
  251. * @return bool|\Yansongda\Pay\Gateways\Alipay|\Yansongda\Pay\Gateways\Wechat|\Yansongda\Pay\Provider\Wechat|\Yansongda\Pay\Provider\Alipay
  252. */
  253. public static function checkNotify($type, $custom = [])
  254. {
  255. $type = strtolower($type);
  256. if (!in_array($type, ['wechat', 'alipay'])) {
  257. return false;
  258. }
  259. $version = self::getSdkVersion();
  260. try {
  261. $config = self::getConfig($type, $custom);
  262. $pay = $type == 'wechat' ? Pay::wechat($config) : Pay::alipay($config);
  263. $data = Service::isVersionV3() ? $pay->callback() : $pay->verify();
  264. if ($type == 'alipay') {
  265. if (in_array($data['trade_status'], ['TRADE_SUCCESS', 'TRADE_FINISHED'])) {
  266. return $pay;
  267. }
  268. } else {
  269. return $pay;
  270. }
  271. } catch (Exception $e) {
  272. \think\Log::record("回调请求参数解析错误", "error");
  273. return false;
  274. }
  275. return false;
  276. }
  277. /**
  278. * 验证返回是否成功,请勿用于判断是否支付成功的逻辑验证
  279. * 已弃用
  280. *
  281. * @param string $type 支付类型
  282. * @param array $custom 自定义配置信息
  283. * @return bool
  284. * @deprecated 已弃用,请勿用于逻辑验证
  285. */
  286. public static function checkReturn($type, $custom = [])
  287. {
  288. //由于PC及移动端无法获取请求的参数信息,取消return验证,均返回true
  289. return true;
  290. }
  291. /**
  292. * 获取配置
  293. * @param string $type 支付类型
  294. * @param array $custom 自定义配置,用于覆盖插件默认配置
  295. * @return array
  296. */
  297. public static function getConfig($type = 'wechat', $custom = [])
  298. {
  299. $addonConfig = get_addon_config('epay');
  300. $config = $addonConfig[$type] ?? $addonConfig['wechat'];
  301. // SDK版本
  302. $version = self::getSdkVersion();
  303. if (isset($config['cert_client']) && substr($config['cert_client'], 0, 8) == '/addons/') {
  304. $config['cert_client'] = ROOT_PATH . str_replace('/', DS, substr($config['cert_client'], 1));
  305. }
  306. if (isset($config['cert_key']) && substr($config['cert_key'], 0, 8) == '/addons/') {
  307. $config['cert_key'] = ROOT_PATH . str_replace('/', DS, substr($config['cert_key'], 1));
  308. }
  309. if (isset($config['app_cert_public_key']) && substr($config['app_cert_public_key'], 0, 8) == '/addons/') {
  310. $config['app_cert_public_key'] = ROOT_PATH . str_replace('/', DS, substr($config['app_cert_public_key'], 1));
  311. }
  312. if (isset($config['alipay_root_cert']) && substr($config['alipay_root_cert'], 0, 8) == '/addons/') {
  313. $config['alipay_root_cert'] = ROOT_PATH . str_replace('/', DS, substr($config['alipay_root_cert'], 1));
  314. }
  315. if (isset($config['ali_public_key']) && (Str::endsWith($config['ali_public_key'], '.crt') || Str::endsWith($config['ali_public_key'], '.pem'))) {
  316. $config['ali_public_key'] = ROOT_PATH . str_replace('/', DS, substr($config['ali_public_key'], 1));
  317. }
  318. // V3支付
  319. if (self::isVersionV3()) {
  320. if ($type == 'wechat') {
  321. $config['mp_app_id'] = $config['app_id'] ?? '';
  322. $config['app_id'] = $config['appid'] ?? '';
  323. $config['mini_app_id'] = $config['miniapp_id'] ?? '';
  324. $config['combine_mch_id'] = $config['combine_mch_id'] ?? '';
  325. $config['mch_secret_key'] = $config['key_v3'] ?? '';
  326. $config['mch_secret_cert'] = $config['cert_key'];
  327. $config['mch_public_cert_path'] = $config['cert_client'];
  328. $config['sub_mp_app_id'] = $config['sub_appid'] ?? '';
  329. $config['sub_app_id'] = $config['sub_app_id'] ?? '';
  330. $config['sub_mini_app_id'] = $config['sub_miniapp_id'] ?? '';
  331. $config['sub_mch_id'] = $config['sub_mch_id'] ?? '';
  332. } elseif ($type == 'alipay') {
  333. $config['app_secret_cert'] = $config['private_key'] ?? '';
  334. $config['app_public_cert_path'] = $config['app_cert_public_key'] ?? '';
  335. $config['alipay_public_cert_path'] = $config['ali_public_key'] ?? '';
  336. $config['alipay_root_cert_path'] = $config['alipay_root_cert'] ?? '';
  337. $config['service_provider_id'] = $config['pid'] ?? '';
  338. }
  339. $modeArr = ['normal' => 0, 'dev' => 1, 'service' => 2];
  340. $config['mode'] = $modeArr[$config['mode']] ?? 0;
  341. }
  342. // 日志
  343. if ($config['log']) {
  344. $config['log'] = [
  345. 'enable' => true,
  346. 'file' => LOG_PATH . 'epaylogs' . DS . $type . '-' . date("Y-m-d") . '.log',
  347. 'level' => 'debug'
  348. ];
  349. } else {
  350. $config['log'] = [
  351. 'enable' => false,
  352. ];
  353. }
  354. // GuzzleHttp配置,可选
  355. $config['http'] = [
  356. 'timeout' => 10,
  357. 'connect_timeout' => 10,
  358. // 更多配置项请参考 [Guzzle](https://guzzle-cn.readthedocs.io/zh_CN/latest/request-options.html)
  359. ];
  360. $config['notify_url'] = empty($config['notify_url']) ? addon_url('epay/api/notifyx', [], false) . '/type/' . $type : $config['notify_url'];
  361. $config['notify_url'] = !preg_match("/^(http:\/\/|https:\/\/)/i", $config['notify_url']) ? request()->root(true) . $config['notify_url'] : $config['notify_url'];
  362. $config['return_url'] = empty($config['return_url']) ? addon_url('epay/api/returnx', [], false) . '/type/' . $type : $config['return_url'];
  363. $config['return_url'] = !preg_match("/^(http:\/\/|https:\/\/)/i", $config['return_url']) ? request()->root(true) . $config['return_url'] : $config['return_url'];
  364. //合并自定义配置
  365. $config = array_merge($config, $custom);
  366. //v3版本时返回的结构不同
  367. if (self::isVersionV3()) {
  368. $config = [$type => ['default' => $config], 'logger' => $config['log'], 'http' => $config['http'], '_force' => true];
  369. }
  370. return $config;
  371. }
  372. /**
  373. * 获取微信Openid
  374. *
  375. * @param array $custom 自定义配置信息
  376. * @return mixed|string
  377. */
  378. public static function getOpenid($custom = [])
  379. {
  380. $openid = '';
  381. $auth = Auth::instance();
  382. if ($auth->isLogin()) {
  383. $third = get_addon_info('third');
  384. if ($third && $third['state']) {
  385. $thirdInfo = Third::where('user_id', $auth->id)->where('platform', 'wechat')->where('apptype', 'mp')->find();
  386. $openid = $thirdInfo ? $thirdInfo['openid'] : '';
  387. }
  388. }
  389. if (!$openid) {
  390. $openid = Session::get("openid");
  391. //如果未传openid,则去读取openid
  392. if (!$openid) {
  393. $addonConfig = get_addon_config('epay');
  394. $wechat = new Wechat($custom['app_id'] ?? $addonConfig['wechat']['app_id'], $custom['app_secret'] ?? $addonConfig['wechat']['app_secret']);
  395. $openid = $wechat->getOpenid();
  396. }
  397. }
  398. return $openid;
  399. }
  400. /**
  401. * 获取SDK版本
  402. * @return mixed|string
  403. */
  404. public static function getSdkVersion()
  405. {
  406. $addonConfig = get_addon_config('epay');
  407. return $addonConfig['version'] ?? self::SDK_VERSION_V2;
  408. }
  409. /**
  410. * 判断是否V2支付
  411. * @return bool
  412. */
  413. public static function isVersionV2()
  414. {
  415. return self::getSdkVersion() === self::SDK_VERSION_V2;
  416. }
  417. /**
  418. * 判断是否V3支付
  419. * @return bool
  420. */
  421. public static function isVersionV3()
  422. {
  423. return self::getSdkVersion() === self::SDK_VERSION_V3;
  424. }
  425. }