PHP 单笔转账到支付宝账户,支付宝公钥证书实现版本

支付宝某些业务只能使用公钥证书方式来验签

如:即使转账

     红包等

笔者就要实现这样一个功能,【单笔转账到支付宝账户】,采用支付宝公钥证书签名来实现。

话不多说,流程先走起

第一步:下载支付宝秘钥生成器

由于我们使用的是php鱼油,点击pkcs1(非java适用)

按照上面图中的 三个步骤操作,操作完成后,点击【打开文件位置】,可以看到下面这些文件

会有

  1. 一个csr文件
  2. 一个公钥
  3. 一个私钥

我们将csr文件上传到支付宝 接口加签方式 里面,然后讲下图里面的三个整数下载下来

 

分别是:

  • alipayCertPublicKey_RSA2.crt
  • alipayRootCert.crt
  • appCertPublicKey_202100000023232.crt

第二步,配置PHP配置

我们将 上面生成的三个文件复制到证书目录,如图:

配置代码如下:

<?php
return [
    'app_id' => '', // 支付宝应用的appid
    'alipay_root_cert_sn' => BASE_PATH . '/cert/alipayRootCert.crt', // 支付宝根证书在自己服务器的绝对路径
    'app_cert_sn' => BASE_PATH . '/cert/appCertPublicKey_2021000199651454.crt', // 应用公钥证书在自己服务器绝对路径
    'rsa_private_key' => '' // 这个是RSA签名,特别要注意的是,是我们第一步中的对应自己域名的那个私钥哦
];

 

编写AlipayTransServer

class AlipayTransfers
{
    protected $appId;
    //私钥值
    protected $rsaPrivateKey;
    /**
     * @var string
     */
    private $charset;
 
    public function __construct($appid, $saPrivateKey)
    {
        $this->appId = $appid;
        $this->charset = 'utf8';
        $this->rsaPrivateKey = $saPrivateKey;
    }
 
    /**
     * 转帐
     * @param float $totalFee 转账金额,单位:元。
     * @param string $outTradeNo 商户转账唯一订单号
     * @param string $remark 转帐备注
     * @return array
     */
    public function doPay($totalFee, $outTradeNo, $account, $realName, $remark = '')
    {
        //请求参数
        $requestConfigs = array(
            'out_biz_no' => $outTradeNo,
            'payee_type' => 'ALIPAY_LOGONID',
            'payee_account' => $account,
            'payee_real_name' => $realName,  //收款方真实姓名
            'amount' => $totalFee, //转账金额,单位:元。
            'remark' => $remark,  //转账备注(选填)
        );
        $commonConfigs = array(
            //公共参数
            'app_id' => $this->appId,
            'method' => 'alipay.fund.trans.toaccount.transfer',             //接口名称
            'format' => 'JSON',
            'charset' => $this->charset,
            'sign_type' => 'RSA2',
            'timestamp' => date('Y-m-d H:i:s'),
            'alipay_root_cert_sn' => $this->getRootCertSN(BASE_PATH . '/cert/alipayRootCert.crt'),//支付宝根证书SN(alipay_root_cert_sn)
            'app_cert_sn' => $this->getCertSN(BASE_PATH . '/cert/appCertPublicKey_2021000199651454.crt'), //应用公钥证书SN(app_cert_sn)
            'version' => '1.0',
            'biz_content' => json_encode($requestConfigs),
        );
        $commonConfigs["sign"] = $this->generateSign($commonConfigs, $commonConfigs['sign_type']);
        $result = $this->curlPost('https://openapi.alipay.com/gateway.do', $commonConfigs);
        $resultArr = json_decode($result, true);
        if (empty($resultArr)) {
            $result = iconv('GBK', 'UTF-8//IGNORE', $result);
            return json_decode($result, true);
        }
        return $resultArr;
    }
 
    public function generateSign($params, $signType = "RSA")
    {
        return $this->sign($this->getSignContent($params), $signType);
    }
 
    protected function sign($data, $signType = "RSA")
    {
        $priKey = $this->rsaPrivateKey;
        $res = "-----BEGIN RSA PRIVATE KEY-----\n" .
            wordwrap($priKey, 64, "\n", true) .
            "\n-----END RSA PRIVATE KEY-----";
        ($res) or die('您使用的私钥格式错误,请检查RSA私钥配置');
        if ("RSA2" == $signType) {
            openssl_sign($data, $sign, $res, version_compare(PHP_VERSION, '5.4.0', '<') ? SHA256 : OPENSSL_ALGO_SHA256); //OPENSSL_ALGO_SHA256是php5.4.8以上版本才支持
        } else {
            openssl_sign($data, $sign, $res);
        }
        $sign = base64_encode($sign);
        return $sign;
    }
 
    /**
     * 校验$value是否非空
     *  if not set ,return true;
     *    if is null , return true;
     **/
    protected function checkEmpty($value)
    {
        if (!isset($value))
            return true;
        if ($value === null)
            return true;
        if (trim($value) === "")
            return true;
        return false;
    }
 
    public function getSignContent($params)
    {
        ksort($params);
        $stringToBeSigned = "";
        $i = 0;
        foreach ($params as $k => $v) {
            if (false === $this->checkEmpty($v) && "@" != substr($v, 0, 1)) {
                // 转换成目标字符集
                $v = $this->characet($v, $this->charset);
                if ($i == 0) {
                    $stringToBeSigned .= "$k" . "=" . "$v";
                } else {
                    $stringToBeSigned .= "&" . "$k" . "=" . "$v";
                }
                $i++;
            }
        }
        unset ($k, $v);
        return $stringToBeSigned;
    }
 
    /**
     * 转换字符集编码
     * @param $data
     * @param $targetCharset
     * @return string
     */
    function characet($data, $targetCharset)
    {
        if (!empty($data)) {
            $fileType = $this->charset;
            if (strcasecmp($fileType, $targetCharset) != 0) {
                $data = mb_convert_encoding($data, $targetCharset, $fileType);
            }
        }
        return $data;
    }
 
    public function curlPost($url = '', $postData = '', $options = array())
    {
        if (is_array($postData)) {
            $url = $url . '?' . http_build_query($postData);
            cli_log($url);
        }
        $ch = curl_init();
        curl_setopt($ch, CURLOPT_URL, $url);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
        curl_setopt($ch, CURLOPT_TIMEOUT, 30); //设置cURL允许执行的最长秒数
        if (!empty($options)) {
            curl_setopt_array($ch, $options);
        }
        //https请求 不验证证书和host
        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
        curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
        $data = curl_exec($ch);
        curl_close($ch);
        return $data;
    }
 
    /**
     * 从证书中提取***
     * @param $cert
     * @return string
     */
    public function getCertSN($certPath)
    {
        $cert = file_get_contents($certPath);
        $ssl = openssl_x509_parse($cert);
        $SN = md5($this->array2string(array_reverse($ssl['issuer'])) . $ssl['serialNumber']);
        return $SN;
    }
 
    /**
     * 提取根证书***
     * @param $cert  根证书
     * @return string|null
     */
    public function getRootCertSN($certPath)
    {
        $cert = file_get_contents($certPath);
        $array = explode("-----END CERTIFICATE-----", $cert);
        $SN = null;
        for ($i = 0; $i < count($array) - 1; $i++) {
            $ssl[$i] = openssl_x509_parse($array[$i] . "-----END CERTIFICATE-----");
            if (strpos($ssl[$i]['serialNumber'], '0x') === 0) {
                $ssl[$i]['serialNumber'] = $this->hex2dec($ssl[$i]['serialNumber']);
            }
            if ($ssl[$i]['signatureTypeLN'] == "sha1WithRSAEncryption" || $ssl[$i]['signatureTypeLN'] == "sha256WithRSAEncryption") {
                if ($SN == null) {
                    $SN = md5($this->array2string(array_reverse($ssl[$i]['issuer'])) . $ssl[$i]['serialNumber']);
                } else {
 
                    $SN = $SN . "_" . md5($this->array2string(array_reverse($ssl[$i]['issuer'])) . $ssl[$i]['serialNumber']);
                }
            }
        }
        return $SN;
    }
 
    /**
     * 0x转高精度数字
     * @param $hex
     * @return int|string
     */
    protected function hex2dec($hex)
    {
        $dec = 0;
        $len = strlen($hex);
        for ($i = 1; $i <= $len; $i++) {
            $dec = bcadd($dec, bcmul(strval(hexdec($hex[$i - 1])), bcpow('16', strval($len - $i))));
        }
        return $dec;
    }
 
    protected function array2string($array)
    {
        $string = [];
        if ($array && is_array($array)) {
            foreach ($array as $key => $value) {
                $string[] = $key . '=' . $value;
            }
        }
        return implode(',', $string);
    }
}

实现业务部分:

      

      /**
     * 转账提现
     * @param $realName 收款人真实姓名
     * @param $account  收款人账号
     * @param $amount  付款金额
     * @param string $remark 付款备注
     * @return array|void
     */
    public function transfer($realName, $account, $amount, $remark = '提现')
    {
        $aliConfig = config('alipay');
        $aliTransfers = new Transfers($aliConfig['app_id'], $aliConfig['rsa_private_key']);
        $outTradeNo = date('Ymd') . time() . rand_string(6, true);
        return $aliTransfers->doPay($amount, $outTradeNo, $account, $realName, $remark);
    }

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
实现Java支付宝转账到银行卡,需要使用支付宝提供的转账接口,具体操作步骤如下: 1. 注册支付宝开发者账号,并创建应用获取应用的 appId、私钥和公钥; 2. 引入支付宝Java SDK; 3. 初始化 AlipayClient 对象,设置请求参数; 4. 调用 AlipayFundTransToaccountTransferRequest 请求对象的 setBizContent() 方法设置转账的参数,包括转账的金额、收款人账号和姓名等信息; 5. 调用 AlipayClient 的 execute() 方法发起请求,获取响应结果。 以下是一个示例代码: ``` import com.alipay.api.AlipayApiException; import com.alipay.api.AlipayClient; import com.alipay.api.DefaultAlipayClient; import com.alipay.api.request.AlipayFundTransToaccountTransferRequest; import com.alipay.api.response.AlipayFundTransToaccountTransferResponse; public class AlipayTransferDemo { // 应用的 appId private static final String APP_ID = "your_app_id"; // 商户私钥 private static final String PRIVATE_KEY = "your_private_key"; // 支付宝公钥 private static final String ALIPAY_PUBLIC_KEY = "alipay_public_key"; // 支付宝网关 private static final String GATEWAY_URL = "https://openapi.alipay.com/gateway.do"; public static void main(String[] args) { // 初始化 AlipayClient AlipayClient client = new DefaultAlipayClient(GATEWAY_URL, APP_ID, PRIVATE_KEY, "json", "utf-8", ALIPAY_PUBLIC_KEY, "RSA2"); // 创建转账请求对象 AlipayFundTransToaccountTransferRequest request = new AlipayFundTransToaccountTransferRequest(); // 设置转账参数 request.setBizContent("{\"out_biz_no\":\"201806300001\",\"payee_type\":\"ALIPAY_LOGONID\",\"payee_account\":\"alipay_user_id\",\"amount\":\"1.00\",\"payer_show_name\":\"示例名称\",\"payee_real_name\":\"示例姓名\",\"remark\":\"转账备注\"}"); try { // 发起转账请求 AlipayFundTransToaccountTransferResponse response = client.execute(request); if (response.isSuccess()) { System.out.println("转账成功!"); } else { System.out.println("转账失败,错误码:" + response.getCode() + ",错误信息:" + response.getMsg()); } } catch (AlipayApiException e) { e.printStackTrace(); } } } ``` 需要注意的是,转账到银行卡需要在支付宝后台进行配置,开通转账到银行卡的功能。开通后,需要使用银行卡转账的接口进行转账操作。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值