近日,随着我们拼客顺风车平台用户量的逐步增大,提现的数量越来越大,人工提现到银行卡的工作量越来越大,所以准备接入微众银行的代付功能,其中涉及到php的RSA2加密加密,这里记录一下下.话不多说直接上代码:
Traits
/**
* 公加密
* @param $originalData
* @param $key
* @return string
*/
public static function encrypt($originalData){
$crypto = '';
$rsaPublicKey = env('NOWPAY_PUBLIC_KEY');
$res = "-----BEGIN PUBLIC KEY-----
-----END PUBLIC KEY-----";
foreach (str_split($originalData, 117) as $chunk) {
openssl_public_encrypt($chunk, $encryptData, $res);
$crypto .= $encryptData;
}
return base64_encode($crypto);
}
public static function getSignTrueRSA2($Arr)
{
if (!empty($Arr))
{
ksort($Arr);
$str = '';
foreach ($Arr as $k => $v)
{
if ($v == '' || $k == 'signature' || $k == 'mhtSignature')
{
continue;
}
$str .= $k . '=' . $v . '&';
}
$str = substr($str, 0, -1);
return self::createSign($str);
}
return false;
}
/**
* 私签名
* @param string $data 数据
* @return null|string
*/
public static function createSign($data = '')
{
$priKey = env('NOWPAY_PRI_KEY');
$res = "-----BEGIN PRIVATE KEY-----
-----END PRIVATE KEY-----";
return openssl_sign($data, $sign, $res, OPENSSL_ALGO_SHA256) ? base64_encode($sign) : null;
}
/**
* 公钥验签
* @param string $data
*/
public static function checkSign($Arr , $signKey)
{
if (!empty($Arr)) {
ksort($Arr);
$str = '';
foreach ($Arr as $k => $v) {
if ($v == '' || $k == 'signature' || $k == 'mhtSignature') {
continue;
}
$str .= $k . '=' . $v . '&';
}
$str = substr($str, 0, -1);
$puKey = "-----BEGIN PUBLIC KEY-----
-----END PUBLIC KEY-----";
if ($puKey) {
$result = (bool)openssl_verify($str, base64_decode($signKey), $puKey, OPENSSL_ALGO_SHA256);
return $result;
}
}
return false;
}
/**
* 微众代付--单笔实时代付
* @param type $userId
* @param type $name
* @param type $card_num
* @return type
*/
public static function sendAp04($account_id,$money,$payeeName,$payeeCardNo)
{
$url = 'https://saapi.ipaynow.cn/agentpay/commonApi';
$req['funcode'] = 'AP04';
$req['version'] = '1.0.0';
$req['appId'] = '153715440751020';
$req['mhtOrderNo'] = $account_id;
$req['mhtReqTime'] = Carbon::now()->format('YmdHis');
$req['payeeAccType'] = '02';//入账账户类型
$req['mhtOrderAmt'] = $money;
$req['agentPayMemo'] = '顺风车APP提现';
$str = "payeeCardNo=$payeeCardNo&payeeName=$payeeName";
$req['secretInfo'] = self::encrypt($str);
$req['signType'] = 'RSA2';
$req['notifyUrl'] = 'https://t.pksfc.com/api/notify_ap15';
$req['signature'] = self::getSignTrueRSA2($req);
Log::info("代付".json_encode($req));
$ch = curl_init();
/* 设置验证方式 */
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type:application/json', 'charset=utf-8'));
/* 设置返回结果为流 */
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
/* 设置超时时间 */
curl_setopt($ch, CURLOPT_TIMEOUT, 5);
/* 设置通信方式 */
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($req));
$content = curl_exec($ch);
Log::info( "代付结果".$content );
return $content;
}
调用文件
/**
* 充值钱包通知
* @param Request $request
*/
public function notifyAP15()
{
$request = file_get_contents('php://input');
Log::info("代付回调参数".$request);
$Arr = json_decode($request , true);
$Arr['payeeName'] = self::unicodeDecode($Arr['payeeName']);
$sign = Inowpay::checkSign($Arr , $Arr['signature']);
if ($sign == TRUE) {
$account = Account::where('id' , $Arr['mhtOrderNo'])->first();
$account->back_content = json_encode($Arr);
if($Arr['tradeStatus'] == 00){
$account->if_arrive = 1;
$account->state = 2;
$account->save();
}else {
$account->state = 5;//(微众)代付失败
$account->save();
}
echo "success=Y";
exit;
}
echo "fail";
exit;
}
function unicodeDecode($name){
$json = '{"str":"'.$name.'"}';
$arr = json_decode($json,true);
if(empty($arr)) return '';
return $arr['str'];
}