THINKPHP 安装Yansongda 微信支付宝支付

半城伤御伤魂 提交于 2021-01-24 13:13:57

1、在vendor 目录使用composer安装 ,命令: composer require yansongda/pay -vvv

2、在控制器中引用 Yansongda\Pay\Pay

微 信支付:

public function done(){
    $config = [
        'appid' => 'wxb3fxxxxxxxxxxx', // APP APPID
        'app_id' => 'appid', // 公众号 APPID
        'miniapp_id' => 'wxb3fxxxxxxxxxxx', // 小程序 APPID
        'mch_id' => 'merchant', // 商户ID
        'key' => 'secretkey',
        'notify_url' => url('user/public/wechatNotify',[],true,true),
        'cert_client' => './cert/apiclient_cert.pem', // optional, 退款,红包等情况时需要用到
        'cert_key' => './cert/apiclient_key.pem',// optional, 退款,红包等情况时需要用到
        'log' => [ // optional
             'file' => LOG_PATH.'logs/wechat.log',
             'level' => 'info', // 建议生产环境等级调整为 info,开发环境为 debug
             'type' => 'single', // optional, 可选 daily.
             'max_file' => 30, // optional, 当 type 为 daily 时有效,默认 30 天
        ],
        'http' => [ // optional
            'timeout' => 5.0,
            'connect_timeout' => 5.0,
        ],
         // 'mode' => 'dev',
   ];
   // 使用方法
   $wechat = Pay::wechat($config);
   $order = [
      'out_trade_no' => $number,
      'body' => '支付',
      'total_fee'      => intval($order['money'] * 100),
   ];
   $result = $wechat->scan($order);
   // 二维码内容:
   $qr = $result->code_url;
   $url2   = base64_encode($qr);
   $this->assign('text', $url2);
   return $this->fetch();
}

微信支付回调:

public function wechatNotify()
    {
        $params = $this->request->param();
//        Log::record('aliPayNotify' . PHP_EOL . \json_encode($params), 'payment');
        try {
            $wechat_pay = cmf_get_option('wechat_pay');
            $config = [
                'appid' => 'wxb3fxxxxxxxxxxx', // APP APPID
                'app_id' => 'appid', // 公众号 APPID
                'miniapp_id' => 'wxb3fxxxxxxxxxxx', // 小程序 APPID
                'mch_id' => 'merchant',
                'key' => 'secretkey',
                'notify_url' => url('user/public/wechatNotify',[],true,true),
                'cert_client' => './cert/apiclient_cert.pem', // optional, 退款,红包等情况时需要用到
                'cert_key' => './cert/apiclient_key.pem',// optional, 退款,红包等情况时需要用到
                'log' => [ // optional
                    'file' => LOG_PATH.'logs/wechat.log',
                    'level' => 'info', // 建议生产环境等级调整为 info,开发环境为 debug
                    'type' => 'single', // optional, 可选 daily.
                    'max_file' => 30, // optional, 当 type 为 daily 时有效,默认 30 天
                ],
                'http' => [ // optional
                    'timeout' => 5.0,
                    'connect_timeout' => 5.0,
                    // 更多配置项请参考 [Guzzle](https://guzzle-cn.readthedocs.io/zh_CN/latest/request-options.html)
                ],
                // 'mode' => 'dev',
            ];

            $this->paywxService = Pay::wechat($config);
            $wechat = Pay::wechat($config);
            $verifyData       = $this->paywxService->verify()->toArray();
            // 业务逻辑处理方法
            if (true === $this->updateWechatPayNotify($verifyData)) {
                return $this->paywxService->success();
            }
        } catch (\Exception $e) {
            $err = [
                'errorCode' => $e->getCode(),
                'errorMsg'  => $e->getMessage(),
                'errorFile' => $e->getFile() . ' [ ' . $e->getLine() . ' ]',
                'errorData' => $e->getTraceAsString(),
                'params'    => $params,
            ];
            // error_log('aliPayNotify Error' . PHP_EOL . \json_encode($err),3,LOG_PATH.'11alipay.log');
            Log::record('aliPayNotify Error' . PHP_EOL . \json_encode($err), 'pay');
        }
        // 已失败
        return 'Failed';
    }
// 业务逻辑处理方法
private function updateWechatPayNotify(array $verifyData): bool
    {
        $outTradeNo = $verifyData['out_trade_no'];
        // 查找订单
        $orderInfo = Db::name('order')->where(['order_code'=>$outTradeNo])->find();
        if (empty($orderInfo)) {
            return true;
        }

        // 金额对比 以分为单位
        $orgAmount = $verifyData['cash_fee']/100; // 回调金额
        // 修改订单状态
        $orderData = [
            'trade_no'   => $verifyData['transaction_id'],
            'pay_state'  => 1,
            'pay_time'   => time(),
            'pay_money'  => $orgAmount,
        ];
        $res = Db::name('order')->where(['order_id'=>$orderInfo['order_id']])->update($orderData);
        if($res){
            return true;
        }else{
            return false;
        }
    }

支付宝

public function done(){
    $config = [
         'app_id' => 'appid',
         'notify_url' => url('user/public/aliPayNotify',[],true,true),
         'return_url' => url('user/company/finance',[],true,true),
         'ali_public_key' => 'ali_public_key',
         'private_key' => 'rsa_private_key',
         'log' => [
               'file' => LOG_PATH.'logs/alipay.log',
               'level' => 'info', // 建议生产环境等级调整为 info,开发环境为 debug
               'type' => 'single', // optional, 可选 daily.
               'max_file' => 30, // optional, 当 type 为 daily 时有效,默认 30 天
          ],
          'http' => [ // optional
                 'timeout' => 5.0,
                 'connect_timeout' => 5.0,
                ],
      ];

      // 支付
      $order = [
            'out_trade_no' => $number,
            'total_amount' => $order['money'],
            'subject' => '支付',
      ];
      $alipay = Pay::alipay($config);
      return $alipay->web($order)->send();
}

支付宝回调

public function aliPayNotify()
    {
        $params = $this->request->param();
        try {
            $config = [
                'app_id' => 'appid',
                'notify_url' => url('user/public/aliPayNotify',[],true,true),
                'return_url' => url('user/company/finance',[],true,true),
                'ali_public_key' => 'ali_public_key',
                'private_key' => 'rsa_private_key',
                'log' => [
                    'file' => LOG_PATH.'logs/alipay.log',
                    'level' => 'info', // 建议生产环境等级调整为 info,开发环境为 debug
                    'type' => 'single', // optional, 可选 daily.
                    'max_file' => 30, // optional, 当 type 为 daily 时有效,默认 30 天
                ],
                'http' => [ // optional
                    'timeout' => 5.0,
                    'connect_timeout' => 5.0,
                    // 更多配置项请参考 [Guzzle](https://guzzle-cn.readthedocs.io/zh_CN/latest/request-options.html)
                ],
                // 'mode' => 'dev', // optional,设置此参数,将进入沙箱模式
            ];
            $this->payService = Pay::alipay($config);
            $verifyData       = $this->payService->verify()->toArray();
            // 业务逻辑方法
            if (true === $this->updatePayNotify($verifyData)) {
                return $this->payService->success();
            }
        } catch (\Exception $e) {
            $err = [
                'errorCode' => $e->getCode(),
                'errorMsg'  => $e->getMessage(),
                'errorFile' => $e->getFile() . ' [ ' . $e->getLine() . ' ]',
                'errorData' => $e->getTraceAsString(),
                'params'    => $params,
            ];
            Log::record('aliPayNotify Error' . PHP_EOL . \json_encode($err), 'pay');
        }
        // 已失败
        return 'Failed';
}
// 业务逻辑方法
private function updatePayNotify(array $verifyData): bool
    {
        $outTradeNo = $verifyData['out_trade_no'];
        // 查找订单
        $orderInfo = Db::name('order')->where(['order_code'=>$outTradeNo])->find();
        if (empty($orderInfo)) {
            return true;
        }

        // 金额对比 以分为单位
        $orgAmount = intval($verifyData['total_amount'] * 100); // 回调金额
        $dbAmount  = intval($orderInfo['money'] * 100); // 数据库金额
        if (empty($orgAmount) || $orgAmount !== $dbAmount) {
            $err = [
                'msg'         => '金额不一致',
                '$orderInfo'  => $orderInfo,
                '$verifyData' => $verifyData,
                '$orgAmount'  => $orgAmount,
                '$dbAmount'   => $dbAmount,
            ];
            Log::record($this->payService->getProviderDriverStr() . ' Notify Error' . PHP_EOL . \json_encode($err), 'payment');
            return false;
        }
        // 修改订单状态
        $orderData = [
            'trade_no'   => $verifyData['trade_no'],
            'pay_state'  => 1,
            'pay_time'   => time(),
            'pay_money'  => $verifyData['receipt_amount'],
        ];
        $res = Db::name('order')->where(['order_id'=>$orderInfo['order_id']])->update($orderData);
        if($res){
            return true;
        }else{
            return false;
        }
}
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!