PHP对接富友支付

<?php
namespace common;

class fuiouPay
{   
    //对方公钥,加密用
	public $rsaPublicKey;
    
    //己方私钥,解密用
	public $rsaPrivateKey;

    //设置商品名称
    public $goodsName;
    
    //设置商品详情
    public $goodsDetail;

    //设置异步通知地址
    public $notifyUrl;

    //设置appid
    public $appId;
    
    //设置openid
    public $openId;

    //设置订单号
    public $orderNo;
    
    //设置订单金额
    public $orderAmt;

    //重定向地址用于获取openID跳转
    public $redirectUri;

    //支付接口
    public $unifiedOrderUrl = "https://aggpcpay.fuioupay.com/aggpos/order.fuiou";

    //获取OpenID接口
    public $openIdUrl = "https://aggpcpay.fuioupay.com/aggpos/getOpenid.fuiou";

    //入网时生成的商户号	
    public $mchntCd;

    //版本号
    public $version = "1.0.0";

    /**
     * 设置对方公钥
     */
    public function setRsaPublicKey($rsaPublicKey)
    {
        $this->rsaPublicKey = $rsaPublicKey;
    }

    /**
     * 设置己方私钥
     */
    public function setRsaPrivateKey($rsaPrivateKey)
    {
        $this->rsaPrivateKey = $rsaPrivateKey;
    }

    /**
     * 设置入网时生成的商户号
     */
    public function setMchntCd($mchntCd)
    {
        $this->mchntCd = $mchntCd;
    }

    /**
     * 设置goodsName
     */
    public function setGoodsName($name)
    {
        $this->goodsName = $name;
    }

    /**
     * 设置goodsDetail
     */
    public function setGoodsDetail($detail)
    {
        $this->goodsDetail = $detail;
    }

    /**
     * 设置通知地址
     */
    public function setNotifyUrl($url)
    {
        $this->notifyUrl = $url;
    }

    /**
     * 设置AppId
     */
    public function setAppId($appId)
    {
        $this->appId = $appId;
    }

    /**
     * 设置openId
     */
    public function setOpenId($openId)
    {
        $this->openId = $openId;
    }

    /**
     * 设置orderNo
     */
    public function setOrderNo($orderNo)
    {
        $this->orderNo = $orderNo;
    }

    /**
     * 设置orderAmt
     */
    public function setOrderAmt($orderAmt)
    {
        $this->orderAmt = $orderAmt;
    }
	
	/**
     * 支付
     */
	public function pay(){
		//报文体,array
		$message_body = [
			'mchnt_cd' 			=> $this->mchntCd,
			'order_date' 		=> date('Ymd'),
			'order_id' 			=> $this->orderNo,
			'order_amt' 		=> $this->orderAmt,
			'order_pay_type' 	=> 'LETPAY',
			'back_notify_url' 	=> $this->notifyUrl,
			'goods_name' 		=> $this->goodsName,
			'goods_detail' 		=> $this->goodsDetail,
			'appid' 			=> $this->appId,
			'openid' 			=> $this->openId,
			'ver' 				=> $this->version
 		];
		//生成message
		$message = $this->publicEncryptRsa(json_encode($message_body));
		
		$data = [
            'mchnt_cd' => $this->mchntCd,
            'message'   => $message
        ];
        return $this->jsonPost($this->unifiedOrderUrl,$data);
	}
	
	//加密
	public function publicEncryptRsa($plainData = ''){
		if (!is_string($plainData)) {
            return null;
        }
        $encrypted = '';
        $partLen = $this->getPublicKenLen()/8 - 11;
        $plainData = str_split($plainData, $partLen);
        $publicPEMKey = $this->getPublicKey();
        foreach ($plainData as $chunk) {
            $partialEncrypted = '';
            $encryptionOk = openssl_public_encrypt($chunk, $partialEncrypted, $publicPEMKey, OPENSSL_PKCS1_PADDING);
            if ($encryptionOk === false) {
                return false;
            }
            $encrypted .= $partialEncrypted;
        }
        return base64_encode($encrypted);
	}
	
	public function getPublicKenLen()
    {
        $pub_id = openssl_get_publickey($this->getPublicKey());
        return openssl_pkey_get_details($pub_id)['bits'];
    }
	
    //获取公钥
	public function getPublicKey()
    {
		$public_key = $this->rsaPublicKey;
		//这里的public_key,是一行的情况,才是这样处理;
        $pubic_pem = chunk_split($public_key, 64, "\n");
        $pubic_pem = "-----BEGIN PUBLIC KEY-----\n" . $pubic_pem . "-----END PUBLIC KEY-----\n";
        return $pubic_pem;
    }
	
	private function privateDecryptRsa($data = '')
    {
        if (!is_string($data)) {
            return null;
        }
        $decrypted = '';

        $partLen = $this->getPrivateKenLen() / 8;
        $data = str_split(base64_decode($data), $partLen);

        $privatePEMKey = $this->getPrivateKey();

        foreach ($data as $chunk) {
            $partial = '';
            $decryptionOK = openssl_private_decrypt($chunk, $partial, $privatePEMKey, OPENSSL_PKCS1_PADDING);
            if ($decryptionOK === false) {
                return false;
            }
            $decrypted .= $partial;
        }
        return $decrypted;
    }
	
	private function getPrivateKenLen()
    {
        $pub_id = openssl_get_privatekey($this->getPrivateKey());
        return openssl_pkey_get_details($pub_id)['bits'];
    }
	
	private function getPrivateKey()
    {
		//这里的private_key,是一行的情况,才是这样处理;
		$private_key = $this->rsaPrivateKey;
        $private_pem = chunk_split($private_key, 64, "\n");
        $private_pem = "-----BEGIN PRIVATE KEY-----\n" . $private_pem . "-----END PRIVATE KEY-----\n";
        return $private_pem;
    }

    /**
     * PHP发送Json对象数据
     * @param $url 请求url
     * @param $data 发送的json字符串/数组
     * @return array
     */
    public function jsonPost($url, $data = NULL)
    {
        $curl = curl_init();

        curl_setopt($curl, CURLOPT_URL, $url);
        curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
        curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, false);
        
        if(is_array($data))
        {
            $data = json_encode($data);
        }
        curl_setopt($curl, CURLOPT_POST, 1);
        curl_setopt($curl, CURLOPT_POSTFIELDS, $data);
        curl_setopt($curl, CURLOPT_HEADER, 0);
        curl_setopt($curl, CURLOPT_HTTPHEADER,array(
                'Content-Type: application/json; charset=utf-8',
                'Content-Length:' . strlen($data),
                'Cache-Control: no-cache',
                'Pragma: no-cache'
        ));
        curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
        $res = curl_exec($curl);
        curl_close($curl);
        return $this->returnData($res);

    }

    //获取post过来的数据
    public function getPost()
    {
        return file_get_contents('php://input');
    }

    /**
     * 获取OpenId
     */
    public function getOpenId(){
        $message_body = [
            'mchnt_cd' => $this->mchntCd,
            'redirect_uri' => $this->redirectUri,
            'ver' => $this->version
        ];
        $message = $this->publicEncryptRsa(json_encode($message_body));
		
		$data = [
            'mchnt_cd' => $this->mchntCd,
            'message'   => $message
        ];
        return  $this->jsonPost($this->openIdUrl,$data);
    }

    /**
     * 解密返回数据
     */
    public function returnData($result){
        $decrypted = "";
        if ($result) {
            $result = json_decode($result, true);
            if ($result['resp_code'] == '0000') {
				//只有resp_code为0000的时候,才有message。
                $decrypted = $this->privateDecryptRsa($result['message'], $this->rsaPrivateKey);
                if ($decrypted) {
                    $decrypted = json_decode($decrypted, true);
                }
            }else{
                return $result;
            }
        }
		//得到解密过后的数据
		return $decrypted;
    }
}

  • 8
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

swoole~

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值