Service.php 15 KB

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