close.php,close.php ╰半夏微凉° 2023-01-15 08:02 138阅读 0赞 /\*\* \* alipay.trade.close(统一收单交易关闭接口) \* \* 用于交易创建后,用户在一定时间内未进行支付,可调用该接口直接将未付款的交易进行关闭。 \*/ header('Content-type:text/html; Charset=utf-8'); /\*\*\* 请填写以下配置信息 \*\*\*/ $appid = 'xxxxx'; //https://open.alipay.com 账户中心->密钥管理->开放平台密钥,填写添加了电脑网站支付的应用的APPID $tradeNo = ''; //在支付宝系统中的交易流水号。最短 16 位,最长 64 位。和out\_trade\_no不能同时为空,如果同时传了 out\_trade\_no和 trade\_no,则以 trade\_no为准。 $outTradeNo = ''; //订单支付时传入的商户订单号,和支付宝交易号不能同时为空。 trade\_no,out\_trade\_no如果同时存在优先取trade\_no $signType = 'RSA2'; //签名算法类型,支持RSA2和RSA,推荐使用RSA2 //商户私钥,填写对应签名算法类型的私钥,如何生成密钥参考:https://docs.open.alipay.com/291/105971和https://docs.open.alipay.com/200/105310 $rsaPrivateKey=''; /\*\*\* 配置结束 \*\*\*/ $aliPay = new AlipayService(); $aliPay->setAppid($appid); $aliPay->setRsaPrivateKey($rsaPrivateKey); $aliPay->setTradeNo($tradeNo); $aliPay->setOutTradeNo($outTradeNo); $result = $aliPay->doClose(); $result = $result\['alipay\_trade\_close\_response'\]; if($result\['code'\] && $result\['code'\]=='10000')\{ echo ' # 订单已关闭 # '; \}else\{ echo $result\['msg'\].' : '.$result\['sub\_msg'\]; \} class AlipayService \{ protected $appId; protected $returnUrl; protected $notifyUrl; protected $charset; //私钥值 protected $rsaPrivateKey; protected $outTradeNo; protected $tradeNo; public function \_\_construct() \{ $this->charset = 'utf8'; \} public function setAppid($appid) \{ $this->appId = $appid; \} public function setRsaPrivateKey($saPrivateKey) \{ $this->rsaPrivateKey = $saPrivateKey; \} public function setOutTradeNo($outTradeNo) \{ $this->outTradeNo = $outTradeNo; \} public function settradeNo($tradeNo) \{ $this->tradeNo = $tradeNo; \} /\*\* \* 关闭订单 \* @return array \*/ public function doClose() \{ //请求参数 $requestConfigs = array( 'trade\_no'=>$this->tradeNo, 'out\_trade\_no'=>$this->outTradeNo, ); $commonConfigs = array( //公共参数 'app\_id' => $this->appId, 'method' => 'alipay.trade.close', //接口名称 'format' => 'JSON', 'charset'=>$this->charset, 'sign\_type'=>'RSA2', 'timestamp'=>date('Y-m-d H:i:s'), 'version'=>'1.0', 'biz\_content'=>json\_encode($requestConfigs), ); $commonConfigs\["sign"\] = $this->generateSign($commonConfigs, $commonConfigs\['sign\_type'\]); $result = $this->curlPost('https://openapi.alipay.com/gateway.do',$commonConfigs); $resultArr = json\_decode($result,true); if(empty($resultArr))\{ $result = iconv('GBK','UTF-8//IGNORE',$result); return json\_decode($result,true); \} return $resultArr; \} 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', ' \} else \{ openssl\_sign($data, $sign, $res); \} $sign = base64\_encode($sign); return $sign; \} /\*\* \* 校验$value是否非空 \* if not set ,return true; \* if is null , return true; \*\*/ 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; \} /\*\* \* 转换字符集编码 \* @param $data \* @param $targetCharset \* @return string \*/ function characet($data, $targetCharset) \{ if (!empty($data)) \{ $fileType = $this->charset; if (strcasecmp($fileType, $targetCharset) != 0) \{ $data = mb\_convert\_encoding($data, $targetCharset, $fileType); //$data = iconv($fileType, $targetCharset.'//IGNORE', $data); \} \} return $data; \} public function curlPost($url = '', $postData = '', $options = array()) \{ if (is\_array($postData)) \{ $postData = http\_build\_query($postData); \} $ch = curl\_init(); curl\_setopt($ch, CURLOPT\_URL, $url); curl\_setopt($ch, CURLOPT\_RETURNTRANSFER, 1); curl\_setopt($ch, CURLOPT\_POST, 1); curl\_setopt($ch, CURLOPT\_POSTFIELDS, $postData); curl\_setopt($ch, CURLOPT\_TIMEOUT, 30); //设置cURL允许执行的最长秒数 if (!empty($options)) \{ curl\_setopt\_array($ch, $options); \} //https请求 不验证证书和host curl\_setopt($ch, CURLOPT\_SSL\_VERIFYPEER, false); curl\_setopt($ch, CURLOPT\_SSL\_VERIFYHOST, false); $data = curl\_exec($ch); curl\_close($ch); return $data; \} \} 一键复制 编辑 Web IDE 原始数据 按行查看 历史
还没有评论,来说两句吧...