[PHP] 流行教育公开课-支付宝支付 →→→→→进入此内容的聊天室

来自 , 2019-11-13, 写在 PHP, 查看 143 次.
URL http://www.code666.cn/view/a91af177
  1. <?php
  2. namespace app\index\common;
  3.  
  4. use Config;
  5. /**
  6.  * 支付的工具类
  7.  */
  8. class Alipay
  9. {
  10.     public function generateSign($params, $signType = "RSA") {
  11.         return $this->sign($this->getSignContent($params), $signType);
  12.     }
  13.  
  14.     protected function sign($data, $signType = "RSA") {
  15.         $priKey=Config::get('alipay.merchant_private_key');
  16.         $res = "-----BEGIN RSA PRIVATE KEY-----\n" .
  17.             wordwrap($priKey, 64, "\n", true) .
  18.             "\n-----END RSA PRIVATE KEY-----";
  19.         ($res) or die('您使用的私钥格式错误,请检查RSA私钥配置');
  20.         if ("RSA2" == $signType) {
  21.             openssl_sign($data, $sign, $res, version_compare(PHP_VERSION,'5.4.0', '<') ? SHA256 : OPENSSL_ALGO_SHA256); //OPENSSL_ALGO_SHA256是php5.4.8以上版本才支持
  22.         } else {
  23.             openssl_sign($data, $sign, $res);
  24.         }
  25.         $sign = base64_encode($sign);
  26.         return $sign;
  27.     }
  28.  
  29.     /**
  30.      * 校验$value是否非空
  31.      *  if not set ,return true;
  32.      *    if is null , return true;
  33.      **/
  34.     protected function checkEmpty($value) {
  35.         if (!isset($value))
  36.             return true;
  37.         if ($value === null)
  38.             return true;
  39.         if (trim($value) === "")
  40.             return true;
  41.         return false;
  42.     }
  43.  
  44.     public function getSignContent($params) {
  45.         ksort($params);
  46.         $stringToBeSigned = "";
  47.         $i = 0;
  48.         foreach ($params as $k => $v) {
  49.             if (false === $this->checkEmpty($v) && "@" != substr($v, 0, 1)) {
  50.                 // 转换成目标字符集
  51.                 $v = $this->characet($v, Config::get('alipay.charset'));
  52.                 if ($i == 0) {
  53.                     $stringToBeSigned .= "$k" . "=" . "$v";
  54.                 } else {
  55.                     $stringToBeSigned .= "&" . "$k" . "=" . "$v";
  56.                 }
  57.                 $i++;
  58.             }
  59.         }
  60.         unset ($k, $v);
  61.         return $stringToBeSigned;
  62.     }
  63.  
  64.     /**
  65.      * 转换字符集编码
  66.      * @param $data
  67.      * @param $targetCharset
  68.      * @return string
  69.      */
  70.     function characet($data, $targetCharset) {
  71.         if (!empty($data)) {
  72.             $fileType = Config::get('alipay.charset');
  73.             if (strcasecmp($fileType, $targetCharset) != 0) {
  74.                 $data = mb_convert_encoding($data, $targetCharset, $fileType);
  75.                 //$data = iconv($fileType, $targetCharset.'//IGNORE', $data);
  76.             }
  77.         }
  78.         return $data;
  79.     }
  80.  
  81.     public function curlPost($url = '', $postData = '', $options = array())
  82.     {
  83.         if (is_array($postData)) {
  84.             $postData = http_build_query($postData);
  85.         }
  86.         $ch = curl_init();
  87.         curl_setopt($ch, CURLOPT_URL, $url);
  88.         curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
  89.         curl_setopt($ch, CURLOPT_POST, 1);
  90.         curl_setopt($ch, CURLOPT_POSTFIELDS, $postData);
  91.         curl_setopt($ch, CURLOPT_TIMEOUT, 30); //设置cURL允许执行的最长秒数
  92.         if (!empty($options)) {
  93.             curl_setopt_array($ch, $options);
  94.         }
  95.         //https请求 不验证证书和host
  96.         curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
  97.         curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
  98.         $data = curl_exec($ch);
  99.         curl_close($ch);
  100.         return $data;
  101.     }
  102. }
  103.  

回复 "流行教育公开课-支付宝支付"

这儿你可以回复上面这条便签

captcha