<?php

namespace Ycpay;

require_once dirname(__FILE__) . '/../AliPaySdk/aop/AopClient.php';
require_once dirname(__FILE__) . '/../AliPaySdk/aop/AopCertClient.php';
require_once dirname(__FILE__) . '/../AliPaySdk/aop/request/AlipayFundAccountQueryRequest.php';
require_once dirname(__FILE__) . '/../AliPaySdk/aop/request/AlipayFundTransUniTransferRequest.php';
require_once dirname(__FILE__) . '/../AliPaySdk/aop/request/AlipayTradeWapPayRequest.php';
require_once dirname(__FILE__) . '/../AliPaySdk/aop/request/AlipayTradeAppPayRequest.php';
require_once dirname(__FILE__) . '/../AliPaySdk/aop/request/AlipayTradeQueryRequest.php';


class AliPay
{
    private  $aop;

    const  GATE_WAY_URL = 'https://openapi.alipay.com/gateway.do';
    
    // 火猫小说
    const  APPID = '2021003192638805';
    
    // 应用私钥
    const  RSAPRIVATEKEY = "MIIEvQIBADANBgkqhkiG9w0BAQEFAASCBKcwggSjAgEAAoIBAQCN6hnKouUoNLvbHmzt4kPlLvxINERaVQ+Fk3COh4lW5ESpIczjLT1fHGEbab60+8Io0XeFkKhOWHVYXV4nzIoBw0d/Yeu/kLI79JeatP4uOZLxdi/LfgAozHajzvzwNBXn9vBPEBiW3+Uq2VsooWdMR6y7qseZXMcXKE3L3fEwGvdmraXlfdYM4bQKjw0S1I1ADQs3ltuzXYM+vK1+n7fI6BHqb6LEN6Uojdc1Vy2Iq74frDpEjRBdhxwB7VZC6cHlIxI6Eed3Nql04/oBtQqdm0b1ORLIr3xU6XS3ZI3E2hIJseIhSOuoAbzQ7LeQcpMraBi4GrG7sr6EH5Qd9RPPAgMBAAECggEAOly0Dqm/JDHLr+lrUslisrYH+onEMoIYyA5Ck9sZmCp4MjY8VJEeVCcPqH5sd3Gx/6dlSACdttqaJ6T+O8/aUa9FkBVtJnMTWaTVVEJq5ge3bqX0MfO3jQ/JGsTdMMu61pKvcVRlSGb7kAujeaonGS/A0c0CGQgWuGdS1yyHLAjL3eT/bRbgTHMwb3X9WBAvo7vgJMUTS/Bp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ui/appCertPublicKey_2021003192638805.crt";
    
    // 支付宝公钥证书
    const  ALIPAY_PUBLIC_CERT_PATH = "/cert/huomaoxiaoshuohui/alipayCertPublicKey_RSA2.crt";
    
    // 支付宝根证书
    const  ALIPAY_ROOT_CERT_PATH = "/cert/huomaoxiaoshuohui/alipayRootCert.crt";
    
    const  SIGN_TYPE = 'RSA2';
    
    const  FORMAT = 'json';
    
    const  CHARST =  "utf-8";

    private $rsaPublicKeyFilePath;

    private $pay_merchant_id;

    public function __construct($config=null)
    {
        $this->aop = new \AopCertClient;
        $this->aop->appId = self::APPID;
        $this->aop->signType = self::SIGN_TYPE;
        $this->aop->gatewayUrl = self::GATE_WAY_URL;
        $this->aop->format = self::FORMAT;
        $this->aop->charset = self::CHARST;
        $this->aop->isCheckAlipayPublicCert = true;
        $this->aop->rsaPrivateKey = self::RSAPRIVATEKEY;

        $this->aop->alipayrsaPublicKey = $this->aop->getPublicKey(dirname(__FILE__) . "/../cert/huomaoxiaoshuohui/alipayCertPublicKey_RSA2.crt");
        //证书序列号
        $this->aop->appCertSN = $this->aop->getCertSN(dirname(__FILE__) . "/../cert/huomaoxiaoshuohui/appCertPublicKey_2021003192638805.crt");
        //支付宝根证书序列号
        $this->aop->alipayRootCertSN = $this->aop->getRootCertSN(dirname(__FILE__) . "/../cert/huomaoxiaoshuohui/alipayRootCert.crt");
         
//         \Log::info('$this->aop->alipayRootCertSN');\Log::info(dirname(__FILE__) . "/../cert/huomaoxiaoshuohui/alipayRootCert.crt");
        
    }


    public function notify(array $data)
    {
        if (isset($data['sign']) && isset($data['trade_status'])  && $data['trade_status'] == 'TRADE_SUCCESS') {
            return $this->aop->rsaCheckV1($data, $this->rsaPublicKeyFilePath, self::SIGN_TYPE);
        } else {
            return false;
        }
    }

    public function queryFundAccount()
    {
        /** 设置业务参数，具体接口参数传值以文档说明为准：https://opendocs.alipay.com/apis/api_28/alipay.fund.account.query **/
        $request = new \AlipayFundAccountQueryRequest;
        
        $request->setBizContent("{" .
            "\"alipay_user_id\":\"2088641232029876\"," .
            "\"account_type\":\"ACCTRANS_ACCOUNT\"" .
            "}");
        
        $result= $this->aop->execute($request);
        \Log::info('queryFundAccount:'.json_encode($result));
        return $result;

    }
    
    public function transFund($trans_param)
    {
        $request = new \AlipayFundTransUniTransferRequest;
        $bizContent = [
            'out_biz_no'=>$trans_param['out_biz_no'],
            'remark'=>$trans_param['remark'],
            'business_params'=>[
                'payer_show_name_use_alias'=>true,
            ],
            'biz_scene'=>'DIRECT_TRANSFER',
            'out_biz_no'=>$trans_param['out_biz_no'],
            'payee_info'=>[
                'identity'=>$trans_param['account'],
                'identity_type'=>'ALIPAY_LOGON_ID',
                'name'=>$trans_param['account_name'],
            ],
            'trans_amount'=>$trans_param['trans_amount'],
            'product_code'=>'TRANS_ACCOUNT_NO_PWD',
            'order_title'=>$trans_param['order_title'],
        ];
        $bizContent_json = json_encode($bizContent);
//         $bizContent = "{".
//             "\"out_biz_no\":\".$trans_param['out_biz_no'].\",".
//                 "\"remark\":\".$trans_param['remark'].\",".
//             "\"business_params\":\"{\\\"payer_show_name_use_alias\\\":\\\"true\\\"}\",".
//             "\"biz_scene\":\"DIRECT_TRANSFER\",".
//             "\"payee_info\":{".
//             "\"identity\":\".$trans_param['account'].\",".
//             "\"identity_type\":\"ALIPAY_LOGON_ID\",".
//             "\"name\":\".$trans_param['account_name'].\"".
//             "},".
//             "\"trans_amount\":\".$trans_param['trans_amount'].\",".
//             "\"product_code\":\"TRANS_ACCOUNT_NO_PWD\",".
//             "\"order_title\":\".$trans_param['order_title'].\"".
//                 "}";
         \Log::info('$bizContent:'.$bizContent_json);
         \Log::info($bizContent);
         $request->setBizContent($bizContent_json);
         
//          {"out_biz_no":"tf-2023052917264016853524006173129",
//          "remark":"\u6536\u76ca\u63d0\u73b0",
//          "business_params":{"payer_show_name_use_alias":true},
//          "biz_scene":"DIRECT_TRANSFER",
//          "payee_info":{"identity":"18668420256","identity_type":"ALIPAY_LOGON_ID","name":"\u5468\u7075\u6770"},
//          "trans_amount":0.01,"product_code":"TRANS_ACCOUNT_NO_PWD","order_title":"\u706b\u732b\u5c0f\u8bf4"} 
         
        
//         $request->setBizContent("{".
//             "\"out_biz_no\":\"hmxs201806300003\",".
//             "\"remark\":\"火猫小说\",".
//             "\"business_params\":\"{\\\"payer_show_name_use_alias\\\":\\\"true\\\"}\",".
//             "\"biz_scene\":\"DIRECT_TRANSFER\",".
//             "\"payee_info\":{".
//             "\"identity\":\"18668420256\",".
//             "\"identity_type\":\"ALIPAY_LOGON_ID\",".
//             "\"name\":\"周灵杰\"".
//             "},".
//             "\"trans_amount\":\"0.1\",".
//             "\"product_code\":\"TRANS_ACCOUNT_NO_PWD\",".
//             "\"order_title\":\"测试转账\"".
//             "}");
        $result = $this->aop->execute($request);
        echo json_encode($result,JSON_UNESCAPED_UNICODE);
        \Log::info('transFund:'.json_encode($result,JSON_UNESCAPED_UNICODE));
        return $result;
        
    }
    

}
