PHP實(shí)現(xiàn)的支付寶支付,退款,回調(diào)函數(shù)
發(fā)布時(shí)間:2018-09-11 14:14:31
來(lái)源:
1.支付寶主函數(shù)
<?php
include('alipay/aop/AopClient.php');
include('alipay/aop/request/AlipayTradeAppPayRequest.php');
include('alipay/aop/request/AlipayTradeRefundRequest.php');
//支付寶appid (應(yīng)用標(biāo)識(shí))
define("ALIPAY_APPID", "");
//支付寶(支付寶私鑰)
define("PRIVATE_KEY", "");
//支付寶(支付寶公鑰)
define("PUBLIC_KEY", "");
class alipay{
/**
* 支付寶支付
*/
public function appAlipay(){
// 時(shí)間戳
$timeStamp = strval(time());
$totalFee = 1;
// 支付結(jié)果顯示文字
$serial_number = date("Ymdhis") . mt_rand(10, 99);
$aop = new \AopClient();
$bizcontent = json_encode([
'body'=>"測(cè)試", //商品名稱(chēng)
'subject'=>'測(cè)試', //主題
'out_trade_no'=> $serial_number, //此訂單號(hào)為商戶(hù)唯一訂單號(hào)
'total_amount'=> $totalFee, //保留兩位小數(shù)
'product_code'=>'QUICK_MSECURITY_PAY'
]);
//支付寶開(kāi)始
$aop->gatewayUrl = "https://openapi.alipay.com/gateway.do";
//實(shí)際上線(xiàn)app id需真實(shí)的
$aop->appId = ALIPAY_APPID;
$aop->rsaPrivateKey = PRIVATE_KEY;
$aop->format = "json";
$aop->charset = "UTF-8";
$aop->signType = "RSA2";
$aop->alipayrsaPublicKey = PUBLIC_KEY;
//實(shí)例化具體API對(duì)應(yīng)的request類(lèi),類(lèi)名稱(chēng)和接口名稱(chēng)對(duì)應(yīng),當(dāng)前調(diào)用接口名稱(chēng):alipay.trade.app.pay
$request = new \AlipayTradeAppPayRequest();
//支付寶回調(diào)
$notifyUrl="https://openapi.alipay.com";
$request->setNotifyUrl($notifyUrl);
$request->setBizContent($bizcontent);
//這里和普通的接口調(diào)用不同,使用的是sdkExecute
$response = $aop->sdkExecute($request);
$this->echoSuccess($response);
}
/**
* 支付寶支付回調(diào)
*
*/
public function alipayNotify() {
if(!empty($_POST)){
$serial=$_POST['out_trade_no'];
$wepay_serial = $_POST['trade_no'];
if (!empty($wepay_serial)) {
//這里可以寫(xiě)自己需求
echo "返回的參數(shù)out_trade_no:".$serial;
echo "返回的參數(shù)trade_no:".$wepay_serial;
}
}
echo 'success';
}
/**
* 支付寶退貨處理
* 退貨前提是必須支付成功
* @param type $orderId
* @return bool
*/
public function alipayRefund($out_trade_no,$wepay_serial,$totalFee) {
$aop = new AopClient ();
$aop->gatewayUrl = 'https://openapi.alipay.com/gateway.do';
$aop->appId = ALIPAY_APPID;
$aop->rsaPrivateKey = PRIVATE_KEY;
$aop->alipayrsaPublicKey=PUBLIC_KEY;
$aop->apiVersion = '1.0';
$aop->signType = 'RSA2';
$aop->postCharset='GBK';
$aop->format='json';
$request = new AlipayTradeRefundRequest ();
$bizcontent = json_encode([
'out_trade_no'=>$out_trade_no,
'trade_no'=> $wepay_serial,
'refund_amount'=> $totalFee,
'refund_reason'=>'正常退款'
]);
$request->setBizContent($bizcontent);
$result = $aop->execute($request);
$responseNode = str_replace(".", "_", $request->getApiMethodName()) . "_response";
$resultCode = $result->$responseNode->code;
echo $resultCode;
if(!empty($resultCode)&&$resultCode == 10000){
echo "退款成功";
} else {
echo "退款失敗";
}
}
/**
* 輸出成功JSON消息
*/
final public function echoSuccess($msg = 'success') {
$this->echoMsg(0, $msg);
exit();
}
/**
* 輸出JSON消息
* @param mixed $code
* @param mixed $msg
*/
final public function echoMsg($code, $msg = '', $options = JSON_UNESCAPED_UNICODE) {
return $this->echoJson(array(
'ret_code' => $code,
'ret_msg' => $msg
), $options);
}
/**
* 輸出JSON
* @param mixed $arr
*/
final public function echoJson($arr, $options = JSON_UNESCAPED_UNICODE) {
header('Content-Type: application/json; charset=utf-8');
if (strpos(PHP_VERSION, '5.3') > -1) {
// php 5.3-
echo json_encode($arr);
} else {
// php 5.4+
echo json_encode($arr, $options);
}
return true;
}
}
2.調(diào)用 生成支付,退款 接口
<?php
include('./alipay.php');
$alipay =new alipay();
$alipay->appAlipay(); //支付函數(shù)
$out_trade_no=""; //訂單號(hào)
$wepay_serial=""; //唯一訂單號(hào)
$totalFee="0.01"; //退款金額
$alipay->alipayRefund($out_trade_no, $wepay_serial, $totalFee);//退款
源碼下載
/uploadfile/2018/0911/20180911021601336.rar