1.访问http://web.chacuo.net/netrsakeypair
点击生成密钥对,删除RSA生成格式->BEGIN RSA PRIVATE KEY和END PUBLIC KEY,即第一行和最后一行
2.访问http://www.ab173.com/other/compress.php
将生成的公钥和私钥复制到压缩文本框中去除换行和空格
<?php
namespace App\Services;
use Illuminate\Support\Facades\Log;
class GoodPay
{
protected $mer_no;
protected $mer_order_no;
protected $pname;
protected $pemail;
protected $phone;
protected $order_amount;
protected $ccy_no;
protected $busi_code;
protected $notifyUrl;
protected $bankCode;
protected $pageUrl;
protected $sign;
protected $acc_no;
protected $acc_name;
protected $bank_code;
protected $mobile_no;
protected $province;
protected $summary;
/**
* @return mixed
*/
public function getBank_code()
{
return $this->bank_code;
}
public function setBank_code($bank_code): void
{
$this->bank_code = $bank_code;
}
/**
* @return mixed
*/
public function getAccNo()
{
return $this->acc_no;
}
/**
* @param mixed $acc_no
*/
public function setAccNo($acc_no): void
{
$this->acc_no = $acc_no;
}
/**
* @return mixed
*/
public function getAccName()
{
return $this->acc_name;
}
/**
* @param mixed $acc_name
*/
public function setAccName($acc_name): void
{
$this->acc_name = $acc_name;
}
/**
* @return mixed
*/
public function getMobileNo()
{
return $this->mobile_no;
}
/**
* @param mixed $mobile_no
*/
public function setMobileNo($mobile_no): void
{
$this->mobile_no = $mobile_no;
}
/**
* @return mixed
*/
public function getProvince()
{
return $this->province;
}
/**
* @param mixed $province
*/
public function setProvince($province): void
{
$this->province = $province;
}
/**
* @return mixed
*/
public function getSummary()
{
return $this->summary;
}
/**
* @param mixed $summary
*/
public function setSummary($summary): void
{
$this->summary = $summary;
}
public function allData()
{
$data = get_object_vars($this);
return array_filter($data);
}
public function sign($data = [])
{
$Secret = config('pay.goodpay.Secret');
if (empty($data)) {
$data = $this->allData();
}
ksort($data);
$build_query = urldecode(http_build_query($data));
$sign = md5($build_query.'&key='.$Secret);
return $sign;
}
//通知验签 $data:通知数据
public function decrypt($data)
{
$getPublicKey = config('pay.goodpay.publicKey');
ksort($data);
$toSign = '';
foreach ($data as $key => $value) {
if (strcmp($key, 'sign') != 0 && $value != '') {
$toSign .= $key . '=' . $value . '&';
}
}
$str = rtrim($toSign, '&');
$pem = chunk_split($getPublicKey, 64, "\n");
$pem = "-----BEGIN PUBLIC KEY-----\n" . $pem . "-----END PUBLIC KEY-----\n";
$publickey = openssl_pkey_get_public($pem);
$base64 = str_replace(array('-', '_'), array('+', '/'), $data['sign']);
$crypto = '';
foreach (str_split(base64_decode($base64), 128) as $chunk) {
openssl_public_decrypt($chunk, $decrypted, $publickey);
$crypto .= $decrypted;
}
if ($str != $crypto) {
Log::info('验签失败');
return false;
}
return true;
}
//加密
public function encrypt()
{
$data = $this->allData();
$getPrivateKey = config('pay.goodpay.privateKey');
ksort($data);
$str = '';
foreach ($data as $k => $v) {
if (!empty($v)) {
$str .= (string)$k . '=' . $v . '&';
}
}
$str = rtrim($str, '&');
$pem = chunk_split($getPrivateKey, 64, "\n");
$pem = "-----BEGIN PRIVATE KEY-----\n" . $pem . "-----END PRIVATE KEY-----\n";
$private_key = openssl_pkey_get_private($pem);
$crypto = '';
foreach (str_split($str, 117) as $chunk) {
openssl_private_encrypt($chunk, $encryptData, $private_key);
$crypto .= $encryptData;
}
$encrypted = base64_encode($crypto);
$encrypted = str_replace(array('+', '/', '='), array('-', '_', ''), $encrypted);
return $encrypted;
}
public function encrypt_rsa($data, $priKey)
{
$getPrivateKey = config('pay.goodpay.privateKey') . "\n";
$encrypted = '';
$pem = chunk_split($getPrivateKey, 64, "\n");
$pem = "-----BEGIN PRIVATE KEY-----\n" . $pem . "-----END PRIVATE KEY-----\n";
$private_key = openssl_pkey_get_private($pem);
$crypto = '';
foreach (str_split($data, 117) as $chunk) {
openssl_private_encrypt($chunk, $encryptData, $private_key);
$crypto .= $encryptData;
}
$encrypted = base64_encode($crypto);
$encrypted = str_replace(array('+', '/', '='), array('-', '_', ''), $encrypted);
// $data['sign']=$encrypted;
return $encrypted;
}
/**
* @return mixed
*/
public function getMerNo()
{
return $this->mer_no;
}
/**
* @param mixed $mer_no
*/
public function setMerNo($mer_no): void
{
$this->mer_no = $mer_no;
}
/**
* @return mixed
*/
public function getMerOrderNo()
{
return $this->mer_order_no;
}
/**
* @param mixed $mer_order_no
*/
public function setMerOrderNo($mer_order_no): void
{
$this->mer_order_no = $mer_order_no;
}
/**
* @return mixed
*/
public function getPname()
{
return $this->pname;
}
/**
* @param mixed $pname
*/
public function setPname($pname): void
{
$this->pname = $pname;
}
/**
* @return mixed
*/
public function getPemail()
{
return $this->pemail;
}
/**
* @param mixed $pemail
*/
public function setPemail($pemail): void
{
$this->pemail = $pemail;
}
/**
* @return mixed
*/
public function getPhone()
{
return $this->phone;
}
/**
* @param mixed $phone
*/
public function setPhone($phone): void
{
$this->phone = $phone;
}
/**
* @return mixed
*/
public function getOrderAmount()
{
return $this->order_amount;
}
/**
* @param mixed $order_amount
*/
public function setOrderAmount($order_amount): void
{
$this->order_amount = $order_amount;
}
/**
* @return mixed
*/
public function getCcyNo()
{
return $this->ccy_no;
}
/**
* @param mixed $ccy_no
*/
public function setCcyNo($ccy_no): void
{
$this->ccy_no = $ccy_no;
}
/**
* @return mixed
*/
public function getBusiCode()
{
return $this->busi_code;
}
/**
* @param mixed $busi_code
*/
public function setBusiCode($busi_code): void
{
$this->busi_code = $busi_code;
}
/**
* @return mixed
*/
public function getNotifyUrl()
{
return $this->notifyUrl;
}
/**
* @param mixed $notifyUrl
*/
public function setNotifyUrl($notifyUrl): void
{
$this->notifyUrl = $notifyUrl;
}
/**
* @return mixed
*/
public function getBankCode()
{
return $this->bankCode;
}
/**
* @param mixed $bankCode
*/
public function setBankCode($bankCode): void
{
$this->bankCode = $bankCode;
}
/**
* @return mixed
*/
public function getPageUrl()
{
return $this->pageUrl;
}
/**
* @param mixed $pageUrl
*/
public function setPageUrl($pageUrl): void
{
$this->pageUrl = $pageUrl;
}
/**
* @return mixed
*/
public function getSign()
{
return $this->sign;
}
/**
* @param mixed $sign
*/
public function setSign($sign): void
{
$this->sign = $sign;
}
}