| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209 |
- <?php
- namespace app\lakala\model;
- use app\vendor\log\Log;
- class Lakala
- {
- private $appid;
- private $mchSerialNo;
- private $mercId;
- private $termNo;
- private $merchantPrivateKeyPath;
- private $lklCertificatePath;
- private $subject;
- private $schema = 'LKLAPI-SHA256withRSA';
- private $version = '1.0.0';
- private $exterOrderSource = 'OS10000071';
- public function __construct($params)
- {
- $this->appid = $params['appid'];
- $this->mchSerialNo = $params['mchSerialNo'];
- $this->mercId = $params['mercId'];
- $this->termNo = $params['termNo'];
- $this->merchantPrivateKeyPath = $params['merchantPrivateKeyPath'];
- $this->lklCertificatePath = $params['lklCertificatePath'];
- $this->subject = $params['subject'] ?? '购买花材';
- }
- public function wechat()
- {
- $reqInfo = new LabsOrderPreOrderPayReq();
- $reqInfo->mercId = '822162070120015';
- $reqInfo->termNo = '47827142';
- $reqInfo->payMode = 'ALIPAY';
- $reqInfo->openId = 'olpr-0pHIp1AjaAr29LENlHi0cJ0';
- $reqInfo->amount = 1;
- $reqInfo->spbillCreateIp = '127.0.0.1';
- $reqInfo->transType = '41';
- $reqInfo->orderId = '2020072915yhl142212';
- $reqInfo->appId = 'wx9ef39b708f16694d';
- $reqInfo->subject = '奥式生活订单';
- $reqInfo->exterOrderSource = 'OS10000071';
- $termExtInfo = new TermExtInfo();
- $termExtInfo->termLoc = '';
- $baseRequestVO = new BaseRequestVO();
- $baseRequestVO->reqData = $reqInfo;
- $baseRequestVO->termExtInfo = $termExtInfo;
- $baseRequestVO->timestamp = random(8);
- $baseRequestVO->ver = $this->version;
- return json_encode($baseRequestVO, JSON_UNESCAPED_UNICODE);
- }
- //支付宝
- public function aliPay($orderId, $amount)
- {
- $reqInfo = new LabsOrderPreOrderPayReq();
- $reqInfo->mercId = $this->mercId;
- $reqInfo->termNo = $this->termNo;
- $reqInfo->payMode = 'ALIPAY';
- // $reqInfo->openId = 'olpr-0pHIp1AjaAr29LENlHi0cJ0';
- $reqInfo->amount = bcmul($amount, 100);
- $reqInfo->spbillCreateIp = '127.0.0.1';
- $reqInfo->transType = '41';
- $reqInfo->orderId = $orderId;
- // $reqInfo->appId = 'wx9ef39b708f16694d';
- $reqInfo->subject = $this->subject;
- $reqInfo->exterOrderSource = $this->exterOrderSource;
- $termExtInfo = new TermExtInfo();
- // $termExtInfo->termLoc = '+155621.316,-125622.12';
- $baseRequestVO = new BaseRequestVO();
- $baseRequestVO->reqData = $reqInfo;
- $baseRequestVO->termExtInfo = $termExtInfo;
- $baseRequestVO->timestamp = random(8);
- $baseRequestVO->ver = $this->version;
- $body = json_encode($baseRequestVO, JSON_UNESCAPED_UNICODE);
- $authorization = $this->getAuthorization($body);
- return $this->post(LKL_ORDERPAY_URL . '/labs_order_pre_orderpay', $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 = random(8);
- $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, 1);
- }
- //签名参数转数组
- 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;
- }
- }
- class BaseRequestVO
- {
- public $reqData;
- public $termExtInfo;
- public $timestamp;
- public $ver;
- }
- class LabsOrderPreOrderPayReq
- {
- public $amount;
- public $appId;
- public $exterOrderSource;
- public $mercId;
- public $openId;
- public $orderId;
- public $payMode;
- public $spbillCreateIp;
- public $subject;
- public $termNo;
- public $transType;
- }
- class TermExtInfo
- {
- public $termLoc;
- }
|