PHP實現微信退款的分析與原始碼實現

2021-09-08 20:23:31 字數 3518 閱讀 3311

原文:

需要注意的事項:

* 4.錯誤碼參照 :

**如下:

<?php/**

* 2.錯誤碼參照 : */

header('content-type:text/html; charset=utf-8');

$mchid = '***xx'; //

$apikey = '***xx'; //

帳戶設定-安全設定-api安全-api金鑰-設定api金鑰

$orderno = ''; //

$wxorderno = ''; //

$totalfee = 0.01; //

訂單金額,單位:元

$refundfee = 0.01; //

退款金額,單位:元

$refundno = 'refund_'.uniqid(); //

退款訂單號(可隨機生成)

);$result = $wxpay->dorefund($totalfee, $refundfee, $refundno, $wxorderno,$orderno

);if($result===true

)echo 'refund fail';

?>

wxpay.php

<?php

class

wxpayservice

/*** 退款

* @param float $totalfee 訂單金額 單位元

* @param float $refundfee 退款金額 單位元

* @param string $refundno 退款單號

* @param string $orderno 商戶訂單號

* @return string

*/public

function dorefund($totalfee, $refundfee, $refundno, $wxorderno='',$orderno='')

if ($unifiedorder->return_code != 'success')

if ($unifiedorder->result_code != 'success')

return

true

; }

public

static

function curlget($url = '', $options = array

())

//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

; }

public

function curlpost($url = '', $postdata = '', $options = array

())

$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

))

//https請求 不驗證證書和host

curl_setopt($ch, curlopt_ssl_verifypeer, false

); curl_setopt(

$ch, curlopt_ssl_verifyhost, false

);

//第一種方法,cert 與 key 分別屬於兩個.pem檔案

//預設格式為pem,可以注釋

curl_setopt($ch,curlopt_sslcerttype,'pem');

curl_setopt(

$ch,curlopt_sslcert,getcwd().'/cert/apiclient_cert.pem');

//預設格式為pem,可以注釋

curl_setopt($ch,curlopt_sslkeytype,'pem');

curl_setopt(

$ch,curlopt_sslkey,getcwd().'/cert/apiclient_key.pem');

//第二種方式,兩個檔案合成乙個.pem檔案

// curl_setopt($ch,curlopt_sslcert,getcwd().'/all.pem');

$data = curl_exec($ch

); curl_close(

$ch);

return

$data

; }

public

static

function createnoncestr($length = 16)

return

$str

; }

public

static

function arraytoxml($arr

)

else

$xml .= "<" . $key . ">$val . "]]>$key . ">";

}$xml .= "";

return

$xml

; }

public

static

function getsign($params, $key

)

protected

static

function formatqueryparamap($paramap, $urlencode = false

)

$buff .= $k . "=" . $v . "&";}}

$reqpar = '';

if (strlen($buff) > 0)

return

$reqpar

; }

}?>

php 實現微信退款

要是在測試的時候,網頁提示 curl 58 說明 證書的路徑出現問題 這裡要填物理路徑,也就是絕對路徑 網頁提示curl 52 說明你的證書引入少了,在官方的demo上只有兩個證書 apiclient cert.pem和 apiclient key.pem 你還需要引入乙個證書 rootca.pem...

PHP實現微信退款的分析與原始碼實現

原文 需要注意的事項 4.錯誤碼參照 如下 2.錯誤碼參照 header content type text html charset utf 8 mchid xx apikey xx 帳戶設定 安全設定 api安全 api金鑰 設定api金鑰 orderno wxorderno totalfee ...

PHP實現微信申請退款

申請退款,wxpayrefund中out trade no transaction id至少填乙個且 out refund no total fee refund fee op user id為必填引數 param wxpayrefund inputobj param int timeout thr...