14 changed files with 1457 additions and 0 deletions
@ -0,0 +1,9 @@ |
|||
-----BEGIN PUBLIC KEY----- |
|||
MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAgFoZG+UeND0T/PQK41Dg |
|||
5bXgpuHwBdc7sfGRTKlB2PmZ1Q8XE4b8u5SMqk9LC7iUL5wJlItJ9rcdXPgc3SnB |
|||
xuq133HRqvsNAxWacYQnarYMko7JKKGENdrcE+MY7ZM6Yo60/6+HVOwphIw8BuZL |
|||
NvvCKx7BrI84t93TFndn6WVF16XgOhQkPyUmsj2ekfw9vrQg89ipNcTHmBCtH9/K |
|||
KEWRPTUkH9xc+GOZ06v5O3Pmpd+hdXyE/Rjtsa1foR3l1UsWFVODHRkeoHTnBdkR |
|||
YiOGXpGthazAcvPAZtTWSKtRirKF87soIill1cmHMCw8d9JoZgq9k0L8n9ZpUCw7 |
|||
twIDAQAB |
|||
-----END PUBLIC KEY----- |
|||
@ -0,0 +1,28 @@ |
|||
-----BEGIN RSA PRIVATE KEY----- |
|||
MIIEvAIBADANBgkqhkiG9w0BAQEFAASCBKYwggSiAgEAAoIBAQCOt2qd98v3uMVM |
|||
XtDlEJBks8spkC+SSdi+/9m+6cVE/eqSqzWMAa2QeoZ7j31ard+FV2ZynITj+Y15 |
|||
6iRJ84J20k7VTuUfjLSjJ079DrRweYimf5MW/QCRyz93YMjC4A9aWrbSw06pkiz4 |
|||
p3DMkitlp9oDEx13vJld0tLw1BO4fSMmoDLg4ri3zFA/PqPb3OWW0R/yRakOdiPx |
|||
kjsOYZ7EOesdc67gdYCiiEy2x38jGMKAJ5jqWmRbnCF+4Ij7QPGXQTG7ba63azNN |
|||
S8mBis5WNHA1EPK+KgRDykZHWTzV8Hu/x5NRnBUYpTdSlLM2zP//8dAcUOUejakv |
|||
DSWbLPDVAgMBAAECggEAVm5KSYTqqAnkow3BIRjY+cPRREioJsZGV9YG/xIAgHnu |
|||
eAjsEvBpNtidXXNXDXXDqrN1ve+Ao7hcr/rXI1owJtnTGGgBZwwF0dFaivM+3+ca |
|||
lQsp/4AVujYHJ7sbmrmrUqMhmZ7iott+FNEDBUF4usXeyIHpFLzJfQLS8Tre/pkS |
|||
KVcepRBKD7WSwSaqI5Gb9i+HHdqgE1UhY4nqMt1F07szFmPSCsS8L5DVGRTKbOlW |
|||
KwwbgyY53aUCb1xVVHXhyxCFTU8a02yBwWDXCeG+Qvnu2qED+wui7yF77299ypPP |
|||
oim72rjyxj1736+PSymgPlNuLFPkH/HLac5jxdR7eQKBgQDQq6nORGAJpPmt+BLY |
|||
nJtJHCJiU4cf4dJ1O+LnUTvlO1dl68wh/fulOG6xwngTXB9izq9Aj6CEGsW7GCne |
|||
3Q1vBELlmDcqp1AiyvpHaymtYWRr6+DdW0DKWTfcqaaF+8GEx6RwXBuwUji7kh5y |
|||
wCCP3ZLZG9zx7DTL9F0+2h7TwwKBgQCvFiqtPK72VT9C9+Q+Ct8Bwhzv0WDbTwx6 |
|||
Ke5YXWuI8mm07TA/osVav4etLrwlKine6QqZ9+1GnYtB09EKX1nmFBgiOgR52woe |
|||
hOhOYTG4bFRfH7caiHcgR3L0nsXCdjlfpxqLiUCvFYzwYsXnIGfQYZvWLELFua5o |
|||
dhRyTfxXhwKBgHxEzwJE3ADCW+ML6oVPdnbqp1txLPaCsKgaev2Q9h+E4H6VF2Ni |
|||
8xso6Vs5WfMFmLdqKRpErq4dl4XdVQohEN/9ZK2AodHoBCRERxqDKjXT208eETMi |
|||
JgqbaVYjPkVqcYvWrQoxAFhYDus9kJ575qhmpw5etozoQ2T26RUGJverAoGANKwy |
|||
gpNy/2B9+6Of4ppapKjFE9ClltSdKFNdBPB13WkwK+H/VeWeAcMFCjC5IWUh25G4 |
|||
QsG3fDsBHfifNsOgw/rfl8xkdFRVIwFQ8eODlbvhVgywhSbwz6EvEDCDxhDTCde3 |
|||
wKq17vZIxDiy2OmC0cnLnA8Mt3c45OxBEnIrrBkCgYBgaR0UQJq/Xfnu+CV8/hV3 |
|||
HYpzquWzqWve3DgEGSXA/fAVSm6JhcumndywlBVXXyML+A+U6MVdJfGVbLSHxLQV |
|||
1f9nTAwXPbauFLrqpHm0hOvCwfAEkK8HDt2mKOfblbWrLo7gF7wErcSEw7tEqJOg |
|||
M2OG+6QXvCsCZUKBf8TnnQ== |
|||
-----END RSA PRIVATE KEY----- |
|||
@ -0,0 +1,16 @@ |
|||
-----BEGIN PRIVATE KEY----- |
|||
MIICdQIBADANBgkqhkiG9w0BAQEFAASCAl8wggJbAgEAAoGBAJgHMGYsspghvP+y |
|||
CbjLG43CkZuQ3YJyDcmEKxvmgblITfmiTPx2b9Y2iwDT9gnLGExTDm1BL2A8VzMo |
|||
bjaHfiCmTbDctu680MLmpDDkVXmJOqdlXh0tcLjhN4+iDA2KkRqiHxsDpiaKT6MM |
|||
BuecXQbJtPlVc1XjVhoUlzUgPCrvAgMBAAECgYAV9saYTGbfsdLOF5kYo0dve1Jx |
|||
aO7dFMCcgkV+z2ujKtNmeHtU54DlhZXJiytQY5Dhc10cjb6xfFDrftuFcfKCaLiy |
|||
6h5ETR8jyv5He6KH/+X6qkcGTkJBYG1XvyyFO3PxoszQAs0mrLCqq0UItlCDn0G7 |
|||
2MR9/NuvdYabGHSzEQJBAMXB1/DUvBTHHH4LiKDiaREruBb3QtP72JQS1ATVXA2v |
|||
6xJzGPMWMBGQDvRfPvuCPVmbHENX+lRxMLp39OvIn6kCQQDEzYpPcuHW/7h3TYHY |
|||
c+T0O6z1VKQT2Mxv92Lj35g1XqV4Oi9xrTj2DtMeV1lMx6n/3icobkCQtuvTI+Ac |
|||
qfTXAkB6bCz9NwUUK8sUsJktV9xJN/JnrTxetOr3h8xfDaJGCuCQdFY+rj6lsLPB |
|||
TnFUC+Vk4mQVwJIE0mmjFf22NWW5AkAmsVaRGkAmui41Xoq52MdZ8WWm8lY0BLrl |
|||
BJlvveU6EPqtcZskWW9KiU2euIO5IcRdpvrB6zNMgHpLD9GfMRcPAkBUWOV/dH13 |
|||
v8V2Y/Fzuag/y5k3/oXi/WQnIxdYbltad2xjmofJ7DbB7MJqiZZD8jlr8PCZPwRN |
|||
zc5ntDStc959 |
|||
-----END PRIVATE KEY----- |
|||
@ -0,0 +1,49 @@ |
|||
<?php |
|||
|
|||
return array( |
|||
// 机构号 商户号 |
|||
'org_num' => '5503001', |
|||
// 'org_num' => '100018380507D2C', |
|||
// 商户号 渠道商户号 |
|||
'cusid' => '10147', |
|||
// 'cusid' => '563222080502LGB', |
|||
// 产品 |
|||
'producp' => 'JX0002', |
|||
// 版本 |
|||
'version' => '11', |
|||
// 门店号 |
|||
'branchno' => '44', |
|||
// 终端号 |
|||
'termcode' => '000000', |
|||
// 加密方式 |
|||
'signtype' => 'RSA', |
|||
// 渠道公钥 |
|||
'public_key' => dirname(__FILE__) . '/../cert/channel_public_key.pem', |
|||
// 商户私钥 |
|||
'private_key' => dirname(__FILE__) . '/../cert/cus_private_key.pem', |
|||
|
|||
// 统一支付接口 测试地址 |
|||
'pay_url' => 'https://open.dalianpay.cn/payapi/unitorder/pay', |
|||
// 统一支付接口 |
|||
// 'pay_url' => 'https://epay.dalianpay.cn/payapi/unitorder/pay', |
|||
|
|||
// 统一扫码接口 测试地址 |
|||
'scan_url' => 'https://open.dalianpay.cn/payapi/unitorder/scanqrpay', |
|||
|
|||
// 交易撤销 测试地址 |
|||
'cancel_url' => 'https://open.dalianpay.cn/payapi/tranx/cancel', |
|||
|
|||
// 交易退款 测试地址 |
|||
'refund_url' => 'https://tpay.01pub.com/payapi/tranx/refund', |
|||
|
|||
// 统一查询接口 测试地址 |
|||
'query_url' => 'https://open.dalianpay.cn/payapi/tranx/query', |
|||
|
|||
// 订单关闭接口 测试地址 |
|||
'close_url' => 'https://open.dalianpay.cn/payapi/tranx/close', |
|||
|
|||
'openid' => '', |
|||
|
|||
); |
|||
|
|||
?> |
|||
@ -0,0 +1,151 @@ |
|||
<?php |
|||
|
|||
// 统一支付接口Demo |
|||
|
|||
// 加载基础类 |
|||
require_once('../library/Base.php'); |
|||
$Base = new Base(); |
|||
|
|||
// 获取配置信息 |
|||
$config = $Base->getConfig(); |
|||
// var_dump('<pre>', $config); |
|||
|
|||
// 接口URL(测试) |
|||
$api_url = $config['cancel_url']; |
|||
|
|||
// 加个body字段传中文试试 |
|||
|
|||
// 组装交易报文 |
|||
$reqsn_prefix = date('Ymd'); |
|||
$trade_data = array( |
|||
'orgid' => $config['org_num'], |
|||
'cusid' => $config['cusid'], |
|||
'branchno' => $config['branchno'], |
|||
'termcode' => $config['termcode'], |
|||
'version' => $config['version'], |
|||
'trxamt' => '1', |
|||
'reqsn' => uniqid($reqsn_prefix), |
|||
'producp' => 'JX0002', |
|||
'transtype' => 'JSP511', |
|||
'randomstr' => md5( uniqid() ), |
|||
// 'oldreqsn' => '2023072464be31055e0e0', |
|||
'oldtrxid' => '100000018608497', |
|||
); |
|||
|
|||
// $trade_data = array( |
|||
// "cusid" => "10147", |
|||
// "branchno" => "44", |
|||
// "termcode" => "000000", |
|||
// "version" => "11", |
|||
// "trxamt" => "1154634", |
|||
// "reqsn" => "112094120001088317", |
|||
// "producp" => "JX0002", |
|||
// "transtype" => "JSP501", |
|||
// "paytype" => "W01", |
|||
// "randomstr" => md5( uniqid() ), |
|||
// "body" => "", |
|||
// "remark" => "", |
|||
// "validtime" => "", |
|||
// "acct" => "", |
|||
// "notify_url" => "", |
|||
// "limit_pay" => "", |
|||
// "sub_appid" => "", |
|||
// "goods_tag" => "", |
|||
// "benefitdetail" => "", |
|||
// "chnlstoreid" => "", |
|||
// "extendparams" => "", |
|||
// "cusip" => "", |
|||
// "front_url" => "", |
|||
// "subbranch" => "", |
|||
// "idno" => "", |
|||
// "truename" => "", |
|||
// "asinfo" => "", |
|||
// "fqnum" => "", |
|||
// "unpid" => "", |
|||
// "terminfo" => "", |
|||
// // 其他参数... |
|||
// ); |
|||
|
|||
// $trade_data = array( |
|||
// "appid"=>"00000051", |
|||
// "cusid"=>"990581007426001", |
|||
// "randomstr"=>"82712208", |
|||
// "signtype"=>"RSA", |
|||
// "trxid"=>"112094120001088317", |
|||
// "version"=>"11", |
|||
// ); |
|||
|
|||
// 生成签名 |
|||
$sign = $Base->Crypt->makeSign($trade_data); |
|||
if($sign === false) |
|||
{ |
|||
echo $Base->Crypt->err_msg; |
|||
die(); |
|||
} |
|||
echo "\r\n"; |
|||
echo "[sign]: {$sign}"; |
|||
// die(); |
|||
|
|||
// 加密交易报文 |
|||
$crypted_trade_data = $Base->Crypt->encryptTradeData($trade_data); |
|||
if($crypted_trade_data === false) |
|||
{ |
|||
echo $Base->Crypt->err_msg; |
|||
die(); |
|||
} |
|||
echo "\r\n"; |
|||
echo "[crypted message]: {$crypted_trade_data}"; |
|||
// die(); |
|||
|
|||
// 发送报文 |
|||
echo "\r\n"; |
|||
var_dump($api_url); |
|||
$response = $Base->Request->send($api_url, $crypted_trade_data, $sign); |
|||
if($response === false) |
|||
{ |
|||
echo $Base->Request->err_msg; |
|||
die(); |
|||
} |
|||
|
|||
echo "\r\n"; |
|||
|
|||
echo "[response message]: {$response}"; |
|||
|
|||
// 解析响应报文 |
|||
$response = json_decode($response, true); |
|||
$response_data_crypted = $response['data']; |
|||
$response_sign = $response['sign']; |
|||
echo "\r\n"; |
|||
echo "[response sign]: {$response_sign}"; |
|||
echo "\r\n"; |
|||
echo "[response data (ciphertext)]: {$response_data_crypted}"; |
|||
|
|||
// 解密响应密文 |
|||
$response_data = $Base->Crypt->decryptTradeData($response_data_crypted); |
|||
if($response_data === false) |
|||
{ |
|||
echo $Base->Crypt->err_msg; |
|||
die(); |
|||
} |
|||
echo "\r\n"; |
|||
// $response_data = mb_convert_encoding($response_data, 'UTF-8', 'GBK'); |
|||
echo "[response data(clear text)]: {$response_data}"; |
|||
// die(); |
|||
|
|||
?> |
|||
|
|||
|
|||
|
|||
|
|||
|
|||
<!DOCTYPE html> |
|||
<html> |
|||
<head> |
|||
<meta charset="utf-8"> |
|||
<title>交易撤销Demo</title> |
|||
<style> |
|||
body{ word-break: break-word; } |
|||
</style> |
|||
</head> |
|||
<body></body> |
|||
</html> |
|||
@ -0,0 +1,99 @@ |
|||
<?php |
|||
|
|||
// 统一支付接口Demo |
|||
|
|||
// 加载基础类 |
|||
require_once('../library/Base.php'); |
|||
$Base = new Base(); |
|||
|
|||
// 获取配置信息 |
|||
$config = $Base->getConfig(); |
|||
// var_dump('<pre>', $config); |
|||
|
|||
// 接口URL(测试) |
|||
$api_url = $config['close_url']; |
|||
|
|||
// 加个body字段传中文试试 |
|||
|
|||
// 组装交易报文 |
|||
$reqsn_prefix = date('Ymd'); |
|||
$trade_data = array( |
|||
'orgid' => $config['org_num'], |
|||
'cusid' => $config['cusid'], |
|||
'branchno' => $config['branchno'], |
|||
'termcode' => $config['termcode'], |
|||
'version' => $config['version'], |
|||
'randomstr' => md5( uniqid() ), |
|||
'oldtrxid' => '100000018608526', |
|||
); |
|||
|
|||
// 生成签名 |
|||
$sign = $Base->Crypt->makeSign($trade_data); |
|||
if($sign === false) |
|||
{ |
|||
echo $Base->Crypt->err_msg; |
|||
die(); |
|||
} |
|||
echo "\r\n"; |
|||
echo "[sign]: {$sign}"; |
|||
// die(); |
|||
|
|||
// 加密交易报文 |
|||
$crypted_trade_data = $Base->Crypt->encryptTradeData($trade_data); |
|||
if($crypted_trade_data === false) |
|||
{ |
|||
echo $Base->Crypt->err_msg; |
|||
die(); |
|||
} |
|||
echo "\r\n"; |
|||
echo "[crypted message]: {$crypted_trade_data}"; |
|||
// die(); |
|||
|
|||
// 发送报文 |
|||
echo "\r\n"; |
|||
var_dump($api_url); |
|||
$response = $Base->Request->send($api_url, $crypted_trade_data, $sign); |
|||
if($response === false) |
|||
{ |
|||
echo $Base->Request->err_msg; |
|||
die(); |
|||
} |
|||
|
|||
echo "\r\n"; |
|||
|
|||
echo "[response message]: {$response}"; |
|||
|
|||
// 解析响应报文 |
|||
$response = json_decode($response, true); |
|||
$response_data_crypted = $response['data']; |
|||
$response_sign = $response['sign']; |
|||
echo "\r\n"; |
|||
echo "[response sign]: {$response_sign}"; |
|||
echo "\r\n"; |
|||
echo "[response data (ciphertext)]: {$response_data_crypted}"; |
|||
|
|||
// 解密响应密文 |
|||
$response_data = $Base->Crypt->decryptTradeData($response_data_crypted); |
|||
if($response_data === false) |
|||
{ |
|||
echo $Base->Crypt->err_msg; |
|||
die(); |
|||
} |
|||
echo "\r\n"; |
|||
echo "[response data(clear text)]: {$response_data}"; |
|||
// die(); |
|||
|
|||
?> |
|||
|
|||
|
|||
<!DOCTYPE html> |
|||
<html> |
|||
<head> |
|||
<meta charset="utf-8"> |
|||
<title>订单关闭Demo</title> |
|||
<style> |
|||
body{ word-break: break-word; } |
|||
</style> |
|||
</head> |
|||
<body></body> |
|||
</html> |
|||
@ -0,0 +1,165 @@ |
|||
<?php |
|||
/** |
|||
* dalianpay插件 用于获取prepareid |
|||
*/ |
|||
class dalianpay{ |
|||
public function encrypt($plaintext) { |
|||
$length = mb_strlen($plaintext); |
|||
$ciphertext = ''; |
|||
for ($i = 0; $i < $length; $i += 2) { |
|||
$ciphertext .= mb_substr($plaintext, $i + 1, 1) . mb_substr($plaintext, $i, 1); |
|||
} |
|||
return $ciphertext; |
|||
} |
|||
|
|||
public function decrypt($ciphertext) { |
|||
$length = mb_strlen($ciphertext); |
|||
$plaintext = ''; |
|||
for ($i = 0; $i < $length; $i += 2) { |
|||
$plaintext .= mb_substr($ciphertext, $i + 1, 1) . mb_substr($ciphertext, $i, 1); |
|||
} |
|||
return $plaintext; |
|||
} |
|||
public function pay($openid_crypt, $order_sn, $order_amount) |
|||
{ |
|||
session_start(); |
|||
// $openid = $_SESSION['openid']; |
|||
// $openid = $this->decrypt($_GET['openid']); |
|||
// $reqsn = $_GET['reqsn']; |
|||
// $trxamt = $_GET['trxamt']; |
|||
|
|||
$openid = $this->decrypt($openid_crypt); |
|||
$reqsn = $order_sn; |
|||
$trxamt = $order_amount; |
|||
|
|||
// var_dump($openid); |
|||
// var_dump($reqsn); |
|||
// var_dump($trxamt); |
|||
$redirect_url = $_SESSION['prev_url']; |
|||
// unset($_SESSION['prev_url']); |
|||
// var_dump($redirect_url); |
|||
|
|||
// 加载基础类 |
|||
require_once('../library/Base.php'); |
|||
$Base = new Base(); |
|||
|
|||
// 获取配置信息 |
|||
$config = $Base->getConfig(); |
|||
// var_dump('<pre>', $config); |
|||
|
|||
// 接口URL(测试) |
|||
$api_url = $config['pay_url']; |
|||
// $api_url = $_POST['url']; |
|||
$message = array( |
|||
"url"=>$api_url, |
|||
); |
|||
// echo json_encode($message,true); |
|||
// 加个body字段传中文试试 |
|||
|
|||
// 组装交易报文 |
|||
$reqsn_prefix = date('Ymd'); |
|||
// var_dump("uniq_id(reqsn)"); |
|||
// echo "\r\n"; |
|||
// var_dump(uniqid($reqsn_prefix)); |
|||
$trade_data = array( |
|||
'orgid' => $config['org_num'], |
|||
'cusid' => $config['cusid'], |
|||
'branchno' => $config['branchno'], |
|||
// 'termcode' => $config['termcode'], |
|||
'version' => $config['version'], |
|||
'trxamt' => $trxamt, // total_fee |
|||
'reqsn' => $reqsn, // 交易订单号 |
|||
'producp' => 'JX0002', |
|||
'transtype' => 'JSP501', // 微信支付 |
|||
'paytype' => 'W02', // 微信js支付 |
|||
'randomstr' => md5( uniqid() ), |
|||
// 'body' => 'pages', |
|||
'acct' => $openid, |
|||
// 'notify_url' => '', // 交易结果通知地址 |
|||
// 'sub_appid' => 'wx79343915f99167e6', |
|||
// 'front_url' => '', // 支付完成跳转 |
|||
); |
|||
// 生成签名 |
|||
$sign = $Base->Crypt->makeSign($trade_data); |
|||
if($sign === false) |
|||
{ |
|||
// echo $Base->Crypt->err_msg; |
|||
$errorMessage = array( |
|||
"data"=>"makeSign error..." |
|||
); |
|||
return json_encode($errorMessage,true); |
|||
die(); |
|||
} |
|||
|
|||
// echo "\r\n"; |
|||
// echo "[sign]: {$sign}"; |
|||
|
|||
// 加密交易报文 |
|||
$crypted_trade_data = $Base->Crypt->encryptTradeData($trade_data); |
|||
if($crypted_trade_data === false) |
|||
{ |
|||
// echo $Base->Crypt->err_msg; |
|||
$errorMessage = array( |
|||
"data"=>"encryptTradeData error..." |
|||
); |
|||
return json_encode($errorMessage,true); |
|||
die(); |
|||
} |
|||
|
|||
// echo "\r\n"; |
|||
// echo "[crypted message]: {$crypted_trade_data}"; |
|||
|
|||
// 发送报文 |
|||
// echo "\r\n"; |
|||
// var_dump($api_url); |
|||
$response = $Base->Request->send($api_url, $crypted_trade_data, $sign); |
|||
echo $response; |
|||
if($response === false) |
|||
{ |
|||
// echo $Base->Request->err_msg; |
|||
$errorMessage = array( |
|||
"data"=>"send error..." |
|||
); |
|||
return json_encode($errorMessage,true); |
|||
die(); |
|||
} |
|||
// echo "\r\n"; |
|||
// echo "[response message]: {$response}"; |
|||
|
|||
// 解析响应报文 |
|||
$response = json_decode($response, true); |
|||
$response_data_crypted = $response['data']; |
|||
$response_sign = $response['sign']; |
|||
// echo "\r\n"; |
|||
// echo "[response sign]: {$response_sign}"; |
|||
// echo "\r\n"; |
|||
// echo "[response data (ciphertext)]: {$response_data_crypted}"; |
|||
|
|||
|
|||
// 解密响应密文 |
|||
$response_data = $Base->Crypt->decryptTradeData($response_data_crypted); |
|||
if($response_data === false) |
|||
{ |
|||
// echo $Base->Crypt->err_msg; |
|||
$errorMessage = array( |
|||
"data"=>"decryptTradeData error..." |
|||
); |
|||
return json_encode($errorMessage,true); |
|||
die(); |
|||
} |
|||
// echo "\r\n"; |
|||
// echo "[response data(clear text)]: {$response_data}"; |
|||
// die(); |
|||
var_dump(json_decode($response_data)); |
|||
$_SESSION['response_data_json'] = $response_data; |
|||
return $response_data; |
|||
|
|||
// var_dump($_SESSION['user_id']); |
|||
// 跳回之前的地址 |
|||
// header('Location: ' . $redirect_url); |
|||
// exit; |
|||
} |
|||
} |
|||
|
|||
?> |
|||
|
|||
@ -0,0 +1,162 @@ |
|||
<?php |
|||
|
|||
function encrypt($plaintext) { |
|||
$length = mb_strlen($plaintext); |
|||
$ciphertext = ''; |
|||
for ($i = 0; $i < $length; $i += 2) { |
|||
$ciphertext .= mb_substr($plaintext, $i + 1, 1) . mb_substr($plaintext, $i, 1); |
|||
} |
|||
return $ciphertext; |
|||
} |
|||
|
|||
function decrypt($ciphertext) { |
|||
$length = mb_strlen($ciphertext); |
|||
$plaintext = ''; |
|||
for ($i = 0; $i < $length; $i += 2) { |
|||
$plaintext .= mb_substr($ciphertext, $i + 1, 1) . mb_substr($ciphertext, $i, 1); |
|||
} |
|||
return $plaintext; |
|||
} |
|||
function pay($openid_crypt, $order_sn, $order_amount) |
|||
{ |
|||
session_start(); |
|||
// $openid = $_SESSION['openid']; |
|||
// $openid = decrypt($_GET['openid']); |
|||
// $reqsn = $_GET['reqsn']; |
|||
// $trxamt = $_GET['trxamt']; |
|||
|
|||
$openid = decrypt($openid_crypt); |
|||
$reqsn = $order_sn; |
|||
$trxamt = $order_amount; |
|||
|
|||
// var_dump($openid); |
|||
// var_dump($reqsn); |
|||
// var_dump($trxamt); |
|||
$redirect_url = $_SESSION['prev_url']; |
|||
// unset($_SESSION['prev_url']); |
|||
// var_dump($redirect_url); |
|||
|
|||
// 加载基础类 |
|||
require_once('../library/Base.php'); |
|||
$Base = new Base(); |
|||
|
|||
// 获取配置信息 |
|||
$config = $Base->getConfig(); |
|||
// var_dump('<pre>', $config); |
|||
|
|||
// 接口URL(测试) |
|||
$api_url = $config['pay_url']; |
|||
// $api_url = $_POST['url']; |
|||
$message = array( |
|||
"url"=>$api_url, |
|||
); |
|||
// echo json_encode($message,true); |
|||
// 加个body字段传中文试试 |
|||
|
|||
// 组装交易报文 |
|||
$reqsn_prefix = date('Ymd'); |
|||
// var_dump("uniq_id(reqsn)"); |
|||
// echo "\r\n"; |
|||
// var_dump(uniqid($reqsn_prefix)); |
|||
$trade_data = array( |
|||
'orgid' => $config['org_num'], |
|||
'cusid' => $config['cusid'], |
|||
'branchno' => $config['branchno'], |
|||
// 'termcode' => $config['termcode'], |
|||
'version' => $config['version'], |
|||
'trxamt' => $trxamt, // total_fee |
|||
'reqsn' => $reqsn, // 交易订单号 |
|||
'producp' => 'JX0002', |
|||
'transtype' => 'JSP501', // 微信支付 |
|||
'paytype' => 'W02', // 微信js支付 |
|||
'randomstr' => md5( uniqid() ), |
|||
// 'body' => 'pages', |
|||
'acct' => $openid, |
|||
// 'notify_url' => '', // 交易结果通知地址 |
|||
// 'sub_appid' => 'wx79343915f99167e6', |
|||
// 'front_url' => '', // 支付完成跳转 |
|||
); |
|||
// 生成签名 |
|||
$sign = $Base->Crypt->makeSign($trade_data); |
|||
if($sign === false) |
|||
{ |
|||
// echo $Base->Crypt->err_msg; |
|||
$errorMessage = array( |
|||
"data"=>"makeSign error..." |
|||
); |
|||
return json_encode($errorMessage,true); |
|||
die(); |
|||
} |
|||
|
|||
// echo "\r\n"; |
|||
// echo "[sign]: {$sign}"; |
|||
|
|||
// 加密交易报文 |
|||
$crypted_trade_data = $Base->Crypt->encryptTradeData($trade_data); |
|||
if($crypted_trade_data === false) |
|||
{ |
|||
// echo $Base->Crypt->err_msg; |
|||
$errorMessage = array( |
|||
"data"=>"encryptTradeData error..." |
|||
); |
|||
return json_encode($errorMessage,true); |
|||
die(); |
|||
} |
|||
|
|||
// echo "\r\n"; |
|||
// echo "[crypted message]: {$crypted_trade_data}"; |
|||
|
|||
// 发送报文 |
|||
// echo "\r\n"; |
|||
// var_dump($api_url); |
|||
$response = $Base->Request->send($api_url, $crypted_trade_data, $sign); |
|||
echo $response; |
|||
if($response === false) |
|||
{ |
|||
// echo $Base->Request->err_msg; |
|||
$errorMessage = array( |
|||
"data"=>"send error..." |
|||
); |
|||
return json_encode($errorMessage,true); |
|||
die(); |
|||
} |
|||
// echo "\r\n"; |
|||
// echo "[response message]: {$response}"; |
|||
|
|||
// 解析响应报文 |
|||
$response = json_decode($response, true); |
|||
$response_data_crypted = $response['data']; |
|||
$response_sign = $response['sign']; |
|||
// echo "\r\n"; |
|||
// echo "[response sign]: {$response_sign}"; |
|||
// echo "\r\n"; |
|||
// echo "[response data (ciphertext)]: {$response_data_crypted}"; |
|||
|
|||
|
|||
// 解密响应密文 |
|||
$response_data = $Base->Crypt->decryptTradeData($response_data_crypted); |
|||
if($response_data === false) |
|||
{ |
|||
// echo $Base->Crypt->err_msg; |
|||
$errorMessage = array( |
|||
"data"=>"decryptTradeData error..." |
|||
); |
|||
return json_encode($errorMessage,true); |
|||
die(); |
|||
} |
|||
// echo "\r\n"; |
|||
// echo "[response data(clear text)]: {$response_data}"; |
|||
// die(); |
|||
var_dump(json_decode($response_data)); |
|||
$_SESSION['response_data_json'] = $response_data; |
|||
return $response_data; |
|||
|
|||
// var_dump($_SESSION['user_id']); |
|||
// 跳回之前的地址 |
|||
// header('Location: ' . $redirect_url); |
|||
// exit; |
|||
} |
|||
|
|||
// return pay(); |
|||
?> |
|||
|
|||
@ -0,0 +1,98 @@ |
|||
<?php |
|||
|
|||
// 统一支付接口Demo |
|||
|
|||
// 加载基础类 |
|||
require_once('../library/Base.php'); |
|||
$Base = new Base(); |
|||
|
|||
// 获取配置信息 |
|||
$config = $Base->getConfig(); |
|||
// var_dump('<pre>', $config); |
|||
|
|||
// 接口URL(测试) |
|||
$api_url = $config['query_url']; |
|||
|
|||
// 加个body字段传中文试试 |
|||
|
|||
// 组装交易报文 |
|||
$reqsn_prefix = date('Ymd'); |
|||
$trade_data = array( |
|||
'orgid' => $config['org_num'], |
|||
'cusid' => $config['cusid'], |
|||
'version' => $config['version'], |
|||
// 'reqsn' => '2023072464be320f45f2a', |
|||
'trxid' => '100000018608526', // 渠道交易流水号 如果返回的有 优先使用 |
|||
'randomstr' => md5( uniqid() ), |
|||
); |
|||
|
|||
// 生成签名 |
|||
$sign = $Base->Crypt->makeSign($trade_data); |
|||
if($sign === false) |
|||
{ |
|||
echo $Base->Crypt->err_msg; |
|||
die(); |
|||
} |
|||
echo "\r\n"; |
|||
echo "[sign]: {$sign}"; |
|||
// die(); |
|||
|
|||
// 加密交易报文 |
|||
$crypted_trade_data = $Base->Crypt->encryptTradeData($trade_data); |
|||
if($crypted_trade_data === false) |
|||
{ |
|||
echo $Base->Crypt->err_msg; |
|||
die(); |
|||
} |
|||
echo "\r\n"; |
|||
echo "[crypted message]: {$crypted_trade_data}"; |
|||
// die(); |
|||
|
|||
// 发送报文 |
|||
echo "\r\n"; |
|||
var_dump($api_url); |
|||
$response = $Base->Request->send($api_url, $crypted_trade_data, $sign); |
|||
if($response === false) |
|||
{ |
|||
echo $Base->Request->err_msg; |
|||
die(); |
|||
} |
|||
|
|||
echo "\r\n"; |
|||
|
|||
echo "[response message]: {$response}"; |
|||
|
|||
// 解析响应报文 |
|||
$response = json_decode($response, true); |
|||
$response_data_crypted = $response['data']; |
|||
$response_sign = $response['sign']; |
|||
echo "\r\n"; |
|||
echo "[response sign]: {$response_sign}"; |
|||
echo "\r\n"; |
|||
echo "[response data (ciphertext)]: {$response_data_crypted}"; |
|||
|
|||
// 解密响应密文 |
|||
$response_data = $Base->Crypt->decryptTradeData($response_data_crypted); |
|||
if($response_data === false) |
|||
{ |
|||
echo $Base->Crypt->err_msg; |
|||
die(); |
|||
} |
|||
echo "\r\n"; |
|||
echo "[response data(clear text)]: {$response_data}"; |
|||
// die(); |
|||
|
|||
?> |
|||
|
|||
|
|||
<!DOCTYPE html> |
|||
<html> |
|||
<head> |
|||
<meta charset="utf-8"> |
|||
<title>统一查询Demo</title> |
|||
<style> |
|||
body{ word-break: break-word; } |
|||
</style> |
|||
</head> |
|||
<body></body> |
|||
</html> |
|||
@ -0,0 +1,105 @@ |
|||
<?php |
|||
|
|||
// 统一支付接口Demo |
|||
|
|||
// 加载基础类 |
|||
require_once('../library/Base.php'); |
|||
$Base = new Base(); |
|||
|
|||
// 获取配置信息 |
|||
$config = $Base->getConfig(); |
|||
// var_dump('<pre>', $config); |
|||
|
|||
// 接口URL(测试) |
|||
$api_url = $config['refund_url']; |
|||
|
|||
// 加个body字段传中文试试 |
|||
|
|||
// 组装交易报文 |
|||
$reqsn_prefix = date('Ymd'); |
|||
$trade_data = array( |
|||
// 'orgid' => $config['org_num'], |
|||
'cusid' => $config['cusid'], |
|||
'branchno' => $config['branchno'], |
|||
// 'termcode' => $config['termcode'], |
|||
'version' => $config['version'], |
|||
'trxamt' => '1', |
|||
'reqsn' => uniqid($reqsn_prefix), |
|||
'producp' => 'JX0002', |
|||
'transtype' => 'JSP511', |
|||
'randomstr' => md5( uniqid() ), |
|||
// 'oldreqsn' => '100000018608489', |
|||
'oldtrxid' => '100000018608532', |
|||
); |
|||
|
|||
// 生成签名 |
|||
$sign = $Base->Crypt->makeSign($trade_data); |
|||
if($sign === false) |
|||
{ |
|||
echo $Base->Crypt->err_msg; |
|||
die(); |
|||
} |
|||
echo "\r\n"; |
|||
echo "[sign]: {$sign}"; |
|||
// die(); |
|||
|
|||
// 加密交易报文 |
|||
$crypted_trade_data = $Base->Crypt->encryptTradeData($trade_data); |
|||
if($crypted_trade_data === false) |
|||
{ |
|||
echo $Base->Crypt->err_msg; |
|||
die(); |
|||
} |
|||
echo "\r\n"; |
|||
echo "[crypted message]: {$crypted_trade_data}"; |
|||
// die(); |
|||
|
|||
// 发送报文 |
|||
echo "\r\n"; |
|||
var_dump($api_url); |
|||
$response = $Base->Request->send($api_url, $crypted_trade_data, $sign); |
|||
if($response === false) |
|||
{ |
|||
echo $Base->Request->err_msg; |
|||
die(); |
|||
} |
|||
|
|||
echo "\r\n"; |
|||
|
|||
echo "[response message]: {$response}"; |
|||
|
|||
// 解析响应报文 |
|||
$response = json_decode($response, true); |
|||
$response_data_crypted = $response['data']; |
|||
$response_sign = $response['sign']; |
|||
echo "\r\n"; |
|||
echo "[response sign]: {$response_sign}"; |
|||
echo "\r\n"; |
|||
echo "[response data (ciphertext)]: {$response_data_crypted}"; |
|||
|
|||
// 解密响应密文 |
|||
$response_data = $Base->Crypt->decryptTradeData($response_data_crypted); |
|||
if($response_data === false) |
|||
{ |
|||
echo $Base->Crypt->err_msg; |
|||
die(); |
|||
} |
|||
echo "\r\n"; |
|||
echo "[response data(clear text)]: {$response_data}"; |
|||
// die(); |
|||
|
|||
?> |
|||
|
|||
|
|||
|
|||
<!DOCTYPE html> |
|||
<html> |
|||
<head> |
|||
<meta charset="utf-8"> |
|||
<title>统一扫码Demo</title> |
|||
<style> |
|||
body{ word-break: break-word; } |
|||
</style> |
|||
</head> |
|||
<body></body> |
|||
</html> |
|||
@ -0,0 +1,158 @@ |
|||
<?php |
|||
|
|||
// 统一支付接口Demo |
|||
|
|||
// 加载基础类 |
|||
require_once('../library/Base.php'); |
|||
$Base = new Base(); |
|||
|
|||
// 获取配置信息 |
|||
$config = $Base->getConfig(); |
|||
// var_dump('<pre>', $config); |
|||
|
|||
// 接口URL(测试) |
|||
$api_url = $config['scan_url']; |
|||
|
|||
// 加个body字段传中文试试 |
|||
|
|||
// 组装交易报文 |
|||
$reqsn_prefix = date('Ymd'); |
|||
$terminfo = array( |
|||
"termsn"=>"dfjskljioe13238023", |
|||
"longitude"=>"+37.12", |
|||
"latitude"=>"-37.12", |
|||
"termno"=>"00000001", |
|||
"devicetype"=>"04", |
|||
); |
|||
$trade_data = array( |
|||
'orgid' => $config['org_num'], |
|||
'cusid' => $config['cusid'], |
|||
'branchno' => $config['branchno'], |
|||
'termcode' => $config['termcode'], |
|||
'version' => $config['version'], |
|||
'trxamt' => '1', |
|||
'reqsn' => uniqid($reqsn_prefix), |
|||
'producp' => 'JX0002', |
|||
'transtype' => 'JSP511', |
|||
'paytype' => 'W01', |
|||
'randomstr' => md5( uniqid() ), |
|||
'terminfo' => json_encode($terminfo), |
|||
'authcode' => '', |
|||
); |
|||
|
|||
// $trade_data = array( |
|||
// "cusid" => "10147", |
|||
// "branchno" => "44", |
|||
// "termcode" => "000000", |
|||
// "version" => "11", |
|||
// "trxamt" => "1154634", |
|||
// "reqsn" => "112094120001088317", |
|||
// "producp" => "JX0002", |
|||
// "transtype" => "JSP501", |
|||
// "paytype" => "W01", |
|||
// "randomstr" => md5( uniqid() ), |
|||
// "body" => "", |
|||
// "remark" => "", |
|||
// "validtime" => "", |
|||
// "acct" => "", |
|||
// "notify_url" => "", |
|||
// "limit_pay" => "", |
|||
// "sub_appid" => "", |
|||
// "goods_tag" => "", |
|||
// "benefitdetail" => "", |
|||
// "chnlstoreid" => "", |
|||
// "extendparams" => "", |
|||
// "cusip" => "", |
|||
// "front_url" => "", |
|||
// "subbranch" => "", |
|||
// "idno" => "", |
|||
// "truename" => "", |
|||
// "asinfo" => "", |
|||
// "fqnum" => "", |
|||
// "unpid" => "", |
|||
// "terminfo" => "", |
|||
// // 其他参数... |
|||
// ); |
|||
|
|||
// $trade_data = array( |
|||
// "appid"=>"00000051", |
|||
// "cusid"=>"990581007426001", |
|||
// "randomstr"=>"82712208", |
|||
// "signtype"=>"RSA", |
|||
// "trxid"=>"112094120001088317", |
|||
// "version"=>"11", |
|||
// ); |
|||
|
|||
// 生成签名 |
|||
$sign = $Base->Crypt->makeSign($trade_data); |
|||
if($sign === false) |
|||
{ |
|||
echo $Base->Crypt->err_msg; |
|||
die(); |
|||
} |
|||
echo "\r\n"; |
|||
echo "[sign]: {$sign}"; |
|||
// die(); |
|||
|
|||
// 加密交易报文 |
|||
$crypted_trade_data = $Base->Crypt->encryptTradeData($trade_data); |
|||
if($crypted_trade_data === false) |
|||
{ |
|||
echo $Base->Crypt->err_msg; |
|||
die(); |
|||
} |
|||
echo "\r\n"; |
|||
echo "[crypted message]: {$crypted_trade_data}"; |
|||
// die(); |
|||
|
|||
// 发送报文 |
|||
echo "\r\n"; |
|||
var_dump($api_url); |
|||
$response = $Base->Request->send($api_url, $crypted_trade_data, $sign); |
|||
if($response === false) |
|||
{ |
|||
echo $Base->Request->err_msg; |
|||
die(); |
|||
} |
|||
|
|||
echo "\r\n"; |
|||
|
|||
echo "[response message]: {$response}"; |
|||
|
|||
// 解析响应报文 |
|||
$response = json_decode($response, true); |
|||
$response_data_crypted = $response['data']; |
|||
$response_sign = $response['sign']; |
|||
echo "\r\n"; |
|||
echo "[response sign]: {$response_sign}"; |
|||
echo "\r\n"; |
|||
echo "[response data (ciphertext)]: {$response_data_crypted}"; |
|||
|
|||
// 解密响应密文 |
|||
$response_data = $Base->Crypt->decryptTradeData($response_data_crypted); |
|||
if($response_data === false) |
|||
{ |
|||
echo $Base->Crypt->err_msg; |
|||
die(); |
|||
} |
|||
echo "\r\n"; |
|||
echo "[response data(clear text)]: {$response_data}"; |
|||
// die(); |
|||
|
|||
?> |
|||
|
|||
|
|||
|
|||
|
|||
|
|||
<!DOCTYPE html> |
|||
<html> |
|||
<head> |
|||
<meta charset="utf-8"> |
|||
<title>统一扫码Demo</title> |
|||
<style> |
|||
body{ word-break: break-word; } |
|||
</style> |
|||
</head> |
|||
<body></body> |
|||
</html> |
|||
@ -0,0 +1,41 @@ |
|||
<?php |
|||
// 基础类 |
|||
class Base |
|||
{ |
|||
// 配置 |
|||
protected $config; |
|||
// 加解密类 |
|||
public $Crypt; |
|||
// 请求类 |
|||
public $Request; |
|||
|
|||
|
|||
/** |
|||
* 构造函数 |
|||
*/ |
|||
public function __construct() |
|||
{ |
|||
|
|||
// 读取配置 |
|||
$this->config = require_once('../config/config.php'); |
|||
// 实例化加解密类 |
|||
require_once('Crypt.php'); |
|||
$this->Crypt = new Crypt($this->config); |
|||
// 实例化请求类 |
|||
require_once('Request.php'); |
|||
$this->Request = new Request($this->config); |
|||
} |
|||
|
|||
|
|||
/** |
|||
* 获取配置信息 |
|||
* @return array |
|||
*/ |
|||
public function getConfig() |
|||
{ |
|||
return $this->config; |
|||
} |
|||
|
|||
|
|||
} |
|||
?> |
|||
@ -0,0 +1,220 @@ |
|||
<?php |
|||
class Crypt |
|||
{ |
|||
// 配置 |
|||
public $config; |
|||
// 错误信息 |
|||
public $err_msg = ''; |
|||
|
|||
|
|||
/** |
|||
* 构造函数 |
|||
*/ |
|||
public function __construct(array $config) |
|||
{ |
|||
$this->config = $config; |
|||
} |
|||
|
|||
|
|||
/** |
|||
* 数据校验 |
|||
* @param array $data 待校验数据 |
|||
* @return bool |
|||
*/ |
|||
private function checkData(array $data) |
|||
{ |
|||
// 非空校验 |
|||
if( count($data) == 0 ) |
|||
{ |
|||
$this->err_msg = '待签名数据为空'; |
|||
return false; |
|||
} |
|||
// 一维数组校验 |
|||
if( count($data) != count($data, 1) ) |
|||
{ |
|||
$this->err_msg = '待签名数据不能是多维数组'; |
|||
return false; |
|||
} |
|||
|
|||
return true; |
|||
} |
|||
|
|||
|
|||
/** |
|||
* 生成签名 |
|||
* @param array $data 待签名数据 |
|||
* @return bool|string |
|||
*/ |
|||
public function makeSign(array $data) |
|||
{ |
|||
// 数据校验 |
|||
$check_data = $this->checkData($data); |
|||
if($check_data === false) |
|||
{ |
|||
return false; |
|||
} |
|||
|
|||
// 升序排列 |
|||
ksort($data); |
|||
// 转字符串 |
|||
$pre_sign_str = http_build_query($data); |
|||
// echo "[pre_sign_str(jiao yi message)]: {$pre_sign_str}"; |
|||
// die(); |
|||
|
|||
// 读取私钥 |
|||
$private_key = file_get_contents($this->config['private_key']); |
|||
$private_key = openssl_pkey_get_private($private_key); |
|||
|
|||
// 私钥签名 |
|||
$res = openssl_sign($pre_sign_str, $sign, $private_key); |
|||
if($res === false) |
|||
{ |
|||
$this->err_msg = openssl_error_string(); |
|||
return false; |
|||
} |
|||
// 签名转Base64 |
|||
$sign = base64_encode($sign); |
|||
|
|||
return $sign; |
|||
} |
|||
|
|||
|
|||
/** |
|||
* 校验签名 |
|||
* @param array $data 待校验数据 |
|||
* @param string $sign 签名 |
|||
* @return bool |
|||
*/ |
|||
public function checkSign(array $data, string $sign) |
|||
{ |
|||
// 数据校验 |
|||
$check_data = $this->checkData($data); |
|||
if($check_data === false) |
|||
{ |
|||
return false; |
|||
} |
|||
|
|||
// 签名转二进制 |
|||
$sign = base64_decode($sign); |
|||
|
|||
// 升序排列 |
|||
ksort($data); |
|||
// 转字符串 |
|||
$pre_sign_str = http_build_query($data); |
|||
$pre_sign_str = urldecode($pre_sign_str); |
|||
// echo "[pre sign str]: {$pre_sign_str}"; |
|||
// die(); |
|||
|
|||
// 读取公钥 |
|||
$public_key = file_get_contents($this->config['public_key']); |
|||
$public_key = openssl_pkey_get_public($public_key); |
|||
|
|||
// 公钥验签 |
|||
$res = openssl_verify($pre_sign_str, $sign, $public_key); |
|||
if($res === false) |
|||
{ |
|||
$this->err_msg = openssl_error_string(); |
|||
return false; |
|||
} |
|||
|
|||
return $res; |
|||
} |
|||
|
|||
|
|||
/** |
|||
* 加密交易报文 |
|||
* @param array $data 待签名数据 |
|||
* @return bool|string |
|||
*/ |
|||
public function encryptTradeData(array $data) |
|||
{ |
|||
// 数据校验 |
|||
$check_data = $this->checkData($data); |
|||
if($check_data === false) |
|||
{ |
|||
return false; |
|||
} |
|||
|
|||
// 读取公钥 |
|||
$public_key = file_get_contents($this->config['public_key']); |
|||
$public_key = openssl_pkey_get_public($public_key); |
|||
// 公钥长度(bit) |
|||
$public_key_info = openssl_pkey_get_details($public_key); |
|||
$public_key_bit = $public_key_info['bits']; |
|||
// 数据长度(数据长度=密钥字节数-11,11为OPENSSL_PKCS1_PADDING的填充长度) |
|||
$data_length = $public_key_bit / 8 - 11; |
|||
|
|||
// 待加密数据转json |
|||
$pre_encrypt_data = json_encode($data, JSON_UNESCAPED_UNICODE|JSON_UNESCAPED_SLASHES); |
|||
// 按密钥长度拆分数据 |
|||
$pre_encrypt_data_arr = str_split($pre_encrypt_data, $data_length); |
|||
// 分段公钥加密 |
|||
$crypted_data = ''; |
|||
foreach($pre_encrypt_data_arr as $key=>$val) |
|||
{ |
|||
$res = openssl_public_encrypt($val, $crypted_data_tmp, $public_key, OPENSSL_PKCS1_PADDING); |
|||
if($res === false) |
|||
{ |
|||
$this->err_msg = openssl_error_string(); |
|||
return false; |
|||
} |
|||
$crypted_data .= $crypted_data_tmp; |
|||
} |
|||
// 加密结果转Base64 |
|||
$crypted_data = base64_encode($crypted_data); |
|||
|
|||
return $crypted_data; |
|||
} |
|||
|
|||
|
|||
/** |
|||
* 解密交易报文 |
|||
* @param string $data 加密的交易报文 |
|||
* @return bool|array |
|||
*/ |
|||
public function decryptTradeData($data) |
|||
{ |
|||
// 加密数据转二进制 |
|||
$pre_decrypt_data = base64_decode($data); |
|||
|
|||
// 读取私钥 |
|||
$private_key = file_get_contents($this->config['private_key']); |
|||
$private_key = openssl_pkey_get_private($private_key); |
|||
// 私钥长度(bit) |
|||
$private_key_info = openssl_pkey_get_details($private_key); |
|||
$private_key_bit = $private_key_info['bits']; |
|||
// 数据长度(数据长度=密钥字节数,解密时无需考虑OPENSSL_PKCS1_PADDING的填充占位) |
|||
$data_length = $private_key_bit / 8; |
|||
|
|||
// 按密钥长度拆分数据 |
|||
$pre_decrypt_data_arr = str_split($pre_decrypt_data, $data_length); |
|||
// 分段私钥解密 |
|||
$decrypted_data = ''; |
|||
foreach($pre_decrypt_data_arr as $key=>$val) |
|||
{ |
|||
$res = openssl_private_decrypt($val, $decrypted_data_tmp, $private_key, OPENSSL_PKCS1_PADDING); |
|||
if($res === false) |
|||
{ |
|||
$this->err_msg = openssl_error_string(); |
|||
return false; |
|||
} |
|||
$decrypted_data .= $decrypted_data_tmp; |
|||
} |
|||
// 解密结果编码转换(JAVA处理后的中文一般为GBK编码) |
|||
// echo "\r\n"; |
|||
// var_dump($decrypted_data); |
|||
$charset = mb_detect_encoding($decrypted_data); // 这个东西有点问题 解析出来是false 先不用了 |
|||
// echo "\r\n"; |
|||
// var_dump($charset); |
|||
// echo "\r\n"; |
|||
|
|||
$decrypted_data = mb_convert_encoding($decrypted_data, 'UTF-8', 'GBK'); |
|||
// echo "\r\n"; |
|||
// var_dump($decrypted_data); |
|||
return $decrypted_data; |
|||
} |
|||
|
|||
|
|||
} |
|||
|
|||
?> |
|||
@ -0,0 +1,156 @@ |
|||
<?php |
|||
class Request |
|||
{ |
|||
// 配置 |
|||
public $config; |
|||
// 错误信息 |
|||
public $err_msg = ''; |
|||
|
|||
|
|||
/** |
|||
* 构造函数 |
|||
*/ |
|||
public function __construct(array $config) |
|||
{ |
|||
$this->config = $config; |
|||
} |
|||
|
|||
|
|||
/** |
|||
* 发送curl请求 |
|||
* @param string $url 要请求的URL |
|||
* @param string $method 请求方法[get|post] |
|||
* @param string|array $post_data 请求数据,模拟表单提交时为Array,提交JSON数据时为String |
|||
* @param string $data_type 数据类型[form|file|json|xml] |
|||
*/ |
|||
private function curlRequest($url, $method, $post_data='', $data_type='form') |
|||
{ |
|||
$ch = curl_init(); |
|||
curl_setopt($ch, CURLOPT_URL, $url); |
|||
// 请求端口,SSL为443 |
|||
curl_setopt($ch, CURLOPT_PORT, 443); |
|||
// 为项目内调用设置专用的UA |
|||
curl_setopt($ch, CURLOPT_USERAGENT, 'Intract cURL'); |
|||
// 设置为不自动输出 |
|||
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); |
|||
// 不输出头部 |
|||
curl_setopt($ch, CURLOPT_HEADER, false); |
|||
// 跳过证书检查 |
|||
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false); |
|||
|
|||
// 不从证书中检查SSL加密算法是否存在 |
|||
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false); |
|||
// // 设定SSL版本,1-3切换 |
|||
// curl_setopt($ch, CURLOPT_SSLVERSION, 1); |
|||
|
|||
// 根据$data_type,选择不同的HTTP Header |
|||
$data_type = strtolower($data_type); |
|||
// echo curl_errno($ch); |
|||
switch($data_type) |
|||
{ |
|||
case 'form': |
|||
$header = 'application/x-www-form-urlencoded'; |
|||
break; |
|||
case 'file': |
|||
$header = 'multipart/form-data'; |
|||
break; |
|||
case 'json': |
|||
$header = 'application/json'; |
|||
break; |
|||
case 'xml': |
|||
$header = 'text/xml'; |
|||
break; |
|||
default: |
|||
// 默认为form |
|||
$header = 'application/x-www-form-urlencoded'; |
|||
} |
|||
$header = array( |
|||
'Content-type: ' . $header, |
|||
'Content-length: ' . strlen($post_data) |
|||
); |
|||
// 根据不同的提交方式,设置头部和数据 |
|||
$method = strtolower($method); |
|||
switch($method) |
|||
{ |
|||
case 'get': |
|||
break; |
|||
case 'post': |
|||
// 设置头部 |
|||
curl_setopt($ch, CURLOPT_HTTPHEADER, $header); |
|||
// 设置POST提交 |
|||
curl_setopt($ch, CURLOPT_POST, true); |
|||
// 设置POST数据 |
|||
curl_setopt($ch, CURLOPT_POSTFIELDS, $post_data); |
|||
break; |
|||
default: |
|||
; |
|||
} |
|||
// 执行请求 |
|||
$response = curl_exec($ch); |
|||
// 返回数据 |
|||
$res = array( |
|||
'status' => 0, |
|||
'err_msg' => '', |
|||
'http_code' => curl_getinfo($ch, CURLINFO_HTTP_CODE), |
|||
'data' => null |
|||
); |
|||
if($response === false) |
|||
{ |
|||
$res['err_msg'] = curl_error($ch); |
|||
} |
|||
else |
|||
{ |
|||
$res['status'] = 1; |
|||
$res['data'] = $response; |
|||
} |
|||
// 关闭连接 |
|||
curl_close($ch); |
|||
|
|||
return $res; |
|||
} |
|||
|
|||
|
|||
/** |
|||
* 发送报文 |
|||
* @param string $url 接口URL |
|||
* @param string $data 加密后的数据 |
|||
* @param string $sign 交易报文签名 |
|||
* @return bool|string 成功时返回JSON字符串 |
|||
*/ |
|||
public function send($url, $data, $sign) |
|||
{ |
|||
// var_dump(gettype($url)); |
|||
// echo "\r\n"; |
|||
// if (!($url instanceof string)) |
|||
// { |
|||
// var_dump("url is not a string"); |
|||
// return false; |
|||
// } |
|||
// 组装通用参数 |
|||
$post_data = array( |
|||
'data' => $data, |
|||
// 'data' => urlencode($data), |
|||
'orgid' => $this->config['org_num'], |
|||
'signtype' => $this->config['signtype'], |
|||
'sign' => $sign, |
|||
// 'sign' => urlencode($sign), |
|||
); |
|||
$post_data = json_encode($post_data); |
|||
// echo "\r\n"; |
|||
// echo "[send message]: {$post_data}"; |
|||
|
|||
// 发送请求 |
|||
$send_res = $this->curlRequest($url, 'post', $post_data, 'json'); |
|||
if($send_res['status'] !== 1) |
|||
{ |
|||
$this->err_msg = $send_res['err_msg'] . '(HTTP Code: ' . $send_res['http_code'] . ')'; |
|||
return false; |
|||
} |
|||
|
|||
return $send_res['data']; |
|||
} |
|||
|
|||
|
|||
} |
|||
|
|||
?> |
|||
Loading…
Reference in new issue