THINKPHP 安裝Yansongda 微信支付寶支付

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;
        }
}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章