<?php
/*
 * @Author: 万永康
 * @Date: 2020-10-11 12:32:50
 * @LastEditTime: 2024-02-20 16:23:38
 * @LastEditors: “wanyongkang” “937888580@qq.com”
 * @Description: In User Settings Edit
 * @FilePath: /phptest/alipay/Alipay.php
 */

namespace extend\alipay;

require_once APP_PATH . 'extends/alipay/aop/AopClient.php';
require_once APP_PATH . 'extends/alipay/aop/AopCertClient.php';
require_once APP_PATH . 'extends/alipay/aop/AopCertification.php';
require_once APP_PATH . 'extends/alipay/aop/request/AlipayTradeQueryRequest.php';
require_once APP_PATH . 'extends/alipay/aop/request/AlipayTradePagePayRequest.php';
require_once APP_PATH . 'extends/alipay/aop/request/AlipayFundTransUniTransferRequest.php';

/**
 * 证书类型AopClient功能方法使用测试
 * 1、execute 调用示例
 * 2、sdkExecute 调用示例
 * 3、pageExecute 调用示例
 */

class Alipay
{

    /**
     *
     * web 付款
     *  $param = [
     *       'body' => '123',
     *      'subject' => 'test',
     *     'total_amount' => '0.01',
     *    'out_trade_no' => '1231313123123',
     *   'product_code' => "FAST_INSTANT_TRADE_PAY",//QUICK_WAP_PAY
     *     'timeout_express'=>"15m"
     *];
     *
     *
     */
    public static function pay($param)
    {
        global $config;
        $aop = new \AopCertClient();
        
        $appCertPath = $config['alipay']['app_crt'];
        $alipayCertPath = $config['alipay']['public_crt'];
        $rootCertPath = $config['alipay']['root_crt'];
        
        $aop->gatewayUrl = 'https://openapi.alipay.com/gateway.do';
        $aop->appId = $config['alipay']['app_id'];
        $aop->rsaPrivateKey = $config['alipay']['private_key'];
        $aop->alipayrsaPublicKey = $aop->getPublicKey($alipayCertPath);//调用getPublicKey从支付宝公钥证书中提取公钥
        $aop->apiVersion = '1.0';
        $aop->signType = 'RSA2';
        $aop->postCharset='utf-8';
        $aop->format='json';
        $aop->isCheckAlipayPublicCert = true;//是否校验自动下载的支付宝公钥证书，如果开启校验要保证支付宝根证书在有效期内
        $aop->appCertSN = $aop->getCertSN($appCertPath);//调用getCertSN获取证书序列号
        $aop->alipayRootCertSN = $aop->getRootCertSN($rootCertPath);//调用getRootCertSN获取支付宝根证书序列号

        $request = new \AlipayTradePagePayRequest();

        

        $param = json_encode($param);

        $request->setReturnUrl('https://juip.com/product/routeros');

        $notifyUrl="https://php-api.juip.com/common/alipay/alipayverify";
        $request->setNotifyUrl($notifyUrl);

        $request->setBizContent($param);
        $result = $aop->pageExecute ( $request); 
        
        return $result;

    }
     /**
     *
     * web 付款
     *  $param = [
     *       'body' => '123',
     *      'subject' => 'test',
     *     'total_amount' => '0.01',
     *    'out_trade_no' => '1231313123123',
     *   'product_code' => "FAST_INSTANT_TRADE_PAY",//QUICK_WAP_PAY
     *     'timeout_express'=>"15m"
     *];
     *
     *
     */
    public static function pay2($param)
    {
        global $config;
        $aop = new \AopClient ();
        $aop->gatewayUrl = 'https://openapi.alipay.com/gateway.do';
        $aop->appId = $config['alipay2']['app_id'];;
        $aop->rsaPrivateKey = $config['alipay2']['private_key'] ;
        $aop->apiVersion = '1.0';
        $aop->signType = 'RSA2';
        $aop->postCharset='utf-8';
        $aop->format='json';
        $aop->alipayrsaPublicKey = $config['alipay2']['public_key'];

        $request = new \AlipayTradePagePayRequest();

        

        $param = json_encode($param);

        $request->setReturnUrl('https://juip.com/product/routeros');

        $notifyUrl="https://php-api.juip.com/common/alipay/alipayverify";
        $request->setNotifyUrl($notifyUrl);

        $request->setBizContent($param);
        $result = $aop->pageExecute ( $request); 
        
        return $result;

    }
    
    public static function pay_http($param)
    {
        global $config;
        $aop = new \AopClient ();
        $aop->gatewayUrl = 'https://openapi.alipay.com/gateway.do';
        $aop->appId = $config['alipay2']['app_id'];;
        $aop->rsaPrivateKey = $config['alipay2']['private_key'] ;
        $aop->apiVersion = '1.0';
        $aop->signType = 'RSA2';
        $aop->postCharset='utf-8';
        $aop->format='json';
        $aop->alipayrsaPublicKey = $config['alipay2']['public_key'];

        $request = new \AlipayTradePagePayRequest();

        

        $param = json_encode($param);

        $request->setReturnUrl('https://juip.com/product/http');

        $notifyUrl="https://php-api.juip.com/common/alipay/httpalipayverify";
        $request->setNotifyUrl($notifyUrl);

        $request->setBizContent($param);
        $result = $aop->pageExecute ( $request); 
        
        return $result;

    }
    public static function pay_jinyou($param)
    {
        global $config;
        $aop = new \AopClient ();
        $aop->gatewayUrl = 'https://openapi.alipay.com/gateway.do';
        $aop->appId = $config['alipay2']['app_id'];;
        $aop->rsaPrivateKey = $config['alipay2']['private_key'] ;
        $aop->apiVersion = '1.0';
        $aop->signType = 'RSA2';
        $aop->postCharset='utf-8';
        $aop->format='json';
        $aop->alipayrsaPublicKey = $config['alipay2']['public_key'];

        $request = new \AlipayTradePagePayRequest();

        

        $param = json_encode($param);

        $request->setReturnUrl('https://juip.com/user/myaccounts');

        $notifyUrl="https://php-api.juip.com/common/alipay/jinyoualipayverify";
        $request->setNotifyUrl($notifyUrl);

        $request->setBizContent($param);
        $result = $aop->pageExecute ( $request); 
        
        return $result;

    }
    public static function pay_jinqiao($param)
    {
        global $config;
        $aop = new \AopClient ();
        $aop->gatewayUrl = 'https://openapi.alipay.com/gateway.do';
        $aop->appId = $config['alipay2']['app_id'];;
        $aop->rsaPrivateKey = $config['alipay2']['private_key'] ;
        $aop->apiVersion = '1.0';
        $aop->signType = 'RSA2';
        $aop->postCharset='utf-8';
        $aop->format='json';
        $aop->alipayrsaPublicKey = $config['alipay2']['public_key'];

        $request = new \AlipayTradePagePayRequest();

        

        $param = json_encode($param);

        $request->setReturnUrl('https://juip.com/user/Index');

        $notifyUrl="https://php-api.juip.com/common/alipay/jinqiaoalipayverify";
        $request->setNotifyUrl($notifyUrl);

        $request->setBizContent($param);
        $result = $aop->pageExecute ( $request); 
        
        return $result;

    }

    /**
     * @description: 获取支付返回参数
     * @param {*} $post
     * @return {*}
     */    
    public static function payVerify($post) {
        global $config;
        //获取支付宝POST过来反馈信息
        $aop = new \AopCertClient();
        $alipayCertPath = $config['alipay']['public_crt'];
        $aop->alipayrsaPublicKey = $aop->getPublicKey($alipayCertPath);//调用getPublicKey从支付宝公钥证书中提取公钥

        //编码格式
        $aop->postCharset="UTF-8";
        //签名方式
        $sign_type="RSA2";
        //输出拆分后的数据数据
        //验签代码
        $flag = $aop->rsaCheckV1($post, null, $sign_type);
        
        if ($flag)
        {
            return true;
        }
        else 
        {
            return false;
        }
    }
    /**
     * @description: 获取支付返回参数
     * @param {*} $post
     * @return {*}
     */    
    public static function payVerify2($post) {
        global $config;
        $aop = new \AopClient();
        $aop->alipayrsaPublicKey = $config['alipay2']['public_key'];

        // //签名方式
        // $sign_type="RSA2";
        // $data = explode('&', $post);
        // $params = array();
        // foreach ($data as $param) {
        //     $item = explode('=', $param,"2");
        //     $params[$item[0]] = $item[1];
        // }
        //输出拆分后的数据数据
        //验签代码
        $flag = $aop->rsaCheckV1($_POST, NULL, "RSA2");
        
        if ($flag)
        {
            return true;
        }
        else 
        {
            return false;
        }
    }
    /**
     * @description: 转帐到支付宝账户
     * @param {type}$param
     * [
     *      'out_biz_no'=>'201806300001',
     *      'trans_amount' => '0.01',
     *      'product_code' => 'TRANS_ACCOUNT_NO_PWD',
     *      'payee_info' => [
     *          'identity' => '208812*****41234',
     *          'identity_type' => 'ALIPAY_LOGON_ID',
     *          'name' => 'peter'
     *      ],
     *      'remark' => '提现-单笔转帐'
     * ]
     * @return {type}
     */
    public static function transfer($param)
    {
        global $config;
        $aop = new \AopCertClient();
        
        $appCertPath = $config['alipay']['app_crt'];
        $alipayCertPath = $config['alipay']['public_crt'];
        $rootCertPath = $config['alipay']['root_crt'];
        
        $aop->gatewayUrl = 'https://openapi.alipay.com/gateway.do';
        $aop->appId = $config['alipay']['app_id'];
        $aop->rsaPrivateKey = $config['alipay']['private_key'];
        $aop->alipayrsaPublicKey = $aop->getPublicKey($alipayCertPath);//调用getPublicKey从支付宝公钥证书中提取公钥
        $aop->apiVersion = '1.0';
        $aop->signType = 'RSA2';
        $aop->postCharset='utf-8';
        $aop->format='json';
        $aop->isCheckAlipayPublicCert = true;//是否校验自动下载的支付宝公钥证书，如果开启校验要保证支付宝根证书在有效期内
        $aop->appCertSN = $aop->getCertSN($appCertPath);//调用getCertSN获取证书序列号
        $aop->alipayRootCertSN = $aop->getRootCertSN($rootCertPath);//调用getRootCertSN获取支付宝根证书序列号

        $request = new \AlipayFundTransUniTransferRequest();

        $param = json_encode($param);

        $request->setBizContent($param);
        $result = $aop->execute($request);
        
        $responseNode = str_replace(".", "_", $request->getApiMethodName()) . "_response";
        $resultCode = $result->$responseNode->code;
        if (!empty($resultCode) && $resultCode == 10000) {
            return $result;
        } else {
            return false;
        }
    }
}
