<?php
namespace common\lakala;
class Lakala {
private $appid;
private $mchSerialNo;
private $mercId;
private $merchantPrivateKeyPath;
private $lklCertificatePath;
private $subject;
private $schema = 'LKLAPI-SHA256withRSA';
private $version = '3.0';
private $outOrderNo;
private $amount;
private $notifyUrl;
private $uoUrl = 'https://test.wsmsd.cn/sit/api/v3/ccss/counter/order/create';
/**
* 设置APPId
*/
public function setAppid($appid) {
$this->appid = $appid;
}
/**
* 设置mchSerialNo
*/
public function setMchSerialNo($mchSerialNo) {
$this->mchSerialNo = $mchSerialNo;
}
/**
* 设置mercId
*/
public function setMercId($mercId) {
$this->mercId = $mercId;
}
/**
* 设置merchantPrivateKeyPath
*/
public function setMerchantPrivateKeyPath($merchantPrivateKeyPath) {
$this->merchantPrivateKeyPath = $merchantPrivateKeyPath;
}
/**
* 设置lklCertificatePath
*/
public function setLklCertificatePath($lklCertificatePath) {
$this->lklCertificatePath = $lklCertificatePath;
}
/**
* 设置out_order_no
*/
public function setOutOrderNo($outOrderNo) {
$this->outOrderNo = $outOrderNo;
}
/**
* 设置subject
*/
public function setSubject($subject) {
$this->subject = $subject;
}
/**
* 设置amount
*/
public function setAmount($amount) {
$this->amount = $amount;
}
/**
* 设置notifyUrl
*/
public function setNotifyUrl($notifyUrl) {
$this->notifyUrl = $notifyUrl;
}
public function pay() {
$params = [
'out_order_no' => $this->outOrderNo,
'merchant_no' => $this->mercId,
'total_amount' => $this->amount,
'order_efficient_time' => date('YmdHis'),
'notify_url' => $this->notifyUrl,
'support_refund' => 1,
'support_repeat_pay' => 1,
'order_info' => $this->subject,
'extend_info' => $this->subject,
'counter_param' => json_encode(['pay_mode' => 'WECHAT'])
];
$requestData = [
'req_data' => $params,
'version' => $this->version,
'req_time' => date('YmdHis'),
];
$body = json_encode($requestData, JSON_UNESCAPED_UNICODE);
$authorization = $this->getAuthorization($body);
return $this->post($this->uoUrl, $body, $authorization);
}
//验签
public function signatureVerification($authorization, $body) {
$authorization = str_replace($this->schema . " ", "", $authorization);
$authorization = str_replace(",","&", $authorization);
$authorization = str_replace("\"","", $authorization);
$authorization = $this->convertUrlQuery($authorization);
$authorization['signature'] = base64_decode($authorization['signature']);
$message = $authorization['timestamp'] . "\n" . $authorization['nonce_str'] . "\n" . $body . "\n";
$key = openssl_get_publickey(file_get_contents($this->lklCertificatePath));
$flag = openssl_verify($message, $authorization['signature'], $key, OPENSSL_ALGO_SHA256);
openssl_free_key($key);
if($flag) {
return true;
}
return false;
}
//签名
public function getAuthorization($body) {
$nonceStr = $this->getRandom(12);
$timestamp = time();
$message = $this->appid . "\n" . $this->mchSerialNo . "\n" . $timestamp . "\n" . $nonceStr . "\n" . $body . "\n";
$key = openssl_get_privatekey(file_get_contents($this->merchantPrivateKeyPath));
openssl_sign($message, $signature, $key, OPENSSL_ALGO_SHA256);
openssl_free_key($key);
return $this->schema . " appid=\"" . $this->appid . "\"," . "serial_no=\"" . $this->mchSerialNo . "\"," . "timestamp=\"" . $timestamp . "\"," . "nonce_str=\"" . $nonceStr . "\"," . "signature=\"" . base64_encode($signature) . "\"";
}
//请求
public function post($url, $data, $authorization) {
$headers = [
"Authorization: " . $authorization,
"Accept: application/json",
"Content-Type:application/json",
];
$ch = curl_init();
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);//设置HTTP头
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_POST, 1);
$res = curl_exec($ch);
curl_close($ch);
return json_decode($res, true);
}
//签名参数转数组
private function convertUrlQuery($query) {
$queryParts = explode('&', $query);
$params = array();
foreach ($queryParts as $param) {
$item = explode('=', $param);
$params[$item[0]] = $item[1];
}
if($params['signature']) {
$params['signature'] = substr($query, strrpos($query, 'signature=') + 10);
}
return $params;
}
/**
* 获取随机字符串
*/
public function getRandom($len)
{
$base = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789';
$count = strlen($base);
$random = '';
for ($i = 0; $i < $len; $i++) {
$random .= $base[rand(0, $count-1)];
}
return $random;
}
}
PHP对接拉卡拉支付
最新推荐文章于 2024-06-15 04:29:48 发布