創建充值:
public function createRechargeOrder()
{
$mobile = $this->request->post('mobile')?$this->request->post('mobile'):0;
$amount = $this->request->post('amount')?$this->request->post('amount'):0;//充值金額
$type = $this->request->post('type')?$this->request->post('type'):1;//充值類型
$subject = $this->request->post('subject')?$this->request->post('subject'):'';//訂單名
$body = $this->request->post('body')?$this->request->post('body'):'';//訂單描述
$channel = $this->request->post('channel')?$this->request->post('channel'):1;//充值渠道:1=支付寶,2=直充
$username = $this->request->post('nickname')?$this->request->post('nickname'):''; //用戶暱稱
if(!$mobile || !$amount || !$username) $this->error("參數錯誤!");
//TODO:: 正則判斷是否字段值是否爲金額
if($amount < 0.01 || $amount > 30000 || !preg_match('/^(0|[1-9]\d{0,4})(\.\d{1,2})?$/', $amount)) $this->error('訂單金額錯誤');
if($mobile=='18888888888') {
$amount=0.01;
}
$data['userid'] = 0;
$data['mobile'] = $mobile;
$data['amount'] = $amount;
$data['type'] = $type;
$data['subject'] = $subject;
$data['body'] = $body;
$data['create_time'] = time();
$data['channel'] = $channel;
$data['username'] = $username;
$data['trade_id'] = date("YmdHis").rand(10000,99999);
$orderCheck = Db::name('recharge_order')->where('trade_id', $data['trade_id'])->find();
if( $orderCheck ) $data['trade_id'] = date("YmdHis").rand(10000,99999);
$res = Db::name('recharge_order')->insert($data);
if(!$res) $this->error('創建訂單失敗,請重試!');
//支付寶相關
$appId = "";
$rsaPrivateKey = "";
$alipayrsaPublicKey = "";
$notifyUrl = "";//回調地址
require_once(VENDOR_PATH . "alipay-sdk-PHP-4.1.0/aop/AopClient.php");
require_once(VENDOR_PATH . "alipay-sdk-PHP-4.1.0/aop/request/AlipayTradeAppPayRequest.php");
$aop = new \AopClient();
$aop->gatewayUrl = 'https://openapi.alipay.com/gateway.do';
$aop->appId = $appId;//請填寫開發者私鑰去頭去尾去回車,一行字符串
$aop->rsaPrivateKey = $rsaPrivateKey;//請填寫支付寶公鑰,一行字符串
$aop->alipayrsaPublicKey = $alipayrsaPublicKey;
$aop->apiVersion = '1.0';
$aop->signType = 'RSA2';
$aop->postCharset = 'UTF-8';
$aop->format = 'json';
$request = new \AlipayTradeAppPayRequest();
//SDK已經封裝掉了公共參數,這裏只需要傳入業務參數
$bizcontent = "{\"body\":\"".$data['body']."\","
. "\"subject\": \"".$data['subject']."\","
. "\"out_trade_no\": \"".$data['trade_id']."\","
. "\"timeout_express\": \"2h\","
. "\"total_amount\": \"".$data['amount']."\","
. "\"product_code\":\"QUICK_MSECURITY_PAY\""
. "}";
$request->setNotifyUrl($notifyUrl);
$request->setBizContent($bizcontent);
//這裏和普通的接口調用不同,使用的是sdkExecute
$response = $aop->sdkExecute($request);
//htmlspecialchars是爲了輸出到頁面時防止被瀏覽器將關鍵參數html轉義,實際打印到日誌以及http傳輸不會有這個問題
$data['string'] = $response;//就是orderString 可以直接給客戶端請求,無需再做處理。
$this->success('', $data);
}
充值回調:
public function alipayNotify()
{
require_once(VENDOR_PATH . "alipay-sdk-PHP-4.1.0/aop/AopClient.php");
require_once(VENDOR_PATH . "alipay-sdk-PHP-4.1.0/aop/request/AlipayTradeAppPayRequest.php");
$aop = new \AopClient ();
$alipayrsaPublicKey = "";//支付寶公鑰
$aop->alipayrsaPublicKey = $alipayrsaPublicKey;
$aop->rsaCheckV1($_POST, null, "RSA2");
//記錄返回值
$alipaynotify = fopen("/www/public/alipaynotify.txt", "a");
fwrite($alipaynotify, "\r\n");
fwrite($alipaynotify, json_encode($_POST));
if ($_POST['trade_status'] == 'TRADE_SUCCESS') {
//TODO 業務處理
echo 'success';
} else {
echo 'fail';
}
fclose($alipaynotify);
}