Service.php 21.4 KB
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538
<?php

namespace addons\epay\library;

use addons\third\model\Third;
use app\common\library\Auth;
use Exception;
use think\Hook;
use think\Session;
use Yansongda\Pay\Pay;
use Yansongda\Supports\Str;

/**
 * 订单服务类
 *
 * @package addons\epay\library
 */
class Service
{

    public const SDK_VERSION_V2 = 'v2';

    public const SDK_VERSION_V3 = 'v3';

    /**
     * 提交订单
     * @param array|float $amount    订单金额
     * @param string      $orderid   订单号
     * @param string      $type      支付类型,可选alipay或wechat
     * @param string      $title     订单标题
     * @param string      $notifyurl 通知回调URL
     * @param string      $returnurl 跳转返回URL
     * @param string      $method    支付方法
     * @param string      $openid    Openid
     * @param array       $custom    自定义微信支付宝相关配置
     * @return Response|RedirectResponse|Collection
     * @throws Exception
     */
    public static function submitOrder($amount, $orderid = null, $type = null, $title = null, $notifyurl = null, $returnurl = null, $method = null, $openid = '', $custom = [])
    {
        $version = self::getSdkVersion();
        $request = request();
        $addonConfig = get_addon_config('epay');

        if (!is_array($amount)) {
            $params = [
                'amount'    => $amount,
                'orderid'   => $orderid,
                'type'      => $type,
                'title'     => $title,
                'notifyurl' => $notifyurl,
                'returnurl' => $returnurl,
                'method'    => $method,
                'openid'    => $openid,
                'custom'    => $custom,
            ];
        } else {
            $params = $amount;
        }
        $type = isset($params['type']) && in_array($params['type'], ['alipay', 'wechat']) ? $params['type'] : 'wechat';
        $method = $params['method'] ?? 'web';
        $orderid = $params['orderid'] ?? date("YmdHis") . mt_rand(100000, 999999);
        $amount = $params['amount'] ?? 1;
        $title = $params['title'] ?? "支付";
        $auth_code = $params['auth_code'] ?? '';
        $openid = $params['openid'] ?? '';

        //自定义微信支付宝相关配置
        $custom = $params['custom'] ?? [];

        //未定义则使用默认回调和跳转
        $notifyurl = !empty($params['notifyurl']) ? $params['notifyurl'] : $request->root(true) . '/addons/epay/index/notifyx/paytype/' . $type;
        $returnurl = !empty($params['returnurl']) ? $params['returnurl'] : $request->root(true) . '/addons/epay/index/returnx/paytype/' . $type . '/out_trade_no/' . $orderid;

        $html = '';
        $config = Service::getConfig($type, array_merge($custom, ['notify_url' => $notifyurl, 'return_url' => $returnurl]));

        //判断是否移动端或微信内浏览器
        $isMobile = $request->isMobile();
        $isWechat = strpos($request->server('HTTP_USER_AGENT'), 'MicroMessenger') !== false;

        $result = null;
        if ($type == 'alipay') {
            //如果是PC支付,判断当前环境,进行跳转
            if ($method == 'web') {
                //如果是微信环境或后台配置PC使用扫码支付
                if ($isWechat || $addonConfig['alipay']['scanpay']) {
                    Session::set("alipayorderdata", $params);
                    $url = addon_url('epay/api/alipay', [], true, true);
                    return new RedirectResponse($url);
                } elseif ($isMobile) {
                    $method = 'wap';
                }
            }

            //创建支付对象
            $pay = Pay::alipay($config);
            $params = [
                'out_trade_no' => $orderid,//你的订单号
                'total_amount' => $amount,//单位元
                'subject'      => $title,
            ];

            switch ($method) {
                case 'web':
                    //电脑支付
                    $result = $pay->web($params);
                    break;
                case 'wap':
                    //手机网页支付
                    $result = $pay->wap($params);
                    break;
                case 'app':
                    //APP支付
                    $result = $pay->app($params);
                    break;
                case 'scan':
                    //扫码支付
                    $result = $pay->scan($params);
                    break;
                case 'pos':
                    //刷卡支付必须要有auth_code
                    $params['auth_code'] = $auth_code;
                    $result = $pay->pos($params);
                    break;
                case 'mini':
                case 'miniapp':
                    //小程序支付
                    //小程序支付,直接返回字符串
                    //小程序支付必须要有buyer_id,这里使用openid
                    $params['buyer_id'] = $openid;
                    $result = $pay->mini($params);
                    break;
                default:
            }
        } else {
            //如果是PC支付,判断当前环境,进行跳转
            if ($method == 'web') {
                //如果是移动端,但不是微信环境
                if ($isMobile && !$isWechat) {
                    $method = 'wap';
                } else {
                    Session::set("wechatorderdata", $params);
                    $url = addon_url('epay/api/wechat', [], true, true);
                    return new RedirectResponse($url);
                }
            }

            //单位分
            $total_fee = function_exists('bcmul') ? bcmul($amount, 100) : $amount * 100;
            $total_fee = (int)$total_fee;
            $ip = $request->ip();
            //微信服务商模式时需传递sub_openid参数
            $openidName = $addonConfig['wechat']['mode'] == 'service' ? 'sub_openid' : 'openid';

            //创建支付对象
            $pay = Pay::wechat($config);

            if (self::isVersionV3()) {
                //V3支付
                $params = [
                    'out_trade_no' => $orderid,
                    'description'  => $title,
                    'amount'       => [
                        'total' => $total_fee,
                    ]
                ];
                switch ($method) {
                    case 'mp':
                        //公众号支付
                        //公众号支付必须有openid
                        $params['payer'] = [$openidName => $openid];
                        $result = $pay->mp($params);
                        break;
                    case 'wap':
                        //手机网页支付,跳转
                        $params['scene_info'] = [
                            'payer_client_ip' => $ip,
                            'h5_info'         => [
                                'type' => 'Wap',
                            ]
                        ];
                        $result = $pay->wap($params);
                        break;
                    case 'app':
                        //APP支付,直接返回字符串
                        $result = $pay->app($params);
                        break;
                    case 'scan':
                        //扫码支付,直接返回字符串
                        $result = $pay->scan($params);
                        break;
                    case 'pos':
                        //刷卡支付,直接返回字符串
                        //刷卡支付必须要有auth_code
                        $params['auth_code'] = $auth_code;
                        $result = $pay->pos($params);
                        break;
                    case 'mini':
                    case 'miniapp':
                        //小程序支付,直接返回字符串
                        //小程序支付必须要有openid
                        $params['payer'] = [$openidName => $openid];
                        $result = $pay->mini($params);
                        break;
                    default:
                }
            } else {
                //V2支付
                $params = [
                    'out_trade_no' => $orderid,
                    'body'         => $title,
                    'total_fee'    => $total_fee,
                ];
                switch ($method) {
                    case 'mp':
                        //公众号支付
                        //公众号支付必须有openid
                        $params[$openidName] = $openid;
                        $result = $pay->mp($params);
                        break;
                    case 'wap':
                        //手机网页支付,跳转
                        $params['spbill_create_ip'] = $ip;
                        $result = $pay->wap($params);
                        break;
                    case 'app':
                        //APP支付,直接返回字符串
                        $result = $pay->app($params);
                        break;
                    case 'scan':
                        //扫码支付,直接返回字符串
                        $result = $pay->scan($params);
                        break;
                    case 'pos':
                        //刷卡支付,直接返回字符串
                        //刷卡支付必须要有auth_code
                        $params['auth_code'] = $auth_code;
                        $result = $pay->pos($params);
                        break;
                    case 'mini':
                    case 'miniapp':
                        //小程序支付,直接返回字符串
                        //小程序支付必须要有openid
                        $params[$openidName] = $openid;
                        $result = $pay->miniapp($params);
                        break;
                    default:
                }
            }
        }

        //使用重写的Response类、RedirectResponse、Collection类
        if ($result instanceof \Symfony\Component\HttpFoundation\RedirectResponse) {
            $result = new RedirectResponse($result->getTargetUrl());
        } elseif ($result instanceof \Symfony\Component\HttpFoundation\Response) {
            $result = new Response($result->getContent());
        } elseif ($result instanceof \Yansongda\Supports\Collection) {
            $result = Collection::make($result->all());
        } elseif ($result instanceof \GuzzleHttp\Psr7\Response) {
            $result = new Response($result->getBody());
        }

        return $result;
    }

    /**
     * 验证回调是否成功
     * @param string $type   支付类型
     * @param array  $custom 自定义配置信息
     * @return bool|\Yansongda\Pay\Gateways\Alipay|\Yansongda\Pay\Gateways\Wechat|\Yansongda\Pay\Provider\Wechat|\Yansongda\Pay\Provider\Alipay
     */
    public static function checkNotify($type, $custom = [])
    {
        $type = strtolower($type);
        if (!in_array($type, ['wechat', 'alipay'])) {
            return false;
        }

        $version = self::getSdkVersion();

        try {
            $config = self::getConfig($type, $custom);
            $pay = $type == 'wechat' ? Pay::wechat($config) : Pay::alipay($config);

            $data = Service::isVersionV3() ? $pay->callback() : $pay->verify();
            if ($type == 'alipay') {
                if (in_array($data['trade_status'], ['TRADE_SUCCESS', 'TRADE_FINISHED'])) {
                    return $pay;
                }
            } else {
                return $pay;
            }
        } catch (Exception $e) {
            \think\Log::record("回调请求参数解析错误", "error");
            return false;
        }

        return false;
    }

    /**
     * 验证返回是否成功,请勿用于判断是否支付成功的逻辑验证
     * 已弃用
     *
     * @param string $type   支付类型
     * @param array  $custom 自定义配置信息
     * @return bool
     * @deprecated  已弃用,请勿用于逻辑验证
     */
    public static function checkReturn($type, $custom = [])
    {
        //由于PC及移动端无法获取请求的参数信息,取消return验证,均返回true
        return true;
    }

    /**
     * 获取配置
     * @param string $type   支付类型
     * @param array  $custom 自定义配置,用于覆盖插件默认配置
     * @return array
     */
    public static function getConfig($type = 'wechat', $custom = [])
    {
        $addonConfig = get_addon_config('epay');
        $config = $addonConfig[$type] ?? $addonConfig['wechat'];

        // SDK版本
        $version = self::getSdkVersion();

        if (isset($config['cert_client']) && substr($config['cert_client'], 0, 8) == '/addons/') {
            $config['cert_client'] = ROOT_PATH . str_replace('/', DS, substr($config['cert_client'], 1));
        }
        if (isset($config['cert_key']) && substr($config['cert_key'], 0, 8) == '/addons/') {
            $config['cert_key'] = ROOT_PATH . str_replace('/', DS, substr($config['cert_key'], 1));
        }
        if (isset($config['app_cert_public_key']) && substr($config['app_cert_public_key'], 0, 8) == '/addons/') {
            $config['app_cert_public_key'] = ROOT_PATH . str_replace('/', DS, substr($config['app_cert_public_key'], 1));
        }
        if (isset($config['alipay_root_cert']) && substr($config['alipay_root_cert'], 0, 8) == '/addons/') {
            $config['alipay_root_cert'] = ROOT_PATH . str_replace('/', DS, substr($config['alipay_root_cert'], 1));
        }
        if (isset($config['ali_public_key']) && (Str::endsWith($config['ali_public_key'], '.crt') || Str::endsWith($config['ali_public_key'], '.pem'))) {
            $config['ali_public_key'] = ROOT_PATH . str_replace('/', DS, substr($config['ali_public_key'], 1));
        }

        // V3支付
        if (self::isVersionV3()) {
            if ($type == 'wechat') {
                $config['app_id'] = $config['appid'] ?? '';
                $config['mp_app_id'] = $config['app_id'] ?? '';
                $config['mini_app_id'] = $config['miniapp_id'] ?? '';
                $config['combine_mch_id'] = $config['combine_mch_id'] ?? '';
                $config['mch_secret_key'] = $config['key_v3'] ?? '';
                $config['mch_secret_cert'] = $config['cert_key'];
                $config['mch_public_cert_path'] = $config['cert_client'];

                $config['sub_mp_app_id'] = $config['sub_appid'] ?? '';
                $config['sub_app_id'] = $config['sub_app_id'] ?? '';
                $config['sub_mini_app_id'] = $config['sub_miniapp_id'] ?? '';
                $config['sub_mch_id'] = $config['sub_mch_id'] ?? '';
            } elseif ($type == 'alipay') {
                $config['app_secret_cert'] = $config['private_key'] ?? '';
                $config['app_public_cert_path'] = $config['app_cert_public_key'] ?? '';
                $config['alipay_public_cert_path'] = $config['ali_public_key'] ?? '';
                $config['alipay_root_cert_path'] = $config['alipay_root_cert'] ?? '';
                $config['service_provider_id'] = $config['pid'] ?? '';
            }
            $modeArr = ['normal' => 0, 'dev' => 1, 'service' => 2];
            $config['mode'] = $modeArr[$config['mode']] ?? 0;
        }

        // 日志
        if ($config['log']) {
            $config['log'] = [
                'enable' => true,
                'file'   => LOG_PATH . 'epaylogs' . DS . $type . '-' . date("Y-m-d") . '.log',
                'level'  => 'debug'
            ];
        } else {
            $config['log'] = [
                'enable' => false,
            ];
        }

        // GuzzleHttp配置,可选
        $config['http'] = [
            'timeout'         => 10,
            'connect_timeout' => 10,
            // 更多配置项请参考 [Guzzle](https://guzzle-cn.readthedocs.io/zh_CN/latest/request-options.html)
        ];

        $config['notify_url'] = empty($config['notify_url']) ? addon_url('epay/api/notifyx', [], false) . '/type/' . $type : $config['notify_url'];
        $config['notify_url'] = !preg_match("/^(http:\/\/|https:\/\/)/i", $config['notify_url']) ? request()->root(true) . $config['notify_url'] : $config['notify_url'];
        $config['return_url'] = empty($config['return_url']) ? addon_url('epay/api/returnx', [], false) . '/type/' . $type : $config['return_url'];
        $config['return_url'] = !preg_match("/^(http:\/\/|https:\/\/)/i", $config['return_url']) ? request()->root(true) . $config['return_url'] : $config['return_url'];

        //合并自定义配置
        $config = array_merge($config, $custom);

        //v3版本时返回的结构不同
        if (self::isVersionV3()) {
            $config = [$type => ['default' => $config], 'logger' => $config['log'], 'http' => $config['http'], '_force' => true];

        }
        return $config;
    }

    /**
     * 获取微信Openid
     *
     * @param array $custom 自定义配置信息
     * @return mixed|string
     */
    public static function getOpenid($custom = [])
    {
        $openid = '';
        $auth = Auth::instance();
        if ($auth->isLogin()) {
            $third = get_addon_info('third');
            if ($third && $third['state']) {
                $thirdInfo = Third::where('user_id', $auth->id)->where('platform', 'wechat')->where('apptype', 'mp')->find();
                $openid = $thirdInfo ? $thirdInfo['openid'] : '';
            }
        }
        if (!$openid) {
            $openid = Session::get("openid");

            //如果未传openid,则去读取openid
            if (!$openid) {
                $addonConfig = get_addon_config('epay');
                $wechat = new Wechat($custom['app_id'] ?? $addonConfig['wechat']['app_id'], $custom['app_secret'] ?? $addonConfig['wechat']['app_secret']);
                $openid = $wechat->getOpenid();
            }
        }
        return $openid;
    }

    /**
     * 获取SDK版本
     * @return mixed|string
     */
    public static function getSdkVersion()
    {
        $addonConfig = get_addon_config('epay');
        return $addonConfig['version'] ?? self::SDK_VERSION_V2;
    }

    /**
     * 判断是否V2支付
     * @return bool
     */
    public static function isVersionV2()
    {
        return self::getSdkVersion() === self::SDK_VERSION_V2;
    }

    /**
     * 判断是否V3支付
     * @return bool
     */
    public static function isVersionV3()
    {
        return self::getSdkVersion() === self::SDK_VERSION_V3;
    }
    /**
     * 提交退款订单
     * @param array|float $amount 订单金额
     * @param array|float $refund_money 退款金额
     * @param string $orderid 订单号
     * @param string $refund_sn 退款订单号
     * @param string $type 支付类型,可选alipay或wechat
     * @param string $remark 退款原因
     * @param string $notifyurl 通知回调URL
     * @param string $returnurl 跳转返回URL
     * @param string $method 支付方式
     * @return Response|RedirectResponse|Collection
     * @throws Exception
     */
    public static function submitRefund($amount = null, $refund_money, $orderid, $refund_sn, $type, $remark = null, $notifyurl = null, $returnurl = null, $method = 'miniapp')
    {
        if (!is_array($amount)) {
            $params = [
                'amount' => $amount,
                'type' => $type,
                'notifyurl' => $notifyurl,
                'returnurl' => $returnurl,
                'method' => $method,
            ];
        } else {
            $params = $amount;
        }
        $type = isset($params['type']) && in_array($params['type'], ['alipay', 'wechat']) ? $params['type'] : 'wechat';
        $request = request();
        //$notifyurl = isset($params['notifyurl']) ? $params['notifyurl'] : $request->root(true) . '/addons/epay/index/' . $type . 'RfdNotify';
        // $returnurl = isset($params['returnurl']) ? $params['returnurl'] : $request->root(true) . '/addons/epay/index/' . $type . 'return/out_trade_no/' . $orderid;
        //$notifyurl = $this->request->domain() . '/api/index/refundNotifyx';
        $config = Service::getConfig($type);
        $config['notify_url'] = $notifyurl;
        $config['return_url'] = $returnurl;
        $result = null;
        //退款参数
        $order_data = [
            'out_trade_no' => $orderid,//原订单号
        ];
        if ($type == 'wechat') {
            //创建支付对象
            $pay = Pay::wechat($config);
            $total_fee = $amount * 100;
            $refund_fee = $refund_money * 100;
            $order_data = array_merge($order_data, [
                'out_refund_no' => $refund_sn,//退款订单号
                'total_fee' => $total_fee,//支付金额
                'refund_fee' => $refund_fee,//退款金额
                'refund_desc' => $remark,//退款原因
                'type' => $method  //支付方式
            ]);
        } else {
            $pay = Pay::alipay($config);
            $order_data = array_merge($order_data, [
                'out_request_no' => $refund_sn,//退款订单号
                'refund_amount' => $refund_money,
            ]);
        }

        $result = $pay->refund($order_data);

        //使用重写的Response类、RedirectResponse、Collection类
        if ($result instanceof \Symfony\Component\HttpFoundation\RedirectResponse) {
            $result = RedirectResponse::create($result->getTargetUrl());
        } elseif ($result instanceof \Symfony\Component\HttpFoundation\Response) {
            $result = Response::create($result->getContent());
        } elseif ($result instanceof \Yansongda\Supports\Collection) {
            $result = Collection::make($result->all());
        }
        return $result;
    }
}