lakala.php 6.0 KB

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