first commit

This commit is contained in:
2025-11-28 10:08:12 +08:00
commit 09a14bf0a3
1088 changed files with 145132 additions and 0 deletions
+203
View File
@@ -0,0 +1,203 @@
<?php
class UmfService
{
private $pay_req_url = 'http://pay.soopay.net/spay/pay/payservice.do';
private $charset = 'UTF-8';
private $sign_type = 'RSA';
private $res_format = 'HTML';
private $version = '4.0';
private $mer_id;
private $platform_public_key;
private $merchant_private_key;
public function __construct($mer_id)
{
$this->mer_id = $mer_id;
if (!file_exists(PAY_ROOT . 'cert/cert.pem')) {
throw new Exception('请将平台公钥cert.pem放到/plugins/umfpay/cert/文件夹下');
}
if (!file_exists(PAY_ROOT . 'cert/key.pem')) {
throw new Exception('请将商户私钥key.pem放到/plugins/umfpay/cert/文件夹下');
}
$this->platform_public_key = file_get_contents(PAY_ROOT . 'cert/cert.pem');
$this->merchant_private_key = file_get_contents(PAY_ROOT . 'cert/key.pem');
}
//发起API请求
public function submit($params){
$public_params = [
'charset' => $this->charset,
'sign_type' => $this->sign_type,
'res_format' => $this->res_format,
'version' => $this->version,
'amt_type' => 'RMB',
'mer_id' => $this->mer_id,
];
$params = array_merge($public_params, $params);
$params = $this->doEncrypt($params);
$params['sign'] = $this->generateSign($params);
$response = get_curl($this->pay_req_url, http_build_query($params));
if(!$response){
throw new Exception('请求接口失败');
}
//对账直接返回
if($params["service"] == "download_settle_file"){
return $response;
}
return $this->parseHTMLStr($response);
}
//获取支付跳转链接
public function getpayurl($params){
$public_params = [
'charset' => $this->charset,
'sign_type' => $this->sign_type,
'res_format' => $this->res_format,
'version' => $this->version,
'amt_type' => 'RMB',
'mer_id' => $this->mer_id,
];
$params = array_merge($public_params, $params);
$params = $this->doEncrypt($params);
$params['sign'] = $this->generateSign($params);
$url = $this->pay_req_url . '?' . http_build_query($params);
return $url;
}
private function parseHTMLStr($htmlStr){
preg_match('/<META\s+name="MobilePayPlatform"\s+content="([\w\W]*?)"/si', $htmlStr, $matches);
$content = $matches[1];
if(!$content){
throw new Exception('平台返回html解析失败');
}
$params = [];
$strs = explode('&',$content);
foreach($strs as $str){
$arr = explode('=',$str);
$params[$arr[0]] = $arr[1];
}
//if(!$this->verifySign($params)){
// throw new Exception('平台响应数据验证签名失败');
//}
return $params;
}
public function responseUMFstr($params){
$public_params = [
'sign_type' => $this->sign_type,
'version' => $this->version,
'mer_id' => $this->mer_id,
'ret_code' => '0000',
'ret_msg' => 'success'
];
$params = array_merge($public_params, $params);
$params['sign'] = $this->generateSign($params);
$str = '';
foreach($params as $key=>$value){
$str .= $key.'='.$value.'&';
}
$str = substr($str,0,-1);
return $str;
}
//获取待签名字符串
private function getSignContent($param){
ksort($param);
$signstr = '';
foreach($param as $k => $v){
if($k != "sign" && $k != "sign_type" && $v!=''){
$signstr .= $k.'='.$v.'&';
}
}
$signstr = substr($signstr,0,-1);
return $signstr;
}
//请求参数签名
private function generateSign($param){
return $this->rsaPrivateSign($this->getSignContent($param));
}
//验签方法
public function verifySign($param){
if(empty($param['sign'])) return false;
return $this->rsaPubilcVerify($this->getSignContent($param), $param['sign']);
}
//敏感字段加密
private function doEncrypt($param) {
$chkKeys = array(
"card_id",
"valid_date",
"cvv2",
"pass_wd",
"identity_code",
"card_holder",
"recv_account",
"recv_user_name",
"identity_holder",
"identityCode",
"cardHolder",
"mer_cust_name",
"account_name",
"bank_account",
"endDate",
);
foreach($chkKeys as $key){
if(isset($param[$key])){
$param[$key] = $this->rsaPubilcEncrypt($param[$key]);
}
}
return $param;
}
//商户私钥签名
private function rsaPrivateSign($data){
$pkeyid = openssl_get_privatekey($this->merchant_private_key);
if(!$pkeyid){
throw new Exception('签名失败,商户私钥不正确');
}
openssl_sign($data, $signature, $pkeyid);
$signature = base64_encode($signature);
return $signature;
}
//平台公钥验签
private function rsaPubilcVerify($data, $signature){
$pubkeyid = openssl_get_publickey($this->platform_public_key);
if(!$pubkeyid){
throw new Exception('验签失败,平台公钥不正确');
}
$result = openssl_verify($data, base64_decode($signature), $pubkeyid);
return $result;
}
//平台公钥加密
private function rsaPubilcEncrypt($data){
$pubkeyid = openssl_get_publickey($this->platform_public_key);
if(!$pubkeyid){
throw new Exception('加密失败,平台公钥不正确');
}
openssl_public_encrypt($data, $encrypted, $pubkeyid);
$encrypted = base64_encode($encrypted);
return $encrypted;
}
//商户私钥解密
private function rsaPrivateDecrypt($data){
$pkeyid = openssl_get_privatekey($this->merchant_private_key);
if(!$pkeyid){
throw new Exception('解密失败,商户私钥不正确');
}
openssl_private_decrypt(base64_decode($data), $decrypted, $pkeyid);
return $decrypted;
}
}
+232
View File
@@ -0,0 +1,232 @@
<?php
class umfpay_plugin
{
static public $info = [
'name' => 'umfpay', //支付插件英文名称,需和目录名称一致,不能有重复
'showname' => '联动优势', //支付插件显示名称
'author' => '联动优势', //支付插件作者
'link' => 'https://xy.umfintech.com/', //支付插件作者链接
'types' => ['alipay','wxpay','bank'], //支付插件支持的支付方式,可选的有alipay,qqpay,wxpay,bank
'inputs' => [ //支付插件要求传入的参数以及参数显示名称,可选的有appid,appkey,appsecret,appurl,appmchid
'appid' => [
'name' => '商户编号',
'type' => 'input',
'note' => '',
],
'appkey' => [
'name' => '商户密钥',
'type' => 'input',
'note' => '此项随便填写',
],
],
'select' => null,
'note' => '将平台公钥cert.pem和商户私钥key.pem放到/plugins/umfpay/cert/文件夹下', //支付密钥填写说明
'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'){
if(checkwechat()){
return ['type'=>'jump','url'=>'/pay/wxjspay/'.TRADE_NO.'/?d=1'];
}else{
return ['type'=>'jump','url'=>'/pay/wxpay/'.TRADE_NO.'/'];
}
}elseif($order['typename']=='bank'){
return ['type'=>'jump','url'=>'/pay/bank/'.TRADE_NO.'/'];
}
}
static public function mapi(){
global $siteurl, $channel, $order, $conf, $device, $mdevice;
if($order['typename']=='alipay'){
return self::alipay();
}elseif($order['typename']=='wxpay'){
if($mdevice=='wechat'){
return self::wxjspay();
}else{
return self::wxpay();
}
}elseif($order['typename']=='bank'){
return self::bank();
}
}
//扫码支付
static private function qrcode($type){
global $siteurl, $channel, $order, $ordername, $conf, $clientip;
require(PAY_ROOT."inc/UmfService.class.php");
$params = [
'service' => 'active_scancode_order_new',
'notify_url' => $conf['localurl'] . 'pay/notify/' . TRADE_NO . '/',
'goods_inf' => $ordername,
'order_id' => TRADE_NO,
'mer_date' => date("Ymd"),
'amount' => strval($order['realmoney']*100),
'user_ip' => $clientip,
'scancode_type' => $type,
'mer_flag' => 'KMER',
'consumer_id' => str_replace('.','',$clientip)
];
$client = new UmfService($channel['appid']);
$result = $client->submit($params);
if (isset($result['ret_code']) && $result['ret_code'] == '0000') {
return base64_decode($result['bank_payurl']);
} elseif(isset($result['ret_code'])) {
throw new Exception('['.$result['ret_code'].']'.$result['ret_msg']);
}else{
throw new Exception('返回数据解析失败');
}
}
//支付宝扫码支付
static public function alipay(){
try{
$code_url = self::qrcode('ALIPAY');
}catch(Exception $ex){
return ['type'=>'error','msg'=>'支付宝支付下单失败!'.$ex->getMessage()];
}
return ['type'=>'qrcode','page'=>'alipay_qrcode','url'=>$code_url];
}
//微信扫码支付
static public function wxpay(){
try{
$code_url = self::qrcode('WECHAT');
}catch(Exception $ex){
return ['type'=>'error','msg'=>'微信支付下单失败!'.$ex->getMessage()];
}
if (checkmobile()) {
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;
require(PAY_ROOT."inc/UmfService.class.php");
$params = [
'service' => 'publicnumber_and_verticalcode',
'notify_url' => $conf['localurl'] . 'pay/notify/' . TRADE_NO . '/',
'ret_url' => $siteurl . 'pay/return/' . TRADE_NO . '/',
'goods_inf' => $ordername,
'order_id' => TRADE_NO,
'mer_date' => date("Ymd"),
'amount' => strval($order['realmoney']*100),
'user_ip' => $clientip,
'is_public_number' => 'Y',
];
$client = new UmfService($channel['appid']);
$url = $client->getpayurl($params);
return ['type'=>'jump','url'=>$url];
}
//云闪付扫码支付
static public function bank(){
try{
$code_url = self::qrcode('UNION');
}catch(Exception $ex){
return ['type'=>'error','msg'=>'云闪付下单失败!'.$ex->getMessage()];
}
return ['type'=>'qrcode','page'=>'bank_qrcode','url'=>$code_url];
}
//异步回调
static public function notify(){
global $channel, $order;
require(PAY_ROOT."inc/UmfService.class.php");
$client = new UmfService($channel['appid']);
$verify_result = $client->verifySign($_GET);
$params = [
'order_id' => $_GET['order_id'],
'mer_date' => $_GET['mer_date'],
];
if($verify_result) {//验证成功
if ($_GET['trade_state'] == 'TRADE_SUCCESS') {
if($_GET['order_id'] == TRADE_NO){
processNotify($order, $_GET['trade_no'], $_GET['mer_cust_id']);
}
}else{
$params['ret_code'] = '0001';
$params['ret_msg'] = 'trade_state'.$_GET['trade_state'];
}
}
else {
//验证失败
$params['ret_code'] = '0001';
$params['ret_msg'] = 'sign fail';
}
$response_str = $client->responseUMFstr($params);
$html = '<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">';
$html .= '<html><head>';
$html .= '<META NAME="MobilePayPlatform" CONTENT="'.$response_str.'">';
$html .= '</head>';
$html .= '<body></body>';
$html .= '</html>';
return ['type'=>'html','data'=>$html];
}
//同步回调
static public function return(){
return ['type'=>'page','page'=>'return'];
}
//退款
static public function refund($order){
global $channel;
if(empty($order))exit();
require(PAY_ROOT."inc/UmfService.class.php");
$params = [
'service' => 'mer_refund',
'refund_no' => $order['refund_no'],
'order_id' => $order['trade_no'],
'mer_date' => substr($order['trade_no'], 0, 8),
'org_amount' => strval($order['realmoney']*100),
'refund_amount' => strval($order['refundmoney']*100),
];
try{
$client = new UmfService($channel['appid']);
$result = $client->submit($params);
}catch(Exception $ex){
return ['code'=>-1, 'msg'=>$ex->getMessage()];
}
if (isset($result['ret_code']) && $result['ret_code'] == '0000') {
return ['code'=>0, 'trade_no'=>$result['order_id'], 'refund_fee'=>$result['refund_amt']];
} elseif(isset($result['ret_code'])) {
return ['code'=>-1, 'msg'=>'['.$result['ret_code'].']'.$result['ret_msg']];
}else{
return ['code'=>-1, 'msg'=>'未知错误'];
}
}
}