您的位置:首页 > 编程语言 > PHP开发

Laravel 调用PING++

2017-09-26 17:35 92 查看
class PayController extends Controller
{
//支付
public function index(Request $request){
\Pingpp\Pingpp::setPrivateKeyPath(__DIR__ . '/your_rsa_private_key.pem');//私钥
$channel = (INT)(strtolower($request['payType']));//支付类型

//$extra 在使用某些渠道的时候,需要填入相应的参数,其它渠道则是 array() .具体见以下代码或者官网中的文档。其他渠道时可以传空值也可以不传。
$extra = array();
switch ($channel) {
case 1:
$channel = 'wx';
$ordertype = 0;
$extra = array(

);
//                \Pingpp\Pingpp::setPrivateKeyPath(__DIR__ . '/alipay.pem');
break;
case 2:
$channel = 'alipay';
$ordertype = 1;
$extra = array(

);
break;
case 3:
$channel = 'upacp';
$ordertype = 2;
$extra = array(
'result_url' => 'http://www.yourdomain.com/result?code='
);
break;

}
\Pingpp\Pingpp::setApiKey('平台的key');

DB::table('orders')->where('order_sn',$request['orderNo'])->update(['pay_type'=>$ordertype]);
try {
$ch = \Pingpp\Charge::create(
array(
'subject'   => '-'.$request['orderNo'],//交易的title
'body'      => '-'.$request['orderNo'],//交易的订单号
'amount'    =>$amount ,
'order_no'  => $request['orderNo'].rand(1000,9999),//防止订单号重复
'currency'  => 'cny',
'extra'     => $extra,
'channel'   => $channel,
'client_ip' => $_SERVER['REMOTE_ADDR'],
'app'       => array('id' => '平台appid')
)
);
return json_encode(array('code'=>0,'msg'=>'成功','data'=>json_decode($ch)));
} catch (\Pingpp\Error\Base $e) {
header('Status: ' . $e->getHttpStatus());
echo($e->getHttpBody());
}

}

//支付完事之后的事情
public function webhooks(Request $request){

$raw_data = file_get_contents('php://input');//这是在ping++设置的回调传回来的值
$headers = \Pingpp\Util\Util::getRequestHeaders();
$signature = isset($headers['X-Pingplusplus-Signature']) ? $headers['X-Pingplusplus-Signature'] : NULL;
$pub_key_path =__DIR__ . '/public_key.pem';//公钥
$result = $this -> verify_signature($raw_data, $signature, $pub_key_path);//验证身份
if ($result === 1) {
// 验证通过
$event = json_decode($raw_data, true);
if ($event['type'] == 'charge.succeeded') {
$charge = $event['data']['object'];
$order_sn = $charge['order_no'];//订单号
$return_num = $charge['id'];//付款成功后的回执
//更改订单状态
do something by myself

}
} elseif ($result === 0) {
http_response_code(400);
echo 'verification failed';
exit;
} else {
http_response_code(400);
echo 'verification error';
exit;
}
}

//验证安全
public function verify_signature($raw_data, $signature, $pub_key_path) {
$p
4000
ub_key_contents = file_get_contents($pub_key_path);
// php 5.4.8 以上,第四个参数可用常量 OPENSSL_ALGO_SHA256
return openssl_verify($raw_data, base64_decode($signature), $pub_key_contents, 'sha256');
}

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: