作者 竞泽

add:part

{
"name": "lackoxygen/gz-cbec",
"type": "library",
"description": "广州单一窗口跨境电商",
"license": "MIT",
"authors": [
{
"name": "jz",
"email": "jingzeou@outlook.com"
}
],
"require": {
"php": ">=7.1",
"illuminate/support": "^5.8"
},
"require-dev": {
"phpunit/phpunit": "~4.0"
},
"autoload": {
"psr-4": {
"Lackoxygen\\GzCbec\\": "src/"
}
},
"extra": {
"laravel": {
"providers": [
"Lackoxygen\\TopWarehouse\\TopWarehouseServiceProvider"
]
}
}
}
... ...
... ... @@ -27,14 +27,7 @@ class Client implements ClientInterface
/**
* @var array
*/
protected $config = [
'base_uri' => 'https://open.singlewindow.gz.cn',
'record_sn' => 'C011111100414353',
'secret_key' => '12345678',
'company_cert_sn' => '20200000000652',
'sender_sn' => 'GEYOUSUOAI',
'receiver_sn' => 'KJPUBLICPT'
];
protected $config = [];
protected function __construct(RequestInterface $request)
{
... ... @@ -43,6 +36,7 @@ class Client implements ClientInterface
'verify' => false
]);
$this->request = $request;
$this->config = config('gz-cbec');
}
/**
... ... @@ -63,7 +57,6 @@ class Client implements ClientInterface
public function send(): ResponseInterface
{
$content = $this->request->getPacket()->getContent();
file_put_contents('transfer.xml', $content);
try {
$response = $this->guzzleHttp->request($this->request->getMethod(), $this->request->getPath(), [
RequestOptions::FORM_PARAMS => [
... ...
<?php
namespace Lackoxygen\GzCbec;
class GzCBECProvider
{
/**
* @var bool
*/
protected $defer = true;
/**
* @var array
*/
protected $commands = [];
/**
* Register any application services.
*
* @return void
*/
public function register()
{
$this->commands($this->commands);
}
public function boot()
{
$this->publishes([__DIR__ . '/../config/gz-cbec.php' => config_path('gz-cbec.php')]);
}
/**
* @return string[]
*/
public function provides()
{
return ['gzCbec', GzCBEC::class];
}
}
... ...
... ... @@ -36,15 +36,7 @@ class Packet
{
$this->request = $request;
$this->config = [
'base_uri' => 'https://open.singlewindow.gz.cn',
'record_sn' => 'C011111100414353',
'secret_key' => '12345678',
'company_cert_sn' => '20200000000652',
'sender_sn' => 'GEYOUSUOAI',
'receiver_sn' => 'KJPUBLICPT',
'signature' => "http://10.10.10.41:8080/signature.do"
];
$this->config = config('gz-cbec');
}
... ...
<?php
namespace Lackoxygen\GzCbec\Utils;
use Lackoxygen\GzCbec\Exception\Exception;
class RSA
{
/**
* @param $key
*
* @return string
*/
public static function trimKey($key): string
{
return wordwrap(preg_replace('/[\r\n]/', '', $key), 64, "\n", true);
}
/**
* @param $data
* @param $key
*
* @return mixed
*/
public static function signPrivate($data, $key)
{
$key = self::trimKey($key);
$key = "-----BEGIN RSA PRIVATE KEY-----\n{$key}\n-----END RSA PRIVATE KEY-----";
openssl_sign($data, $sign, $key, \OPENSSL_ALGO_SHA1);
return $sign;
}
/**
* @param $data
* @param $fileName
*
* @return mixed
* @throws Exception
*/
public static function signPrivateFromFile($data, $fileName)
{
$key = file_get_contents($fileName);
$res = openssl_get_privatekey($key);
if (!$res) {
throw new Exception('Incorrect public key file format');
}
openssl_sign($data, $sign, $res, \OPENSSL_ALGO_SHA1);
openssl_free_key($res);
return $sign;
}
/**
* @param $data
* @param $key
* @param $sign
*
* @return bool
*/
public static function verifyPublic($data, $key, $sign)
{
$key = static::trimKey($key);
$key = "-----BEGIN PUBLIC KEY-----\n{$key}\n-----END PUBLIC KEY-----";
return 1 === openssl_verify($data, $sign, $key, \OPENSSL_ALGO_SHA1);
}
/**
* @param $data
* @param $fileName
* @param $sign
*
* @return bool
* @throws Exception
*/
public static function verifyPublicFromFile($data, $fileName, $sign): bool
{
$key = file_get_contents($fileName);
$res = openssl_get_publickey($key);
if (!$res) {
throw new Exception('Incorrect public key file format');
}
$result = openssl_verify($data, $sign, $res, \OPENSSL_ALGO_SHA1);
openssl_free_key($res);
return 1 === $result;
}
/**
* @param $data
* @param $fileName
*
* @return mixed
* @throws Exception
*/
public static function encryptPublicFromFile($data, $fileName)
{
$res = openssl_get_publickey(file_get_contents($fileName));
if (!$res) {
throw new Exception('公钥文件格式错误');
}
openssl_public_encrypt($data, $result, $res, \OPENSSL_PKCS1_OAEP_PADDING);
openssl_free_key($res);
return $result;
}
/**
* @param $data
* @param $public
*
* @return mixed
*/
public static function encryptPublic($data, $public)
{
openssl_public_encrypt($data, $result, $public, \OPENSSL_PKCS1_OAEP_PADDING);
return $result;
}
}