<?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;
}
}
PHP对接富友支付
于 2023-12-21 18:23:19 首次发布