ping++支付實現步驟
1. 微信支付實現
登錄ping++平臺,https://dashboard.pingxx.com/login
2.開通支付渠道
支付渠道->開通微信公衆號支付
開通後 就可以下載php sdk包 和client sdk包實現支付流程啦
3.設置微信支付目錄;
需要進入你們的微信公衆賬號,有一個支付設置點擊進去,設置你HTML頁面的所在目錄;
比如域名:https:www.zhaiguang.html/zhifu/ (http協議根據公司需求定義一般都是http)
以後所有的微信支付頁面就全部放進zhifu這個目錄即可;
設置你的項目域名在微信上的回調地址
4.下載php sdk包
下載地址:https://github.com/PingPlusPlus/pingpp-php
PHP 版本要求 5.3 及以上,你可以使用 Composer 或直接手動引入
5. 支付
設置ping++平臺上的apikey
設置 微信的 app_id app_secret
設置ping++在項目中的app_id
從服務端發起支付請求,獲取支付憑據
將獲得的支付憑據傳給 Client
接收 Webhooks 通知 可自行處理業務需求
6.獲取clinet sdk 添加ping++的js文件 下載地址:[email protected]:529834149/pingpp-js.git
<script src="../js/pingpp.js?wxhctime=0.1"></script>
//?後面的字符串可以刪除掉;我之前是爲了避免微信緩存;
7. 支付流程的環節步驟;
首先你需要ajax請求服務端給你返回一個 charge,這個charge也就是喚醒微信支付的憑證;(如果沒有讓你們的服務端給你返回)
拿到charger後進行ping++封裝函數的執行即可;
server
php響應ping++
<?php
public function getPingview(Request $request){
$code = $request->input('code','');
if(!isset($code) || empty($code) || $code == NULL){
$url = 'http://wallet.blogchina.com/demo/pingview'; //微信回調地址以及授權目錄
$code_url = \Pingpp\WxpubOAUth::createOauthUrlForCode('微信公衆平臺的app_id',$url,false); //響應微信網頁授權 獲取code
header("Location:".$code_url); die;
}
return view('default.zhifu.pingview',compact('code'));//因爲在微信網頁授權獲取的的code是可變的 所以可以分配到支付頁面
}
?>
關於 open_id(微信公衆號授權用戶唯一標識)先跳轉到微信獲取授權 code,地址由下方代碼生成,$app_id 是你的微信公衆號應用唯一標識,$redirect_url 是用戶確認授權後跳轉的地址,用來接收 code。
public function postCharge(Request $request){
$app_id = '微信公衆平臺的app_id';
$app_secret = "微信公衆平臺上的app_secret";
\Pingpp\Pingpp::setApiKey('ping++平臺上的apikey'); //可操作是test還是live
$code = $request->input('code');
$open_id = \Pingpp\WxpubOAUth::getOpenid($app_id, $app_secret, $code);
$data = \Pingpp\Charge::create(array(
'order_no' => date('YmdHis') . (microtime(true) % 1) * 1000 . mt_rand(0, 9999).uniqid(),//商戶訂單號,適配每個渠道對此參數的要求,必須在商戶系統內唯一
'amount' => $request->input('amount',8),//訂單總金額, 人民幣單位:分(如訂單總金額爲 1 元,此處請填 100)
'app' => array('id' => 'ping++平臺上的app_id'),//支付使用的 app 對象的 id
'channel' => $request->input('channel'),//支付使用的第三方支付渠道 https://www.pingxx.com/api#支付渠道屬性值
'currency' => 'cny',//三位 ISO 貨幣代碼,目前僅支持人民幣 cny 。
'client_ip' => \Request::ip(),//發起支付請求客戶端的 IPv4 地址,如: 127.0.0.1。
'subject' => $request->input('subject','subject'),//商品的標題,該參數最長爲 32 個 Unicode 字符,
'body' => $request->input('body','body'),//商品的描述信息,該參數最長爲 128 個 Unicode 字符
'extra' => [
'limit_pay'=>'no_credit',
'open_id' => $open_id,
])//特定渠道發起交易時需要的額外參數,以及部分渠道支付成功返回的額外參數
);
return $data;
}
Webhooks
//回調地址在ping++平臺上設置 操作信息https://www.pingxx.com/docs/webhooks/webhooks
public function postPingresult(Request $request){
\Pingpp\Pingpp::setApiKey('ping++平臺上的apikey');//可操作是test還是live
$raw_data = file_get_contents('php://input');//php://input可以讀取沒有處理過的POST數據。
$event = json_decode($raw_data, true);
if ($event['type'] == 'charge.succeeded') {
$charge = $event['data']['object'];
$data = \Pingpp\Charge::retrieve($charge['id']);//單條查詢 可根據業務自行處理
$r = \Pingpp\Charge::all(array('limit' => 5, 'app' => array('id' => $charge['app'])));//多個查詢 可根據業務自行處理
\Log::info($r);
// ...
http_response_code(200); // PHP 5.4 or greater
} elseif ($event['type'] == 'refund.succeeded') {
$refund = $event['data']['object'];
// ...
http_response_code(200); // PHP 5.4 or greater
} else {
/**
* 其它類型 ...
* - summary.daily.available
* - summary.weekly.available
* - summary.monthly.available
* - transfer.succeeded
* - red_envelope.sent
* - red_envelope.received
* ...
*/
http_response_code(200);
// 異常時返回非 2xx 的返回碼
// http_response_code(400);
}
}
client
<script src="。/js/jquery.min.js"></script>
<script src="。/js/pingpp.js" type="text/javascript"></script> //引入client端的sdk包上的pingpp.js
function wap_pay(channel) {
var allowajax = true;
var amount = $.trim($('#amount').val());
if(allowajax){
allowajax = false;
$.ajax({
type:'post',
url:'http://xxxx.com/demo/charge',
dataType:"json",
data:{amount:amount,channel:channel,subject:'測試標題',body:'測試摘要',code:$.trim($('#code').val())},
success:function(msg){
pingpp.createPayment(msg, function(result, err) { //調起微信支付控件 進行支付
if (result=="success") {
// payment succeeded支付成功後的回調函數
window.location.href='http://xxxx
.com/demo/pingview'+"?id="+10000*Math.random();//成功跳轉到指定地址
} else {
window.location.href='http://xxxx.com/demo/pingview'+"?id="+10000*Math.random();//失敗或關閉了支付控件 做對應處理
console.log(result+" "+err.msg+" "+err.extra);
}
});
},
error:function(){
console.log('請求是否關注信息失敗');
},
});
}
}