first commit
This commit is contained in:
@@ -0,0 +1,137 @@
|
||||
<?php
|
||||
namespace xsy;
|
||||
use Exception;
|
||||
|
||||
/**
|
||||
* @see https://www.yuque.com/zhaohanying/egbhoy/wca6r5ueiaiyga3c
|
||||
*/
|
||||
class PayClient
|
||||
{
|
||||
const VERSION = '1.0';
|
||||
const SIGN_TYPE = 'RSA';
|
||||
|
||||
protected $gateway_url = 'https://gateway-hpx.hnapay.com/order';
|
||||
protected $appid;
|
||||
private $public_key;
|
||||
private $private_key;
|
||||
|
||||
public function __construct($appid, $public_key, $private_key, $isTest = false)
|
||||
{
|
||||
$this->appid = $appid;
|
||||
$this->public_key = $public_key;
|
||||
$this->private_key = $private_key;
|
||||
if($isTest) $this->gateway_url = 'https://gateway-hpxtest1.hnapay.com/order';
|
||||
}
|
||||
|
||||
//发起API请求
|
||||
public function request($path, $data){
|
||||
$data = array_filter($data, function ($value) {
|
||||
return $value !== null;
|
||||
});
|
||||
$requrl = $this->gateway_url . $path;
|
||||
|
||||
$param = [
|
||||
'reqId' => random(60),
|
||||
'orgNo' => $this->appid,
|
||||
'reqData' => $data,
|
||||
'signType' => self::SIGN_TYPE,
|
||||
'timestamp' => $this->getMillisecond(),
|
||||
'version' => self::VERSION
|
||||
];
|
||||
$param['sign'] = $this->generateSign($param);
|
||||
$body = json_encode($param, JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES);
|
||||
|
||||
$response = get_curl($requrl, $body, 0, 0, 0, 0, 0, ['Content-Type: application/json; charset=utf-8']);
|
||||
|
||||
$result = json_decode($response, true);
|
||||
|
||||
if(isset($result['code']) && $result['code']=='0000'){
|
||||
/*if (!empty($result['sign']) && strpos($path, '/merchant/queryApplyResult')===false) {
|
||||
if(!$this->verifySign($result, null)) throw new Exception('返回数据验签失败');
|
||||
}*/
|
||||
return $result['respData'];
|
||||
}else{
|
||||
throw new Exception($result['msg']?$result['msg']:'返回数据解析失败');
|
||||
}
|
||||
}
|
||||
|
||||
//异步回调验证
|
||||
public function verifySign($data, $rawdata)
|
||||
{
|
||||
if(!isset($data['sign'])) return false;
|
||||
|
||||
return $this->rsaPublicVerify($this->getSignContent($data, $rawdata), $data['sign']);
|
||||
}
|
||||
|
||||
//请求参数签名
|
||||
private function generateSign($param){
|
||||
return $this->rsaPrivateSign($this->getSignContent($param));
|
||||
}
|
||||
|
||||
//获取待签名字符串
|
||||
private function getSignContent($param, $rawdata = null){
|
||||
if(isset($param['respData']) && is_array($param['respData'])){
|
||||
if($rawdata){
|
||||
$param['respData'] = getSubstr($rawdata, '"respData":', ',"sign"');
|
||||
}else{
|
||||
foreach ($param['respData'] as $k => $v) {
|
||||
if ($v === '' || $v === null) {
|
||||
unset($param['respData'][$k]);
|
||||
}
|
||||
}
|
||||
$param['respData'] = json_encode($param['respData'], JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES);
|
||||
}
|
||||
}
|
||||
if(isset($param['reqData']) && is_array($param['reqData'])){
|
||||
$param['reqData'] = json_encode($param['reqData'], JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES);
|
||||
}
|
||||
|
||||
ksort($param);
|
||||
$signstr = '';
|
||||
|
||||
foreach($param as $k => $v){
|
||||
if($k != "sign" && $v!=='' && $v!==null){
|
||||
$signstr .= $k.'='.$v.'&';
|
||||
}
|
||||
}
|
||||
$signstr = substr($signstr,0,-1);
|
||||
return $signstr;
|
||||
}
|
||||
|
||||
//商户私钥签名
|
||||
private function rsaPrivateSign($data){
|
||||
$key = "-----BEGIN RSA PRIVATE KEY-----\n" .
|
||||
wordwrap($this->private_key, 64, "\n", true) .
|
||||
"\n-----END RSA PRIVATE KEY-----";
|
||||
$privatekey = openssl_get_privatekey($key);
|
||||
if(!$privatekey){
|
||||
throw new Exception('签名失败,商户私钥错误');
|
||||
}
|
||||
openssl_sign($data, $sign, $privatekey);
|
||||
return base64_encode($sign);
|
||||
}
|
||||
|
||||
//平台公钥验签
|
||||
private function rsaPublicVerify($data, $sign){
|
||||
$key = "-----BEGIN PUBLIC KEY-----\n" .
|
||||
wordwrap($this->public_key, 64, "\n", true) .
|
||||
"\n-----END PUBLIC KEY-----";
|
||||
$publickey = openssl_get_publickey($key);
|
||||
if (!$publickey) {
|
||||
throw new \Exception("验签失败,平台公钥错误");
|
||||
}
|
||||
$result = openssl_verify($data, base64_decode($sign), $publickey);
|
||||
return $result === 1;
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取毫秒级时间戳
|
||||
* @return float
|
||||
*/
|
||||
private function getMillisecond()
|
||||
{
|
||||
list($s1, $s2) = explode(' ', microtime());
|
||||
return (float)sprintf('%.0f', (floatval($s1) + floatval($s2)) * 1000);
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,343 @@
|
||||
<?php
|
||||
|
||||
class xsy_plugin
|
||||
{
|
||||
static $info = [
|
||||
'name' => 'xsy', // 插件标识
|
||||
'showname' => '新生易', // 插件显示名称
|
||||
'author' => '新生易', // 作者信息
|
||||
'link' => 'https://www.hnapay.com/', // 支付官网
|
||||
'types' => ['wxpay', 'alipay', 'bank'], // 可支持的支付类型
|
||||
'inputs' => [ // 配置信息表单
|
||||
'appid' => [
|
||||
'name' => '机构代码',
|
||||
'type' => 'input',
|
||||
],
|
||||
'appkey' => [
|
||||
'name' => '平台公钥',
|
||||
'type' => 'textarea',
|
||||
],
|
||||
'appsecret' => [
|
||||
'name' => '商户私钥',
|
||||
'type' => 'textarea',
|
||||
],
|
||||
'appmchid' => [
|
||||
'name' => '商户编号',
|
||||
'type' => 'input',
|
||||
],
|
||||
'appswitch' => [
|
||||
'name' => '环境选择',
|
||||
'type' => 'select',
|
||||
'options' => [0=>'生产环境',1=>'测试环境'],
|
||||
],
|
||||
],
|
||||
'select_alipay' => [
|
||||
'1' => '扫码支付',
|
||||
'2' => '生活号支付',
|
||||
],
|
||||
'bindwxmp' => true, // 绑定微信公众号
|
||||
'bindwxa' => true, // 绑定微信小程序
|
||||
'note' => '',
|
||||
];
|
||||
|
||||
public static function submit()
|
||||
{
|
||||
global $siteurl, $channel, $order;
|
||||
|
||||
if($order['typename']=='alipay'){
|
||||
if(checkalipay() && in_array('2',$channel['apptype'])){
|
||||
return ['type'=>'jump','url'=>'/pay/alipayjs/'.TRADE_NO.'/?d=1'];
|
||||
}else{
|
||||
return ['type'=>'jump','url'=>'/pay/alipay/'.TRADE_NO.'/'];
|
||||
}
|
||||
}elseif($order['typename']=='wxpay'){
|
||||
if(checkwechat()){
|
||||
return ['type'=>'jump','url'=>'/pay/wxjspay/'.TRADE_NO.'/?d=1'];
|
||||
}elseif(checkmobile() && $channel['appwxa']>0){
|
||||
return ['type'=>'jump','url'=>'/pay/wxwappay/'.TRADE_NO.'/'];
|
||||
}else{
|
||||
return ['type'=>'jump','url'=>'/pay/wxpay/'.TRADE_NO.'/'];
|
||||
}
|
||||
}elseif($order['typename']=='bank'){
|
||||
return ['type'=>'jump','url'=>'/pay/bank/'.TRADE_NO.'/'];
|
||||
}
|
||||
}
|
||||
|
||||
public static function mapi()
|
||||
{
|
||||
global $siteurl, $channel, $order, $conf, $device, $mdevice;
|
||||
|
||||
if($order['typename']=='alipay'){
|
||||
if($mdevice=='alipay' && in_array('2',$channel['apptype'])){
|
||||
return ['type'=>'jump','url'=>$siteurl.'pay/alipayjs/'.TRADE_NO.'/?d=1'];
|
||||
}else{
|
||||
return self::alipay();
|
||||
}
|
||||
}elseif($order['typename']=='wxpay'){
|
||||
if($mdevice=='wechat'){
|
||||
return ['type'=>'jump','url'=>$siteurl.'pay/wxjspay/'.TRADE_NO.'/?d=1'];
|
||||
}elseif($device=='mobile' && $channel['appwxa']>0){
|
||||
return self::wxwappay();
|
||||
}else{
|
||||
return self::wxpay();
|
||||
}
|
||||
}elseif($order['typename']=='bank'){
|
||||
return self::bank();
|
||||
}
|
||||
}
|
||||
|
||||
//扫码支付
|
||||
private static function qrcode($pay_type)
|
||||
{
|
||||
global $siteurl, $channel, $order, $ordername, $conf, $clientip;
|
||||
require(PAY_ROOT . 'lib/PayClient.php');
|
||||
|
||||
$param = [
|
||||
'merchantNo' => $channel['appmchid'],
|
||||
'orderNo' => TRADE_NO,
|
||||
'amt' => intval(round($order['realmoney']*100)),
|
||||
'payType' => $pay_type,
|
||||
'subject' => $order['name'],
|
||||
'trmIp' => $clientip,
|
||||
'customerIp' => $clientip,
|
||||
'notifyUrl' => $conf['localurl'] . 'pay/notify/' . TRADE_NO . '/'
|
||||
];
|
||||
|
||||
$client = new \xsy\PayClient($channel['appid'], $channel['appkey'], $channel['appsecret'], $channel['appswitch'] == 1);
|
||||
return \lib\Payment::lockPayData(TRADE_NO, function () use ($client, $param) {
|
||||
$result = $client->request('/trade/activeScan', $param);
|
||||
if(strpos($result['payUrl'], 'qrContent=')){
|
||||
$result['payUrl'] = getSubstr($result['payUrl'], 'qrContent=', '&sign=');
|
||||
}
|
||||
return $result['payUrl'];
|
||||
});
|
||||
}
|
||||
|
||||
//公众号小程序支付
|
||||
private static function jsapi($pay_type, $pay_way, $userid, $appid = null)
|
||||
{
|
||||
global $siteurl, $channel, $order, $ordername, $conf, $clientip;
|
||||
require(PAY_ROOT . 'lib/PayClient.php');
|
||||
|
||||
$param = [
|
||||
'merchantNo' => $channel['appmchid'],
|
||||
'orderNo' => TRADE_NO,
|
||||
'amt' => intval(round($order['realmoney']*100)),
|
||||
'payType' => $pay_type,
|
||||
'payWay' => $pay_way,
|
||||
'subAppId' => $appid,
|
||||
'userId' => $userid,
|
||||
'subject' => $order['name'],
|
||||
'trmIp' => $clientip,
|
||||
'customerIp' => $clientip,
|
||||
'notifyUrl' => $conf['localurl'] . 'pay/notify/' . TRADE_NO . '/'
|
||||
];
|
||||
|
||||
$client = new \xsy\PayClient($channel['appid'], $channel['appkey'], $channel['appsecret'], $channel['appswitch'] == 1);
|
||||
return \lib\Payment::lockPayData(TRADE_NO, function () use ($client, $param) {
|
||||
$result = $client->request('/trade/jsapiScan', $param);
|
||||
return $result;
|
||||
});
|
||||
}
|
||||
|
||||
//支付宝扫码支付
|
||||
public static function alipay()
|
||||
{
|
||||
global $channel, $siteurl, $mdevice;
|
||||
if(in_array('2',$channel['apptype']) && !in_array('1',$channel['apptype'])){
|
||||
$code_url = $siteurl.'pay/alipayjs/'.TRADE_NO.'/';
|
||||
}else{
|
||||
try {
|
||||
$code_url = self::qrcode('ALIPAY');
|
||||
} catch (Exception $e) {
|
||||
return ['type'=>'error','msg'=>'支付宝下单失败!'.$e->getMessage()];
|
||||
}
|
||||
}
|
||||
if(checkalipay() || $mdevice=='alipay'){
|
||||
return ['type'=>'jump','url'=>$code_url];
|
||||
}else{
|
||||
return ['type'=>'qrcode','page'=>'alipay_qrcode','url'=>$code_url];
|
||||
}
|
||||
}
|
||||
|
||||
//支付宝生活号支付
|
||||
public static function alipayjs()
|
||||
{
|
||||
[$user_type, $user_id] = alipay_oauth();
|
||||
|
||||
$blocks = checkBlockUser($user_id, TRADE_NO);
|
||||
if($blocks) return $blocks;
|
||||
|
||||
try{
|
||||
$retData = self::jsapi('ALIPAY', '02', $user_id);
|
||||
}catch(Exception $ex){
|
||||
return ['type'=>'error','msg'=>'支付宝下单失败!'.$ex->getMessage()];
|
||||
}
|
||||
|
||||
if($_GET['d']=='1'){
|
||||
$redirect_url='data.backurl';
|
||||
}else{
|
||||
$redirect_url='\'/pay/ok/'.TRADE_NO.'/\'';
|
||||
}
|
||||
return ['type'=>'page','page'=>'alipay_jspay','data'=>['alipay_trade_no'=>$retData['source'], 'redirect_url'=>$redirect_url]];
|
||||
}
|
||||
|
||||
//微信扫码支付
|
||||
public static function wxpay()
|
||||
{
|
||||
global $siteurl, $device;
|
||||
$code_url = $siteurl.'pay/wxjspay/'.TRADE_NO.'/';
|
||||
if (checkmobile() || $device=='mobile') {
|
||||
return ['type'=>'qrcode','page'=>'wxpay_wap','url'=>$code_url];
|
||||
} else {
|
||||
return ['type'=>'qrcode','page'=>'wxpay_qrcode','url'=>$code_url];
|
||||
}
|
||||
}
|
||||
|
||||
//微信公众号支付
|
||||
static public function wxjspay(){
|
||||
global $siteurl, $channel, $order, $ordername, $conf, $clientip;
|
||||
|
||||
//①、获取用户openid
|
||||
$wxinfo = \lib\Channel::getWeixin($channel['appwxmp']);
|
||||
if(!$wxinfo) return ['type'=>'error','msg'=>'支付通道绑定的微信公众号不存在'];
|
||||
try{
|
||||
$tools = new \WeChatPay\JsApiTool($wxinfo['appid'], $wxinfo['appsecret']);
|
||||
$openid = $tools->GetOpenid();
|
||||
}catch(Exception $e){
|
||||
return ['type'=>'error','msg'=>$e->getMessage()];
|
||||
}
|
||||
$blocks = checkBlockUser($openid, TRADE_NO);
|
||||
if($blocks) return $blocks;
|
||||
|
||||
//②、统一下单
|
||||
try{
|
||||
$result = self::jsapi('WECHAT', '02', $openid, $wxinfo['appid']);
|
||||
}catch(Exception $ex){
|
||||
return ['type'=>'error','msg'=>'微信支付下单失败!'.$ex->getMessage()];
|
||||
}
|
||||
|
||||
$payinfo = ['appId'=>$result['payAppId'], 'timeStamp'=>$result['payTimeStamp'], 'nonceStr'=>$result['paynonceStr'], 'package'=>$result['payPackage'], 'signType'=>$result['paySignType'], 'paySign'=>$result['paySign']];
|
||||
|
||||
if($_GET['d']==1){
|
||||
$redirect_url='data.backurl';
|
||||
}else{
|
||||
$redirect_url='\'/pay/ok/'.TRADE_NO.'/\'';
|
||||
}
|
||||
return ['type'=>'page','page'=>'wxpay_jspay','data'=>['jsApiParameters'=>json_encode($payinfo), 'redirect_url'=>$redirect_url]];
|
||||
}
|
||||
|
||||
//微信小程序支付
|
||||
static public function wxminipay(){
|
||||
global $siteurl, $channel, $order, $ordername, $conf, $clientip;
|
||||
|
||||
$code = isset($_GET['code'])?trim($_GET['code']):exit('{"code":-1,"msg":"code不能为空"}');
|
||||
|
||||
//①、获取用户openid
|
||||
$wxinfo = \lib\Channel::getWeixin($channel['appwxa']);
|
||||
if(!$wxinfo)exit('{"code":-1,"msg":"支付通道绑定的微信小程序不存在"}');
|
||||
try{
|
||||
$tools = new \WeChatPay\JsApiTool($wxinfo['appid'], $wxinfo['appsecret']);
|
||||
$openid = $tools->AppGetOpenid($code);
|
||||
}catch(Exception $e){
|
||||
exit('{"code":-1,"msg":"'.$e->getMessage().'"}');
|
||||
}
|
||||
$blocks = checkBlockUser($openid, TRADE_NO);
|
||||
if($blocks)exit('{"code":-1,"msg":"'.$blocks['msg'].'"}');
|
||||
|
||||
//②、统一下单
|
||||
try{
|
||||
$result = self::jsapi('WECHAT', '03', $openid, $wxinfo['appid']);
|
||||
}catch(Exception $e){
|
||||
exit('{"code":-1,"msg":"'.$e->getMessage().'"}');
|
||||
}
|
||||
|
||||
$payinfo = ['appId'=>$result['payAppId'], 'timeStamp'=>$result['payTimeStamp'], 'nonceStr'=>$result['paynonceStr'], 'package'=>$result['payPackage'], 'signType'=>$result['paySignType'], 'paySign'=>$result['paySign']];
|
||||
|
||||
exit(json_encode(['code'=>0, 'data'=>$payinfo]));
|
||||
}
|
||||
|
||||
//微信手机支付
|
||||
static public function wxwappay(){
|
||||
global $channel;
|
||||
$wxinfo = \lib\Channel::getWeixin($channel['appwxa']);
|
||||
if(!$wxinfo) return ['type'=>'error','msg'=>'支付通道绑定的微信小程序不存在'];
|
||||
try{
|
||||
$code_url = wxminipay_jump_scheme($wxinfo['id'], TRADE_NO);
|
||||
}catch(Exception $e){
|
||||
return ['type'=>'error','msg'=>$e->getMessage()];
|
||||
}
|
||||
return ['type'=>'scheme','page'=>'wxpay_mini','url'=>$code_url];
|
||||
}
|
||||
|
||||
//云闪付扫码支付
|
||||
public static function bank()
|
||||
{
|
||||
try {
|
||||
$url = self::qrcode('UNIONPAY');
|
||||
} catch (Exception $e) {
|
||||
return ['type'=>'error','msg'=>'云闪付下单失败!'.$e->getMessage()];
|
||||
}
|
||||
return ['type' => 'qrcode', 'page' => 'bank_qrcode', 'url' => $url];
|
||||
}
|
||||
|
||||
//回调
|
||||
public static function notify()
|
||||
{
|
||||
global $channel, $order;
|
||||
$data = file_get_contents('php://input');
|
||||
$arr = json_decode($data, true);
|
||||
if (!$arr) return ['type' => 'html', 'data' => '{"code":"nodata"}'];
|
||||
|
||||
require(PAY_ROOT . 'lib/PayClient.php');
|
||||
$client = new \xsy\PayClient($channel['appid'], $channel['appkey'], $channel['appsecret'], $channel['appswitch'] == 1);
|
||||
|
||||
try {
|
||||
if (!$client->verifySign($arr, $data)){
|
||||
return ['type' => 'html', 'data' => '{"code":"fail"}'];
|
||||
}
|
||||
$out_trade_no = $arr['respData']['orderNo'];
|
||||
$api_trade_no = $arr['respData']['outOrderNo'];
|
||||
$buyer = $arr['respData']['buyerId'];
|
||||
if($out_trade_no == TRADE_NO){
|
||||
processNotify($order, $api_trade_no, $buyer);
|
||||
}
|
||||
return ['type' => 'html', 'data' => '{"code":"success"}'];
|
||||
} catch (Exception $e) {
|
||||
return ['type' => 'html', 'data' => '{"code":"'.$e->getMessage().'"}'];
|
||||
}
|
||||
}
|
||||
|
||||
//同步回调
|
||||
static public function return(){
|
||||
return ['type'=>'page','page'=>'return'];
|
||||
}
|
||||
|
||||
//支付成功页面
|
||||
static public function ok(){
|
||||
return ['type'=>'page','page'=>'ok'];
|
||||
}
|
||||
|
||||
//退款
|
||||
public static function refund($order)
|
||||
{
|
||||
global $channel, $order;
|
||||
|
||||
require(PAY_ROOT . 'lib/PayClient.php');
|
||||
|
||||
$param = [
|
||||
'merchantNo' => $channel['appmchid'],
|
||||
'orderNo' => $order['refund_no'],
|
||||
'origOrderNo' => $order['trade_no'],
|
||||
'amt' => intval(round($order['refundmoney']*100))
|
||||
];
|
||||
|
||||
try {
|
||||
$client = new \xsy\PayClient($channel['appid'], $channel['appkey'], $channel['appsecret'], $channel['appswitch'] == 1);
|
||||
$result = $client->request('/trade/refund', $param);
|
||||
return ['code' => 0, 'trade_no'=>$result['orderNo'], 'refund_fee'=>$result['amt']/100];
|
||||
} catch (Exception $e) {
|
||||
return ['code' => -1, 'msg' => $e->getMessage()];
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user