lakala.php 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196
  1. <?php
  2. namespace app\lakala\model;
  3. use app\vendor\log\Log;
  4. class Lakala {
  5. private $appid;
  6. private $mchSerialNo;
  7. private $mercId;
  8. private $termNo;
  9. private $merchantPrivateKeyPath;
  10. private $lklCertificatePath;
  11. private $subject;
  12. private $schema = 'LKLAPI-SHA256withRSA';
  13. private $version = '1.0.0';
  14. private $exterOrderSource = 'OS10000071';
  15. public function __construct($params) {
  16. $this->appid = $params['appid'];
  17. $this->mchSerialNo = $params['mchSerialNo'];
  18. $this->mercId = $params['mercId'];
  19. $this->termNo = $params['termNo'];
  20. $this->merchantPrivateKeyPath = $params['merchantPrivateKeyPath'];
  21. $this->lklCertificatePath = $params['lklCertificatePath'];
  22. $this->subject = $params['subject'] ?? '奥式生活订单';
  23. }
  24. public function wechat() {
  25. $reqInfo = new LabsOrderPreOrderPayReq();
  26. $reqInfo->mercId = '822162070120015';
  27. $reqInfo->termNo = '47827142';
  28. $reqInfo->payMode = 'ALIPAY';
  29. $reqInfo->openId = 'olpr-0pHIp1AjaAr29LENlHi0cJ0';
  30. $reqInfo->amount = 1;
  31. $reqInfo->spbillCreateIp = '127.0.0.1';
  32. $reqInfo->transType = '41';
  33. $reqInfo->orderId = '2020072915yhl142212';
  34. $reqInfo->appId = 'wx9ef39b708f16694d';
  35. $reqInfo->subject = '奥式生活订单';
  36. $reqInfo->exterOrderSource = 'OS10000071';
  37. $termExtInfo = new TermExtInfo();
  38. $termExtInfo->termLoc = '';
  39. $baseRequestVO = new BaseRequestVO();
  40. $baseRequestVO->reqData = $reqInfo;
  41. $baseRequestVO->termExtInfo = $termExtInfo;
  42. $baseRequestVO->timestamp = random(8);
  43. $baseRequestVO->ver = $this->version;
  44. return json_encode($baseRequestVO, JSON_UNESCAPED_UNICODE);
  45. }
  46. //支付宝
  47. public function aliPay($orderId, $amount) {
  48. $reqInfo = new LabsOrderPreOrderPayReq();
  49. $reqInfo->mercId = $this->mercId;
  50. $reqInfo->termNo = $this->termNo;
  51. $reqInfo->payMode = 'ALIPAY';
  52. // $reqInfo->openId = 'olpr-0pHIp1AjaAr29LENlHi0cJ0';
  53. $reqInfo->amount = $amount * 100;
  54. $reqInfo->spbillCreateIp = '127.0.0.1';
  55. $reqInfo->transType = '41';
  56. $reqInfo->orderId = $orderId;
  57. // $reqInfo->appId = 'wx9ef39b708f16694d';
  58. $reqInfo->subject = $this->subject;
  59. $reqInfo->exterOrderSource = $this->exterOrderSource;
  60. $termExtInfo = new TermExtInfo();
  61. // $termExtInfo->termLoc = '+155621.316,-125622.12';
  62. $baseRequestVO = new BaseRequestVO();
  63. $baseRequestVO->reqData = $reqInfo;
  64. $baseRequestVO->termExtInfo = $termExtInfo;
  65. $baseRequestVO->timestamp = random(8);
  66. $baseRequestVO->ver = $this->version;
  67. $body = json_encode($baseRequestVO, JSON_UNESCAPED_UNICODE);
  68. $authorization = $this->getAuthorization($body);
  69. return $this->post(LKL_ORDERPAY_URL . '/labs_order_pre_orderpay', $body, $authorization);
  70. }
  71. //验签
  72. public function signatureVerification($authorization, $body) {
  73. $authorization = str_replace($this->schema . " ", "", $authorization);
  74. $authorization = str_replace(",","&", $authorization);
  75. $authorization = str_replace("\"","", $authorization);
  76. $authorization = $this->convertUrlQuery($authorization);
  77. $authorization['signature'] = base64_decode($authorization['signature']);
  78. $message = $authorization['timestamp'] . "\n" . $authorization['nonce_str'] . "\n" . $body . "\n";
  79. $key = openssl_get_publickey(file_get_contents($this->lklCertificatePath));
  80. $flag = openssl_verify($message, $authorization['signature'], $key, OPENSSL_ALGO_SHA256);
  81. openssl_free_key($key);
  82. if($flag) {
  83. return true;
  84. }
  85. return false;
  86. }
  87. //签名
  88. public function getAuthorization($body) {
  89. $nonceStr = random(8);
  90. $timestamp = time();
  91. $message = $this->appid . "\n" . $this->mchSerialNo . "\n" . $timestamp . "\n" . $nonceStr . "\n" . $body . "\n";
  92. $key = openssl_get_privatekey(file_get_contents($this->merchantPrivateKeyPath));
  93. openssl_sign($message, $signature, $key, OPENSSL_ALGO_SHA256);
  94. openssl_free_key($key);
  95. return $this->schema . " appid=\"" . $this->appid . "\"," . "serial_no=\"" . $this->mchSerialNo . "\"," . "timestamp=\"" . $timestamp . "\"," . "nonce_str=\"" . $nonceStr . "\"," . "signature=\"" . base64_encode($signature) . "\"";
  96. }
  97. //请求
  98. public function post($url, $data, $authorization) {
  99. $headers = [
  100. "Authorization: " . $authorization,
  101. "Accept: application/json",
  102. "Content-Type:application/json",
  103. ];
  104. $ch = curl_init();
  105. curl_setopt($ch, CURLOPT_POST, 1);
  106. curl_setopt($ch, CURLOPT_URL, $url);
  107. curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
  108. curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);//设置HTTP头
  109. curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
  110. curl_setopt($ch, CURLOPT_POST, 1);
  111. $res = curl_exec($ch);
  112. curl_close($ch);
  113. return json_decode($res, 1);
  114. }
  115. //签名参数转数组
  116. private function convertUrlQuery($query) {
  117. $queryParts = explode('&', $query);
  118. $params = array();
  119. foreach ($queryParts as $param) {
  120. $item = explode('=', $param);
  121. $params[$item[0]] = $item[1];
  122. }
  123. if($params['signature']) {
  124. $params['signature'] = substr($query, strrpos($query, 'signature=') + 10);
  125. }
  126. return $params;
  127. }
  128. }
  129. class BaseRequestVO {
  130. public $reqData;
  131. public $termExtInfo;
  132. public $timestamp;
  133. public $ver;
  134. }
  135. class LabsOrderPreOrderPayReq {
  136. public $amount;
  137. public $appId;
  138. public $exterOrderSource;
  139. public $mercId;
  140. public $openId;
  141. public $orderId;
  142. public $payMode;
  143. public $spbillCreateIp;
  144. public $subject;
  145. public $termNo;
  146. public $transType;
  147. }
  148. class TermExtInfo {
  149. public $termLoc;
  150. }