shish 2 лет назад
Родитель
Сommit
0c23f7195d
1 измененных файлов с 52 добавлено и 39 удалено
  1. 52 39
      common/components/lakala.php

+ 52 - 39
common/components/lakala.php

@@ -1,10 +1,12 @@
 <?php
+
 namespace app\lakala\model;
 
 
 use app\vendor\log\Log;
 
-class Lakala {
+class Lakala
+{
 
     private $appid;
     private $mchSerialNo;
@@ -17,18 +19,20 @@ class Lakala {
     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 __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() {
+    public function wechat()
+    {
         $reqInfo = new LabsOrderPreOrderPayReq();
         $reqInfo->mercId = '822162070120015';
         $reqInfo->termNo = '47827142';
@@ -51,17 +55,18 @@ class Lakala {
         $baseRequestVO->timestamp = random(8);
         $baseRequestVO->ver = $this->version;
 
-        return json_encode($baseRequestVO, JSON_UNESCAPED_UNICODE);      
+        return json_encode($baseRequestVO, JSON_UNESCAPED_UNICODE);
     }
 
     //支付宝
-    public function aliPay($orderId, $amount) {
+    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 = $amount * 100;
+        $reqInfo->amount = bcmul($amount, 100);
         $reqInfo->spbillCreateIp = '127.0.0.1';
         $reqInfo->transType = '41';
         $reqInfo->orderId = $orderId;
@@ -86,12 +91,13 @@ class Lakala {
     }
 
     //验签
-    public function signatureVerification($authorization, $body) {
+    public function signatureVerification($authorization, $body)
+    {
         $authorization = str_replace($this->schema . " ", "", $authorization);
-        $authorization = str_replace(",","&", $authorization);
-        $authorization = str_replace("\"","", $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";
@@ -99,29 +105,31 @@ class Lakala {
         $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) {
+        if ($flag) {
             return true;
         }
         return false;
     }
 
-	//签名
-	public function getAuthorization($body) {
-		$nonceStr = random(8);
-     	$timestamp = time();
+    //签名
+    public function getAuthorization($body)
+    {
+        $nonceStr = random(8);
+        $timestamp = time();
 
-      	$message = $this->appid . "\n" . $this->mchSerialNo . "\n" . $timestamp . "\n" . $nonceStr . "\n" . $body . "\n";
+        $message = $this->appid . "\n" . $this->mchSerialNo . "\n" . $timestamp . "\n" . $nonceStr . "\n" . $body . "\n";
 
-		$key = openssl_get_privatekey(file_get_contents($this->merchantPrivateKeyPath));
+        $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) {
+    public function post($url, $data, $authorization)
+    {
 
         $headers = [
             "Authorization: " . $authorization,
@@ -143,31 +151,35 @@ class Lakala {
     }
 
     //签名参数转数组
-    private function convertUrlQuery($query) { 
-        $queryParts = explode('&', $query); 
-         
-        $params = array(); 
-        foreach ($queryParts as $param) { 
-            $item = explode('=', $param); 
-            $params[$item[0]] = $item[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']) {
+        if ($params['signature']) {
             $params['signature'] = substr($query, strrpos($query, 'signature=') + 10);
         }
-         
-        return $params; 
+
+        return $params;
     }
 
 
 }
 
-class BaseRequestVO {
+class BaseRequestVO
+{
     public $reqData;
     public $termExtInfo;
     public $timestamp;
     public $ver;
 }
-class LabsOrderPreOrderPayReq {
+
+class LabsOrderPreOrderPayReq
+{
     public $amount;
     public $appId;
     public $exterOrderSource;
@@ -181,7 +193,8 @@ class LabsOrderPreOrderPayReq {
     public $transType;
 }
 
-class TermExtInfo {
+class TermExtInfo
+{
     public $termLoc;
 }