1. <?php
  2. header('Content-type:text/html; Charset=utf-8');
  3. /*** 请填写以下配置信息 ***/
  4. $appid = 'xxxxx'; //https://open.alipay.com 账户中心->密钥管理->开放平台密钥,填写添加了电脑网站支付的应用的APPID
  5. $notifyUrl = 'http://www.xxx.com/alipay/notify.php'; //付款成功后的异步回调地址
  6. $outTradeNo = uniqid(); //你自己的商品订单号,不能重复
  7. $payAmount = 0.01; //付款金额,单位:元
  8. $orderName = '支付测试'; //订单标题
  9. $signType = 'RSA2'; //签名算法类型,支持RSA2和RSA,推荐使用RSA2
  10. $rsaPrivateKey='xxxxx'; //商户私钥,填写对应签名算法类型的私钥,如何生成密钥参考:https://docs.open.alipay.com/291/105971和https://docs.open.alipay.com/200/105310
  11. /*** 配置结束 ***/
  12. $aliPay = new AlipayService();
  13. $aliPay->setAppid($appid);
  14. $aliPay->setNotifyUrl($notifyUrl);
  15. $aliPay->setRsaPrivateKey($rsaPrivateKey);
  16. $aliPay->setTotalFee($payAmount);
  17. $aliPay->setOutTradeNo($outTradeNo);
  18. $aliPay->setOrderName($orderName);
  19. $result = $aliPay->doPay();
  20. $result = $result['alipay_trade_precreate_response'];
  21. if($result['code'] && $result['code']=='10000'){
  22. //下面的URL生成二维码即可
  23. $url = $result['qr_code'];
  24. echo '二维码内容:'.$result['qr_code'];
  25. }else{
  26. echo $result['msg'].' : '.$result['sub_msg'];
  27. }
  28. class AlipayService
  29. {
  30. protected $appId;
  31. protected $notifyUrl;
  32. protected $charset;
  33. //私钥值
  34. protected $rsaPrivateKey;
  35. protected $totalFee;
  36. protected $outTradeNo;
  37. protected $orderName;
  38. public function __construct()
  39. {
  40. $this->charset = 'utf-8';
  41. }
  42. public function setAppid($appid)
  43. {
  44. $this->appId = $appid;
  45. }
  46. public function setNotifyUrl($notifyUrl)
  47. {
  48. $this->notifyUrl = $notifyUrl;
  49. }
  50. public function setRsaPrivateKey($saPrivateKey)
  51. {
  52. $this->rsaPrivateKey = $saPrivateKey;
  53. }
  54. public function setTotalFee($payAmount)
  55. {
  56. $this->totalFee = $payAmount;
  57. }
  58. public function setOutTradeNo($outTradeNo)
  59. {
  60. $this->outTradeNo = $outTradeNo;
  61. }
  62. public function setOrderName($orderName)
  63. {
  64. $this->orderName = $orderName;
  65. }
  66. /**
  67. * 发起订单
  68. * @return array
  69. */
  70. public function doPay()
  71. {
  72. //请求参数
  73. $requestConfigs = array(
  74. 'out_trade_no'=>$this->outTradeNo,
  75. 'total_amount'=>$this->totalFee, //单位 元
  76. 'subject'=>$this->orderName, //订单标题
  77. 'timeout_express'=>'2h' //该笔订单允许的最晚付款时间,逾期将关闭交易。取值范围:1m~15d。m-分钟,h-小时,d-天,1c-当天(1c-当天的情况下,无论交易何时创建,都在0点关闭)。 该参数数值不接受小数点, 如 1.5h,可转换为 90m。
  78. );
  79. $commonConfigs = array(
  80. //公共参数
  81. 'app_id' => $this->appId,
  82. 'method' => 'alipay.trade.precreate', //接口名称
  83. 'format' => 'JSON',
  84. 'charset'=>$this->charset,
  85. 'sign_type'=>'RSA2',
  86. 'timestamp'=>date('Y-m-d H:i:s'),
  87. 'version'=>'1.0',
  88. 'notify_url' => $this->notifyUrl,
  89. 'biz_content'=>json_encode($requestConfigs),
  90. );
  91. $commonConfigs["sign"] = $this->generateSign($commonConfigs, $commonConfigs['sign_type']);
  92. $result = $this->curlPost('https://openapi.alipay.com/gateway.do?charset='.$this->charset,$commonConfigs);
  93. return json_decode($result,true);
  94. }
  95. public function generateSign($params, $signType = "RSA") {
  96. return $this->sign($this->getSignContent($params), $signType);
  97. }
  98. protected function sign($data, $signType = "RSA") {
  99. $priKey=$this->rsaPrivateKey;
  100. $res = "-----BEGIN RSA PRIVATE KEY-----\n" .
  101. wordwrap($priKey, 64, "\n", true) .
  102. "\n-----END RSA PRIVATE KEY-----";
  103. ($res) or die('您使用的私钥格式错误,请检查RSA私钥配置');
  104. if ("RSA2" == $signType) {
  105. openssl_sign($data, $sign, $res, version_compare(PHP_VERSION,'5.4.0', '<') ? SHA256 : OPENSSL_ALGO_SHA256); //OPENSSL_ALGO_SHA256是php5.4.8以上版本才支持
  106. } else {
  107. openssl_sign($data, $sign, $res);
  108. }
  109. $sign = base64_encode($sign);
  110. return $sign;
  111. }
  112. /**
  113. * 校验$value是否非空
  114. * if not set ,return true;
  115. * if is null , return true;
  116. **/
  117. protected function checkEmpty($value) {
  118. if (!isset($value))
  119. return true;
  120. if ($value === null)
  121. return true;
  122. if (trim($value) === "")
  123. return true;
  124. return false;
  125. }
  126. public function getSignContent($params) {
  127. ksort($params);
  128. $stringToBeSigned = "";
  129. $i = 0;
  130. foreach ($params as $k => $v) {
  131. if (false === $this->checkEmpty($v) && "@" != substr($v, 0, 1)) {
  132. // 转换成目标字符集
  133. $v = $this->characet($v, $this->charset);
  134. if ($i == 0) {
  135. $stringToBeSigned .= "$k" . "=" . "$v";
  136. } else {
  137. $stringToBeSigned .= "&" . "$k" . "=" . "$v";
  138. }
  139. $i++;
  140. }
  141. }
  142. unset ($k, $v);
  143. return $stringToBeSigned;
  144. }
  145. /**
  146. * 转换字符集编码
  147. * @param $data
  148. * @param $targetCharset
  149. * @return string
  150. */
  151. function characet($data, $targetCharset) {
  152. if (!empty($data)) {
  153. $fileType = $this->charset;
  154. if (strcasecmp($fileType, $targetCharset) != 0) {
  155. $data = mb_convert_encoding($data, $targetCharset, $fileType);
  156. //$data = iconv($fileType, $targetCharset.'//IGNORE', $data);
  157. }
  158. }
  159. return $data;
  160. }
  161. public function curlPost($url = '', $postData = '', $options = array())
  162. {
  163. if (is_array($postData)) {
  164. $postData = http_build_query($postData);
  165. }
  166. $ch = curl_init();
  167. curl_setopt($ch, CURLOPT_URL, $url);
  168. curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
  169. curl_setopt($ch, CURLOPT_POST, 1);
  170. curl_setopt($ch, CURLOPT_POSTFIELDS, $postData);
  171. curl_setopt($ch, CURLOPT_TIMEOUT, 30); //设置cURL允许执行的最长秒数
  172. if (!empty($options)) {
  173. curl_setopt_array($ch, $options);
  174. }
  175. //https请求 不验证证书和host
  176. curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
  177. curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
  178. $data = curl_exec($ch);
  179. curl_close($ch);
  180. return $data;
  181. }
  182. }