123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230 |
- <?php
- namespace crmeb\services;
- class AlipayService
- {
- protected $appId;
- protected $notifyUrl;
- protected $charset;
-
- protected $rsaPrivateKey;
- protected $alipayPublicKey;
- protected $totalFee;
- protected $outTradeNo;
- protected $orderName;
- protected $passback_params = [];
- public function __construct()
- {
- $this->charset = 'utf-8';
- }
- public function setAppid($appid)
- {
- $this->appId = $appid;
- }
- public function setNotifyUrl($notifyUrl)
- {
- $this->notifyUrl = $notifyUrl;
- }
- public function setRsaPrivateKey($saPrivateKey)
- {
- $this->rsaPrivateKey = $saPrivateKey;
- }
- public function setAlipayPublicKey($alipayPublicKey)
- {
- $this->alipayPublicKey = $alipayPublicKey;
- }
- public function setTotalFee($payAmount)
- {
- $this->totalFee = $payAmount;
- }
- public function setOutTradeNo($outTradeNo)
- {
- $this->outTradeNo = $outTradeNo;
- }
- public function setOrderName($orderName)
- {
- $this->orderName = $orderName;
- }
- public function setPassbackParams($passback_params)
- {
- $this->passback_params = $passback_params;
- }
-
- public function getOrderStr()
- {
-
- $requestConfigs = array(
- 'out_trade_no' => $this->outTradeNo,
- 'total_amount' => $this->totalFee,
- 'subject' => $this->orderName,
- 'product_code' => 'QUICK_MSECURITY_PAY',
- 'timeout_express' => '15m',
- 'passback_params' => http_build_query($this->passback_params),
- );
- $commonConfigs = array(
-
- 'app_id' => $this->appId,
- 'method' => 'alipay.trade.app.pay',
- 'format' => 'JSON',
- 'charset' => $this->charset,
- 'sign_type' => 'RSA2',
- 'timestamp' => date('Y-m-d H:i:s'),
- 'version' => '1.0',
- 'notify_url' => $this->notifyUrl,
- 'biz_content' => json_encode($requestConfigs),
- );
- $commonConfigs["sign"] = $this->generateSign($commonConfigs, $commonConfigs['sign_type']);
- $result = $this->buildOrderStr($commonConfigs);
- return $result;
- }
- public function generateSign($params, $signType = "RSA")
- {
- return $this->sign($this->getSignContent($params), $signType);
- }
- protected function sign($data, $signType = "RSA")
- {
- $priKey = $this->rsaPrivateKey;
- $res = "-----BEGIN RSA PRIVATE KEY-----\n" .
- wordwrap($priKey, 64, "\n", true) .
- "\n-----END RSA PRIVATE KEY-----";
- ($res) or die('您使用的私钥格式错误,请检查RSA私钥配置');
- if ("RSA2" == $signType) {
- openssl_sign($data, $sign, $res, version_compare(PHP_VERSION, '5.4.0', '<') ? SHA256 : OPENSSL_ALGO_SHA256);
- } else {
- openssl_sign($data, $sign, $res);
- }
- $sign = base64_encode($sign);
- return $sign;
- }
-
- protected function checkEmpty($value)
- {
- if (!isset($value))
- return true;
- if ($value === null)
- return true;
- if (trim($value) === "")
- return true;
- return false;
- }
- public function getSignContent($params)
- {
- ksort($params);
- $stringToBeSigned = "";
- $i = 0;
- foreach ($params as $k => $v) {
- if (false === $this->checkEmpty($v) && "@" != substr($v, 0, 1)) {
-
- $v = $this->characet($v, $this->charset);
- if ($i == 0) {
- $stringToBeSigned .= "$k" . "=" . "$v";
- } else {
- $stringToBeSigned .= "&" . "$k" . "=" . "$v";
- }
- $i++;
- }
- }
- unset ($k, $v);
- return $stringToBeSigned;
- }
-
- function characet($data, $targetCharset)
- {
- if (!empty($data)) {
- $fileType = $this->charset;
- if (strcasecmp($fileType, $targetCharset) != 0) {
- $data = mb_convert_encoding($data, $targetCharset, $fileType);
-
- }
- }
- return $data;
- }
- public function buildOrderStr($data)
- {
- return http_build_query($data);
- }
-
- public function rsaCheck($params)
- {
- $sign = $params['sign'];
- $signType = $params['sign_type'];
- unset($params['sign_type']);
- unset($params['sign']);
- return $this->verify($this->getSignContent($params), $sign, $signType);
- }
- function verify($data, $sign, $signType = 'RSA')
- {
- $pubKey = $this->alipayPublicKey;
- $res = "-----BEGIN PUBLIC KEY-----\n" .
- wordwrap($pubKey, 64, "\n", true) .
- "\n-----END PUBLIC KEY-----";
- ($res) or die('支付宝RSA公钥错误。请检查公钥文件格式是否正确');
-
- if ("RSA2" == $signType) {
- $result = (bool)openssl_verify($data, base64_decode($sign), $res, version_compare(PHP_VERSION, '5.4.0', '<') ? SHA256 : OPENSSL_ALGO_SHA256);
- } else {
- $result = (bool)openssl_verify($data, base64_decode($sign), $res);
- }
- return $result;
- }
- }
- function isInAlipayClient()
- {
- if (strpos($_SERVER['HTTP_USER_AGENT'], 'AlipayClient') !== false) {
- return true;
- }
- return false;
- }
- function getCurrentUrl()
- {
- $scheme = $_SERVER['HTTPS'] == 'on' ? 'https://' : 'http://';
- $uri = $_SERVER['PHP_SELF'] . $_SERVER['QUERY_STRING'];
- if ($_SERVER['REQUEST_URI']) $uri = $_SERVER['REQUEST_URI'];
- $baseUrl = urlencode($scheme . $_SERVER['HTTP_HOST'] . $uri);
- return $baseUrl;
- }
|