PHP 支付宝(单笔转账到银行账户接口)

alipay.fund.trans.tobank.transfer(单笔转账到银行账户接口)

小程序文档 - 支付宝文档中心

一、下载支付宝SDK,现有版本v1、v2、v3

https://github.com/alipay/alipay-sdk-php-all

github 慢的话,DNS   直达即可 140.82.112.3 github.com

【host文件路径c:\windows\system32\drivers\etc】

二、引用代码编写

alipay_bank_config;
        $appCertPath = WY_ROOT . '/includes/aop/cert3/appCertPublicKey_2021002131640271.crt'; //"例如:/home/admin/cert/appCertPublicKey.crt";
        $alipayCertPath = WY_ROOT . '/includes/aop/cert3/alipayCertPublicKey_RSA2.crt'; //"例如:/home/admin/cert/alipayCertPublicKey_RSA2.crt";
        $rootCertPath = WY_ROOT . '/includes/aop/cert3/alipayRootCert.crt'; //"例如:/home/admin/cert/alipayRootCert.crt";
        $aop->gatewayUrl = 'https://openapi.alipay.com/gateway.do';
//        $aop->method='alipay.fund.trans.tobank.transfer';
        $aop->appId = $alipay_config['appId'];
        $aop->rsaPrivateKey = $alipay_config['rsaPrivateKey'];
        $aop->alipayrsaPublicKey = $aop->getPublicKey($alipayCertPath);
        $aop->apiVersion = $alipay_config['apiVersion'];
        $aop->signType = $alipay_config['signType'];
        $aop->postCharset = $alipay_config['postCharset'];
        $aop->format = $alipay_config['format'];
        $aop->isCheckAlipayPublicCert = true; //是否校验自动下载的支付宝公钥证书,如果开启校验要保证支付宝根证书在有效期内
        $aop->appCertSN = $aop->getCertSN($appCertPath); //调用getCertSN获取证书序列号
        $aop->alipayRootCertSN = $aop->getRootCertSN($rootCertPath); //调用getRootCertSN获取支付宝根证书序列号
        return $aop;
    }

 /**
     * 支付宝转账到银行卡
     * @param type $data    用户信息
     * @param type $orderId 订单号
     * @param type $money   订单金额
     * @param type $fee     手续费
     * @return type     $ret = array(
      'error' => false, //是否出错
      'error_msg' => '' //错误信息
      );
     */
    public function alipay_bank($data, $orderId, $money, $fee) {
        require_once dirname(WY_ROOT) . '/ApiV1' . '/function_apiv1.php'; //引用 加载
        $ret = array(
            'error' => false, //是否出错
            'error_msg' => '' //错误信息
        );
        $pub = new ALLClass();
        $realmoney = $money - $fee; //实际支付 
        $realmoney = sprintf("%.2f", $realmoney); //精确到小数点后两位
        require_once WY_ROOT . '/includes/aop/request/AlipayFundTransTobankTransferRequest.php';
        $request = new AlipayFundTransTobankTransferRequest ();
        $this->aop = $this->alipay_init_bank();
        $payee_card_no = $data['bank_account_id']; //银行卡号
        $payer_real_name = ''; //付款方名称
//        $payer_real_name='';//付款方名称
        $payee_account_name = $data['bank_account_name']; //收款名称,
        $payee_inst_name = $data['position']; //收款行

        $bizContentObj['out_biz_no'] = $orderId; //商户端的唯一订单号,对于同一笔转账请求,商户需保证该订单号唯一。201806300001
        $bizContentObj['amount'] = $realmoney; // $realmoney; //订单总金额,单位为元,精确到小数点后两位,STD_RED_PACKET产品取值范围[0.01,100000000];
        $bizContentObj['payer_real_name'] = $payer_real_name;
        $bizContentObj['payee_card_no'] = $payee_card_no;
        $bizContentObj['payee_account_name'] = $payee_account_name;
        $bizContentObj['payee_account_type'] = '2';
        $bizContentObj['payee_inst_name'] = $payee_inst_name;
        $bizContentObj['payee_inst_province'] = '';
        $bizContentObj['payee_inst_city'] = '';
        $bizContentObj['payee_inst_branch_name'] = '';
        $bizContentObj['payee_bank_code'] = '';
        $bizContentObj['time_liness'] = 'T0';
        $bizContentObj['memo'] = '';
        $bizContentObj['remark'] = '结算款'; //业务备注
        $pub->data_log($bizContentObj, __FUNCTION__ . '.log');
        $request->setNotifyUrl('https://alipay_notify_url.php');//回调地址
        $request->setBizContent(json_encode($bizContentObj, JSON_UNESCAPED_UNICODE));
        $result = $this->aop->execute($request);
        $pub->data_log('alipay_fund_transfer_new==' . json_encode($result, JSON_UNESCAPED_UNICODE), __FUNCTION__ . '.log');
        $responseNode = str_replace(".", "_", $request->getApiMethodName()) . "_response";
        $resultCode = $result->$responseNode->code;
        if (!empty($resultCode) && $resultCode == 10000) {
            $pub->data_log('Success', __FUNCTION__ . '.log');
        } else {
            $ret['error'] = true;
            $ret['error_msg'] = $result->$responseNode->sub_msg;
            $pub->data_log('error_msg==' . $ret['error_msg'], __FUNCTION__ . '.log');
        }
        return $ret;
    }

/**
回调验签
*/
function check_sign(){
$postData = parameters_certification();
require_once WY_ROOT . '/includes/aop/AopCertClient.php'; //支付宝加载
$aop = new AopCertClient ();
$alipayCertPath = ''; //支付宝公钥证书路径(要确保证书文件可读),
$alipayCertPath = WY_ROOT . '/includes/aop/cert3/alipayCertPublicKey_RSA2.crt'; //"例如:/home/admin/cert/alipayCertPublicKey_RSA2.crt";
$aop->alipayrsaPublicKey = $aop->getPublicKey($alipayCertPath);
$bool=$aop->rsaCheckV1($postData, $alipayCertPath,$postData['sign_type']);
if(!$bool){
    die('sign error.');
}

}

/**
 * 参数验证
 * @param array $field      参数索引数组【'a','b'】
 * @param array $data       需要验证数组,不传拿$_REQUEST
 * @return boolean | array  不存在参数,空参数返回false,否则返回验证数组
 */
function parameters_certification($field = [], $data = []) {
    if (empty($data)) {
        $data = $_REQUEST;
    }
    if (empty($field)) {
        return $data;
    }
    foreach ($field as $val) {
        if (isset($data[$val])) {
            if (is_array($data[$val])) {
                if (empty($data[$val])) {
                    return false;
                } else {
                    continue;
                }
            }
            if (strlen($data[$val]) > 0) {
                continue;
            } else {
                return false;
            }
        } else {
            return false;
        }
    }
    return $data;
}

?>

回调的时候,默认签名使用的是 rsaCheckV1

暂时还没有碰到 rsaCheckV2 的,本来我以为我发送的是V2版本的签名,回调也是V2的

后面试了又试,rsaCheckV1 无疑了

支付宝的SDK无疑是很完善的,接入也方便很多

这里使用的是V2版本

你可能感兴趣的:(PHP,第三方,php,开发语言)