shopxo/extend/payment/BaiduMini.php

340 lines
11 KiB
PHP
Raw Normal View History

2019-07-17 20:12:20 +08:00
<?php
// +----------------------------------------------------------------------
// | ShopXO 国内领先企业级B2C免费开源电商系统
// +----------------------------------------------------------------------
2021-03-16 10:34:52 +08:00
// | Copyright (c) 2011~2099 http://shopxo.net All rights reserved.
2019-07-17 20:12:20 +08:00
// +----------------------------------------------------------------------
2021-03-16 10:34:52 +08:00
// | Licensed ( https://opensource.org/licenses/mit-license.php )
2019-07-17 20:12:20 +08:00
// +----------------------------------------------------------------------
// | Author: Devil
// +----------------------------------------------------------------------
namespace payment;
/**
2019-07-18 15:48:02 +08:00
* 百度小程序支付
2019-07-17 20:12:20 +08:00
* @author Devil
* @blog http://gong.gg/
* @version 1.0.0
* @date 2018-09-19
* @desc description
*/
2019-07-18 15:48:02 +08:00
class BaiduMini
2019-07-17 20:12:20 +08:00
{
// 插件配置参数
private $config;
/**
* 构造方法
* @author Devil
* @blog http://gong.gg/
* @version 1.0.0
* @date 2018-09-17
* @desc description
* @param [array] $params [输入参数(支付配置参数)]
*/
public function __construct($params = [])
{
$this->config = $params;
}
/**
* 配置信息
* @author Devil
* @blog http://gong.gg/
* @version 1.0.0
* @date 2018-09-19
* @desc description
*/
public function Config()
{
// 基础信息
$base = [
'name' => '百度', // 插件名称
2019-08-18 12:12:47 +08:00
'version' => '1.0.0', // 插件版本
2019-07-17 20:12:20 +08:00
'apply_version' => '不限', // 适用系统版本描述
2019-07-18 11:56:06 +08:00
'apply_terminal'=> ['baidu'], // 适用终端 默认全部 ['pc', 'h5', 'app', 'alipay', 'weixin', 'baidu']
2019-07-18 15:48:02 +08:00
'desc' => '适用百度小程序,百度收银台已集成度小满、支付宝、微信支付,即时到帐支付方式,买家的交易资金直接打入卖家百度账户,快速回笼交易资金。 <a href="https://smartprogram.baidu.com/docs/introduction/pay/" target="_blank">立即申请</a>', // 插件描述支持html
2019-07-17 20:12:20 +08:00
'author' => 'Devil', // 开发者
'author_url' => 'http://shopxo.net/', // 开发者主页
];
// 配置信息
$element = [
2019-07-18 11:56:06 +08:00
[
'element' => 'input',
'type' => 'text',
'default' => '',
'name' => 'dealid',
'placeholder' => 'dealId',
'title' => 'dealId',
'is_required' => 0,
'message' => '请填写dealId',
],
[
'element' => 'input',
'type' => 'text',
'default' => '',
'name' => 'appkey',
'placeholder' => 'APP KEY',
'title' => 'APP KEY',
2019-07-17 20:12:20 +08:00
'is_required' => 0,
2019-07-18 11:56:06 +08:00
'message' => '请填写APP KEY',
2019-07-17 20:12:20 +08:00
],
[
'element' => 'textarea',
'name' => 'rsa_public',
'placeholder' => '应用公钥',
'title' => '应用公钥',
'is_required' => 0,
'rows' => 6,
'message' => '请填写应用公钥',
],
[
'element' => 'textarea',
'name' => 'rsa_private',
'placeholder' => '应用私钥',
'title' => '应用私钥',
'is_required' => 0,
'rows' => 6,
'message' => '请填写应用私钥',
],
[
'element' => 'textarea',
'name' => 'out_rsa_public',
2019-07-18 11:56:06 +08:00
'placeholder' => '平台公钥',
'title' => '平台公钥',
2019-07-17 20:12:20 +08:00
'is_required' => 0,
'rows' => 6,
2019-07-18 11:56:06 +08:00
'message' => '请填写平台公钥',
2019-07-17 20:12:20 +08:00
],
2019-08-18 12:12:47 +08:00
[
2019-08-18 18:46:49 +08:00
'element' => 'message',
'message' => '异步通知地址,将该地址配置到百度小程序支付后台异步通知<br />'.__MY_URL__.'payment_order_'.strtolower(str_replace(['payment', '\\'], '', get_class($this))).'_notify.php',
2019-08-18 12:12:47 +08:00
],
2019-07-17 20:12:20 +08:00
];
return [
'base' => $base,
'element' => $element,
];
}
/**
* 支付入口
* @author Devil
* @blog http://gong.gg/
* @version 1.0.0
* @date 2018-09-19
* @desc description
* @param [array] $params [输入参数]
*/
public function Pay($params = [])
{
// 参数
if(empty($params))
{
return DataReturn('参数不能为空', -1);
}
// 配置信息
if(empty($this->config))
{
return DataReturn('支付缺少配置', -1);
}
2019-07-18 15:48:02 +08:00
// opensssl校验
if(!function_exists('openssl_pkey_get_private') || !function_exists('openssl_sign'))
{
return DataReturn('openssl扩展不存在', -1);
}
// 支付参数
2019-07-18 11:56:06 +08:00
$data = [
'dealId' => $this->config['dealid'],
'appKey' => $this->config['appkey'],
2020-02-22 12:22:22 +08:00
'totalAmount' => (int) (($params['total_price']*1000)/10),
2019-07-18 11:56:06 +08:00
'tpOrderId' => $params['order_no'],
'dealTitle' => $params['name'],
2019-07-18 13:57:48 +08:00
'signFieldsRange' => 1,
2019-07-18 11:56:06 +08:00
];
$data['rsaSign'] = $this->SignWithRsa($data);
2019-07-18 14:32:25 +08:00
$biz_info = [
'tpData' => [
'appKey' => $data['appKey'],
'dealId' => $data['dealId'],
'tpOrderId' => $data['tpOrderId'],
'rsaSign' => $data['rsaSign'],
'totalAmount' => $data['totalAmount'],
'returnData' => (object) [],
'displayData' => (object) [],
],
];
2019-07-18 14:40:16 +08:00
$data['bizInfo'] = json_encode($biz_info, JSON_UNESCAPED_UNICODE);
2019-07-18 11:56:06 +08:00
return DataReturn('处理成功', 0, $data);
2019-07-17 20:12:20 +08:00
}
/**
* 支付回调处理
* @author Devil
* @blog http://gong.gg/
* @version 1.0.0
* @date 2018-09-19
* @desc description
* @param [array] $params [输入参数]
*/
public function Respond($params = [])
{
// 签名
2019-07-18 15:48:02 +08:00
if(!$this->CheckSignWithRsa($params))
2019-07-17 20:12:20 +08:00
{
return DataReturn('签名校验失败', -1);
}
// 支付状态
2019-07-18 15:25:35 +08:00
if(isset($params['status']) && $params['status'] == 2)
2019-07-17 20:12:20 +08:00
{
2019-07-18 15:25:35 +08:00
return DataReturn('支付成功', 0, $this->ReturnData($params));
2019-07-17 20:12:20 +08:00
}
return DataReturn('处理异常错误', -100);
}
/**
* [ReturnData 返回数据统一格式]
* @author Devil
* @blog http://gong.gg/
* @version 1.0.0
* @datetime 2018-10-06T16:54:24+0800
* @param [array] $data [返回数据]
*/
private function ReturnData($data)
{
// 返回数据固定基础参数
2019-07-18 15:25:35 +08:00
$data['trade_no'] = $data['orderId']; // 支付平台 - 订单号
$data['buyer_user'] = $data['userId']; // 支付平台 - 用户
$data['out_trade_no'] = $data['tpOrderId']; // 本系统发起支付的 - 订单号
2019-07-17 20:12:20 +08:00
$data['subject'] = isset($data['subject']) ? $data['subject'] : ''; // 本系统发起支付的 - 商品名称
2019-07-18 15:25:35 +08:00
$data['pay_price'] = $data['totalMoney']/100; // 本系统发起支付的 - 总价
2019-07-17 20:12:20 +08:00
return $data;
}
2019-07-18 11:56:06 +08:00
/**
2019-07-18 15:48:02 +08:00
* 私钥生成签名字符串
* @author Devil
* @blog http://gong.gg/
* @version 1.0.0
* @date 2019-07-18
* @desc description
* @param [array] $data [需要生成签名的数据]
2019-07-18 11:56:06 +08:00
*/
2019-07-18 15:48:02 +08:00
public function SignWithRsa($data)
2019-07-18 11:56:06 +08:00
{
$sign = '';
2019-07-18 15:48:02 +08:00
if(empty($data))
{
2019-07-18 11:56:06 +08:00
return $sign;
}
2019-11-28 11:32:02 +08:00
if(stripos($this->config['rsa_private'], '-----') === false)
{
$res = "-----BEGIN RSA PRIVATE KEY-----\n";
$res .= wordwrap($this->config['rsa_private'], 64, "\n", true);
$res .= "\n-----END RSA PRIVATE KEY-----";
} else {
$res = $this->config['rsa_private'];
}
2019-07-18 15:48:02 +08:00
$prikey = openssl_pkey_get_private($res);
2019-07-18 11:56:06 +08:00
2019-07-18 15:48:02 +08:00
if(isset($data['sign']))
{
unset($data['sign']);
2019-07-18 11:56:06 +08:00
}
2019-07-18 15:48:02 +08:00
ksort($data); //按字母升序排序
$parts = [];
foreach ($data as $k => $v) {
2019-07-18 14:36:28 +08:00
if(in_array($k, ['appKey', 'dealId', 'tpOrderId', 'totalAmount']))
{
$parts[] = $k . '=' . $v;
}
2019-07-18 11:56:06 +08:00
}
$str = implode('&', $parts);
2019-07-18 15:48:02 +08:00
openssl_sign($str, $sign, $prikey);
openssl_free_key($prikey);
2019-07-18 11:56:06 +08:00
return base64_encode($sign);
}
/**
2019-07-18 15:48:02 +08:00
* 公钥校验签名
* @author Devil
* @blog http://gong.gg/
* @version 1.0.0
* @date 2019-07-18
* @desc description
* @param [array] $data [需要验签的数据]
2019-07-18 11:56:06 +08:00
*/
2019-07-18 15:48:02 +08:00
public function CheckSignWithRsa($data)
2019-07-18 11:56:06 +08:00
{
2019-07-18 15:48:02 +08:00
if(!isset($data['rsaSign']) || empty($data))
{
2019-07-18 11:56:06 +08:00
return false;
}
2019-07-18 15:48:02 +08:00
$sign = $data['rsaSign'];
unset($data['rsaSign']);
2019-07-18 11:56:06 +08:00
2019-07-18 15:48:02 +08:00
if(empty($data))
{
2019-07-18 11:56:06 +08:00
return false;
}
2019-07-18 15:48:02 +08:00
ksort($data); //按字母升序排序
$parts = [];
foreach ($data as $k => $v) {
2019-07-18 11:56:06 +08:00
$parts[] = $k . '=' . $v;
}
$str = implode('&', $parts);
$sign = base64_decode($sign);
2019-11-28 11:32:02 +08:00
if(stripos($this->config['out_rsa_public'], '-----') === false)
{
$res = "-----BEGIN PUBLIC KEY-----\n";
$res .= wordwrap($this->config['out_rsa_public'], 64, "\n", true);
$res .= "\n-----END PUBLIC KEY-----";
} else {
$res = $this->config['out_rsa_public'];
}
2019-07-18 15:48:02 +08:00
$pubkey = openssl_pkey_get_public($res);
$result = (bool)openssl_verify($str, $sign, $pubkey);
openssl_free_key($pubkey);
2019-07-18 11:56:06 +08:00
return $result;
}
2020-07-01 22:46:52 +08:00
/**
* 自定义成功返回内容
* @author Devil
* @blog http://gong.gg/
* @version 1.0.0
* @date 2020-07-01
* @desc description
*/
public function SuccessReturn()
{
return '{"errno":0,"msg":"success","data":{"isConsumed":2}}';
}
/**
* 自定义失败返回内容
* @author Devil
* @blog http://gong.gg/
* @version 1.0.0
* @date 2020-07-01
* @desc description
*/
public function ErrorReturn()
{
return '{"errno": 0,"msg": "success","data": {"isErrorOrder": 1,"isConsumed": 2}';
}
2019-07-17 20:12:20 +08:00
}
?>