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文件 下载地址:git@github.com: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('请求是否关注信息失败');
},
});
}
}