first commit
This commit is contained in:
@@ -0,0 +1,117 @@
|
||||
<?php
|
||||
|
||||
class XunhupayClient
|
||||
{
|
||||
private $apiurl = 'https://api.xunhupay.com/payment/do.html';
|
||||
private $appid;
|
||||
private $appsecret;
|
||||
|
||||
function __construct($appid, $appsecret, $apiurl){
|
||||
$this->appid = $appid;
|
||||
$this->appsecret = $appsecret;
|
||||
if(!empty($apiurl)) {
|
||||
$this->apiurl = $apiurl;
|
||||
}
|
||||
}
|
||||
|
||||
//发起支付
|
||||
public function do_payment($params){
|
||||
return $this->execute($this->apiurl, $params);
|
||||
}
|
||||
|
||||
//查询订单
|
||||
public function query_payment($params){
|
||||
$url = str_replace('/payment/do.html', '/payment/query.html', $this->apiurl);
|
||||
return $this->execute($url, $params);
|
||||
}
|
||||
|
||||
//发起通用请求
|
||||
public function execute($url, $params){
|
||||
$publicParams = [
|
||||
'appid' => $this->appid,
|
||||
'time' => time(),
|
||||
'nonce_str' => str_shuffle(time())
|
||||
];
|
||||
$params = array_merge($publicParams, $params);
|
||||
$params['hash'] = $this->generate_hash($params, $this->appsecret);
|
||||
$response = $this->curl_post($url, json_encode($params));
|
||||
$result = json_decode($response, true);
|
||||
if(isset($result['errcode']) && $result['errcode']==0){
|
||||
$hash = $this->generate_hash($result, $this->appsecret);
|
||||
if(!isset($result['hash']) || $hash !== $hash){
|
||||
throw new \Exception('返回数据签名校验失败');
|
||||
}
|
||||
return $result;
|
||||
}else{
|
||||
throw new \Exception($result['errmsg']?$result['errmsg']:'返回数据解析失败');
|
||||
}
|
||||
}
|
||||
|
||||
//二维码图片链接解析二维码链接
|
||||
public function parseQrcode($url_qrcode){
|
||||
$redirect_url = $this->get_redirect_url($url_qrcode);
|
||||
if($redirect_url){
|
||||
$url = getSubstr($redirect_url, 'data=', '&');
|
||||
if($url){
|
||||
return base64_decode($url);
|
||||
}
|
||||
}
|
||||
throw new \Exception('获取二维码链接失败');
|
||||
}
|
||||
|
||||
public function verify($arr){
|
||||
if(!isset($arr['hash'])) return false;
|
||||
$hash = $this->generate_hash($arr, $this->appsecret);
|
||||
return $hash === $arr['hash'];
|
||||
}
|
||||
|
||||
private function curl_post($url, $post, $timeout = 10){
|
||||
$ch = curl_init($url);
|
||||
curl_setopt($ch, CURLOPT_TIMEOUT, $timeout);
|
||||
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
|
||||
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
|
||||
$httpheader[] = "Accept: */*";
|
||||
$httpheader[] = "Accept-Language: zh-CN,zh;q=0.8";
|
||||
$httpheader[] = "Connection: close";
|
||||
$httpheader[] = "Content-Type: application/json; charset=utf-8";
|
||||
curl_setopt($ch, CURLOPT_HTTPHEADER, $httpheader);
|
||||
curl_setopt($ch, CURLOPT_HEADER, false);
|
||||
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
|
||||
curl_setopt($ch, CURLOPT_POST, true);
|
||||
curl_setopt($ch, CURLOPT_POSTFIELDS, $post);
|
||||
$response = curl_exec($ch);
|
||||
curl_close($ch);
|
||||
return $response;
|
||||
}
|
||||
|
||||
private function get_redirect_url($url, $timeout = 10){
|
||||
$ch = curl_init($url);
|
||||
curl_setopt($ch, CURLOPT_TIMEOUT, $timeout);
|
||||
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
|
||||
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
|
||||
$httpheader[] = "Accept: */*";
|
||||
$httpheader[] = "Accept-Language: zh-CN,zh;q=0.8";
|
||||
$httpheader[] = "Connection: close";
|
||||
curl_setopt($ch, CURLOPT_HTTPHEADER, $httpheader);
|
||||
curl_setopt($ch, CURLOPT_HEADER, false);
|
||||
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
|
||||
curl_exec($ch);
|
||||
$redirect_url = curl_getinfo($ch, CURLINFO_REDIRECT_URL);
|
||||
curl_close($ch);
|
||||
return $redirect_url;
|
||||
}
|
||||
|
||||
private function generate_hash($param, $key){
|
||||
ksort($param);
|
||||
$signstr = '';
|
||||
|
||||
foreach($param as $k => $v){
|
||||
if($k != "hash" && $v!=='' && !is_null($v)){
|
||||
$signstr .= $k.'='.$v.'&';
|
||||
}
|
||||
}
|
||||
$signstr = substr($signstr,0,-1);
|
||||
$sign = md5($signstr.$key);
|
||||
return $sign;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,147 @@
|
||||
<?php
|
||||
|
||||
class xunhupay_plugin
|
||||
{
|
||||
static public $info = [
|
||||
'name' => 'xunhupay', //支付插件英文名称,需和目录名称一致,不能有重复
|
||||
'showname' => '虎皮椒支付', //支付插件显示名称
|
||||
'author' => '虎皮椒', //支付插件作者
|
||||
'link' => 'https://www.xunhupay.com/', //支付插件作者链接
|
||||
'types' => ['alipay','wxpay'], //支付插件支持的支付方式,可选的有alipay,qqpay,wxpay,bank
|
||||
'inputs' => [ //支付插件要求传入的参数以及参数显示名称,可选的有appid,appkey,appsecret,appurl,appmchid
|
||||
'appid' => [
|
||||
'name' => '商户ID',
|
||||
'type' => 'input',
|
||||
'note' => '',
|
||||
],
|
||||
'appkey' => [
|
||||
'name' => 'API密钥',
|
||||
'type' => 'input',
|
||||
'note' => '',
|
||||
],
|
||||
'appurl' => [
|
||||
'name' => '网关地址',
|
||||
'type' => 'input',
|
||||
'note' => '不填写默认为https://api.xunhupay.com/payment/do.html',
|
||||
],
|
||||
],
|
||||
'select' => null,
|
||||
'note' => '', //支付密钥填写说明
|
||||
'bindwxmp' => false, //是否支持绑定微信公众号
|
||||
'bindwxa' => false, //是否支持绑定微信小程序
|
||||
];
|
||||
|
||||
static public function submit(){
|
||||
global $siteurl, $channel, $order, $sitename;
|
||||
|
||||
if($order['typename']=='alipay'){
|
||||
return ['type'=>'jump','url'=>'/pay/alipay/'.TRADE_NO.'/'];
|
||||
}elseif($order['typename']=='wxpay'){
|
||||
return ['type'=>'jump','url'=>'/pay/wxpay/'.TRADE_NO.'/'];
|
||||
}
|
||||
}
|
||||
|
||||
static public function mapi(){
|
||||
global $siteurl, $channel, $order, $conf, $device, $mdevice;
|
||||
|
||||
$typename = $order['typename'];
|
||||
return self::$typename();
|
||||
}
|
||||
|
||||
//通用下单
|
||||
static private function addOrder($type){
|
||||
global $channel, $order, $ordername, $conf, $clientip, $siteurl, $device;
|
||||
|
||||
require_once(PAY_ROOT."inc/XunhupayClient.php");
|
||||
|
||||
$params = [
|
||||
'version' => '1.1',
|
||||
'trade_order_id'=> TRADE_NO,
|
||||
'payment' => $type,
|
||||
'total_fee' => $order['realmoney'],
|
||||
'title' => $ordername,
|
||||
'notify_url'=> $conf['localurl'].'pay/notify/'.TRADE_NO.'/',
|
||||
'return_url'=> $siteurl.'pay/return/'.TRADE_NO.'/',
|
||||
];
|
||||
|
||||
if($type == 'wechat' && (checkmobile() || $device=='mobile')){
|
||||
$params['type'] = 'WAP';
|
||||
$params['wap_url'] = $_SERVER['HTTP_HOST'];
|
||||
$params['wap_name'] = $conf['sitename'];
|
||||
}
|
||||
|
||||
$client = new XunhupayClient($channel['appid'],$channel['appkey'],$channel['appurl']);
|
||||
$result = $client->do_payment($params);
|
||||
|
||||
if(checkmobile() || $device=='mobile'){
|
||||
return ['jump', $result['url']];
|
||||
}else{
|
||||
$code_url = $client->parseQrcode($result['url_qrcode']);
|
||||
return ['qrcode', $code_url];
|
||||
}
|
||||
}
|
||||
|
||||
//支付宝扫码支付
|
||||
static public function alipay(){
|
||||
try{
|
||||
[$type, $code_url] = self::addOrder('alipay');
|
||||
}catch(Exception $ex){
|
||||
return ['type'=>'error','msg'=>'支付宝下单失败!'.$ex->getMessage()];
|
||||
}
|
||||
|
||||
if($type == 'jump'){
|
||||
return ['type'=>'jump','url'=>$code_url];
|
||||
}else{
|
||||
return ['type'=>'qrcode','page'=>'alipay_qrcode','url'=>$code_url];
|
||||
}
|
||||
}
|
||||
|
||||
//微信扫码支付
|
||||
static public function wxpay(){
|
||||
try{
|
||||
[$type, $code_url] = self::addOrder('wechat');
|
||||
}catch(Exception $ex){
|
||||
return ['type'=>'error','msg'=>'微信支付下单失败!'.$ex->getMessage()];
|
||||
}
|
||||
|
||||
if($type == 'jump'){
|
||||
return ['type'=>'jump','url'=>$code_url];
|
||||
}else{
|
||||
return ['type'=>'qrcode','page'=>'wxpay_qrcode','url'=>$code_url];
|
||||
}
|
||||
}
|
||||
|
||||
//异步回调
|
||||
static public function notify(){
|
||||
global $channel, $order;
|
||||
|
||||
if(!isset($_POST) || !isset($_POST['hash']) || !isset($_POST['trade_order_id'])) {
|
||||
return ['type'=>'html','data'=>'data_fail'];
|
||||
}
|
||||
|
||||
require_once(PAY_ROOT."inc/XunhupayClient.php");
|
||||
|
||||
$client = new XunhupayClient($channel['appid'],$channel['appkey'],$channel['appurl']);
|
||||
$verify_result = $client->verify($_POST);
|
||||
if(!$verify_result) {
|
||||
return ['type'=>'html','data'=>'sign_fail'];
|
||||
}
|
||||
|
||||
if($_POST['status']=='OD'){
|
||||
$out_trade_no = $_POST['trade_order_id'];
|
||||
$order_id = $_POST['open_order_id'];
|
||||
$total_fee = $_POST['total_fee'];
|
||||
if($out_trade_no == TRADE_NO && round($total_fee,2)==round($order['realmoney'],2)){
|
||||
processNotify($order, $order_id);
|
||||
}
|
||||
return ['type'=>'html','data'=>'success'];
|
||||
}
|
||||
return ['type'=>'html','data'=>'fail'];
|
||||
}
|
||||
|
||||
//支付返回页面
|
||||
static public function return(){
|
||||
return ['type'=>'page','page'=>'return'];
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user