提交其他文件
This commit is contained in:
372
app/common/service/pay/AliPayService.php
Normal file
372
app/common/service/pay/AliPayService.php
Normal file
@ -0,0 +1,372 @@
|
||||
<?php
|
||||
// +----------------------------------------------------------------------
|
||||
// | likeadmin快速开发前后端分离管理后台(PHP版)
|
||||
// +----------------------------------------------------------------------
|
||||
// | 欢迎阅读学习系统程序代码,建议反馈是我们前进的动力
|
||||
// | 开源版本可自由商用,可去除界面版权logo
|
||||
// | gitee下载:https://gitee.com/likeshop_gitee/likeadmin
|
||||
// | github下载:https://github.com/likeshop-github/likeadmin
|
||||
// | 访问官网:https://www.likeadmin.cn
|
||||
// | likeadmin团队 版权所有 拥有最终解释权
|
||||
// +----------------------------------------------------------------------
|
||||
// | author: likeadminTeam
|
||||
// +----------------------------------------------------------------------
|
||||
namespace app\common\service\pay;
|
||||
|
||||
use Alipay\EasySDK\Kernel\Factory;
|
||||
use Alipay\EasySDK\Kernel\Config;
|
||||
use app\common\enum\PayEnum;
|
||||
use app\common\enum\user\UserTerminalEnum;
|
||||
use app\common\logic\PayNotifyLogic;
|
||||
use app\common\model\member\MemberOrder;
|
||||
use app\common\model\pay\PayConfig;
|
||||
use app\common\model\recharge\RechargeOrder;
|
||||
use think\facade\Log;
|
||||
|
||||
/**
|
||||
* 支付宝支付
|
||||
* Class AliPlsayService
|
||||
* @package app\common\server
|
||||
*/
|
||||
class AliPayService extends BasePayService
|
||||
{
|
||||
|
||||
/**
|
||||
* 用户客户端
|
||||
* @var
|
||||
*/
|
||||
protected $terminal;
|
||||
|
||||
/**
|
||||
* 支付实例
|
||||
* @var
|
||||
*/
|
||||
protected $pay;
|
||||
|
||||
/**
|
||||
* 初始化设置
|
||||
* AliPayService constructor.
|
||||
* @throws \Exception
|
||||
*/
|
||||
public function __construct($terminal = null)
|
||||
{
|
||||
//设置用户终端
|
||||
$this->terminal = $terminal;
|
||||
//初始化支付配置
|
||||
Factory::setOptions($this->getOptions());
|
||||
$this->pay = Factory::payment();
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @notes 支付设置
|
||||
* @return Config
|
||||
* @throws \think\db\exception\DataNotFoundException
|
||||
* @throws \think\db\exception\DbException
|
||||
* @throws \think\db\exception\ModelNotFoundException
|
||||
* @author 段誉
|
||||
* @date 2021/7/28 17:43
|
||||
*/
|
||||
public function getOptions()
|
||||
{
|
||||
$config = (new PayConfig())->where(['pay_way' => PayEnum::ALI_PAY])->find();
|
||||
if (empty($config)) {
|
||||
throw new \Exception('请配置好支付设置');
|
||||
}
|
||||
$options = new Config();
|
||||
$options->protocol = 'https';
|
||||
$options->gatewayHost = 'openapi.alipay.com';
|
||||
// $options->gatewayHost = 'openapi.alipaydev.com'; //测试沙箱地址
|
||||
$options->signType = 'RSA2';
|
||||
$options->appId = $config['config']['app_id'] ?? '';
|
||||
// 应用私钥
|
||||
$options->merchantPrivateKey = $config['config']['private_key'] ?? '';
|
||||
//接口加签方式
|
||||
// 秘钥模式
|
||||
if ($config['config']['mode'] == 'normal_mode') {
|
||||
//支付宝公钥
|
||||
$options->alipayPublicKey = $config['config']['ali_public_key'] ?? '';
|
||||
}
|
||||
|
||||
//证书模式
|
||||
if ($config['config']['mode'] == 'certificate') {
|
||||
//判断是否已经存在证书文件夹,不存在则新建
|
||||
if (!file_exists(app()->getRootPath() . 'runtime/certificate')) {
|
||||
mkdir(app()->getRootPath() . 'runtime/certificate', 0775, true);
|
||||
}
|
||||
//写入文件
|
||||
$publicCert = $config['config']['public_cert'] ?? '';
|
||||
$aliPublicCert = $config['config']['ali_public_cert'] ?? '';
|
||||
$aliRootCert = $config['config']['ali_root_cert'] ?? '';
|
||||
$publicCertPath = app()->getRootPath() . 'runtime/certificate/' . md5($publicCert) . '.crt';
|
||||
$aliPublicCertPath = app()->getRootPath() . 'runtime/certificate/' . md5($aliPublicCert) . '.crt';
|
||||
$aliRootCertPath = app()->getRootPath() . 'runtime/certificate/' . md5($aliRootCert) . '.crt';
|
||||
if (!file_exists($publicCertPath)) {
|
||||
$fopenPublicCertPath = fopen($publicCertPath, 'w');
|
||||
fwrite($fopenPublicCertPath, $publicCert);
|
||||
fclose($fopenPublicCertPath);
|
||||
}
|
||||
if (!file_exists($aliPublicCertPath)) {
|
||||
$fopenAliPublicCertPath = fopen($aliPublicCertPath, 'w');
|
||||
fwrite($fopenAliPublicCertPath, $aliPublicCert);
|
||||
fclose($fopenAliPublicCertPath);
|
||||
}
|
||||
if (!file_exists($aliRootCertPath)) {
|
||||
$fopenAliRootCertPath = fopen($aliRootCertPath, 'w');
|
||||
fwrite($fopenAliRootCertPath, $aliRootCert);
|
||||
fclose($fopenAliRootCertPath);
|
||||
}
|
||||
//应用公钥证书路径
|
||||
$options->merchantCertPath = $publicCertPath;
|
||||
//支付宝公钥证书路径
|
||||
$options->alipayCertPath = $aliPublicCertPath;
|
||||
//支付宝根证书路径
|
||||
$options->alipayRootCertPath = $aliRootCertPath;
|
||||
}
|
||||
//回调地址
|
||||
$options->notifyUrl = (string)url('pay/aliNotify', [], false, true);
|
||||
return $options;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @notes 支付
|
||||
* @param $from //订单来源;order-商品订单;recharge-充值订单
|
||||
* @param $order //订单信息
|
||||
* @return false|string[]
|
||||
* @author 段誉
|
||||
* @date 2021/8/13 17:08
|
||||
*/
|
||||
public function pay($from, $order): array|bool
|
||||
{
|
||||
try {
|
||||
$result = match ($this->terminal) {
|
||||
UserTerminalEnum::PC => $this->pagePay($from, $order),
|
||||
UserTerminalEnum::IOS, UserTerminalEnum::ANDROID => $this->appPay($from, $order),
|
||||
UserTerminalEnum::WECHAT_OA, UserTerminalEnum::H5 => $this->wapPay($from, $order),
|
||||
default => throw new \Exception('支付方式错误'),
|
||||
};
|
||||
return [
|
||||
'config' => $result,
|
||||
'pay_way' => PayEnum::ALI_PAY
|
||||
];
|
||||
} catch (\Exception $e) {
|
||||
$this->error = $e->getMessage();
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Notes: 支付回调
|
||||
* @param $data
|
||||
* @return bool
|
||||
* @author 段誉(2021/3/22 17:22)
|
||||
*/
|
||||
public function notify($data)
|
||||
{
|
||||
try {
|
||||
$verify = $this->pay->common()->verifyNotify($data);
|
||||
if (false === $verify) {
|
||||
throw new \Exception('异步通知验签失败');
|
||||
}
|
||||
if (!in_array($data['trade_status'], ['TRADE_SUCCESS', 'TRADE_FINISHED'])) {
|
||||
return true;
|
||||
}
|
||||
$extra['transaction_id'] = $data['trade_no'];
|
||||
//验证订单是否已支付
|
||||
switch ($data['passback_params']) {
|
||||
case 'recharge':
|
||||
$order = RechargeOrder::where(['sn' => $data['out_trade_no']])->findOrEmpty();
|
||||
if ($order->isEmpty() || $order->pay_status == PayEnum::ISPAID) {
|
||||
return true;
|
||||
}
|
||||
PayNotifyLogic::handle('recharge', $data['out_trade_no'], $extra);
|
||||
break;
|
||||
}
|
||||
|
||||
return true;
|
||||
} catch (\Exception $e) {
|
||||
$record = [
|
||||
__CLASS__,
|
||||
__FUNCTION__,
|
||||
$e->getFile(),
|
||||
$e->getLine(),
|
||||
$e->getMessage()
|
||||
];
|
||||
Log::write(implode('-', $record));
|
||||
$this->setError($e->getMessage());
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @notes PC支付
|
||||
* @param $attach //附加参数(在回调时会返回)
|
||||
* @param $order //订单信息
|
||||
* @return string
|
||||
* @author 段誉
|
||||
* @date 2021/7/28 17:34
|
||||
*/
|
||||
public function pagePay($attach, $order)
|
||||
{
|
||||
$domain = request()->domain();
|
||||
$result = $this->pay->page()->optional('passback_params', $attach)->pay(
|
||||
'订单:' . $order['sn'],
|
||||
$order['sn'],
|
||||
$order['order_amount'],
|
||||
$domain . $order['redirect_url']
|
||||
);
|
||||
return $result->body;
|
||||
}
|
||||
|
||||
/**
|
||||
* @notes APP支付
|
||||
* @param $attach //附加参数(在回调时会返回)
|
||||
* @param $order //订单信息
|
||||
* @return string
|
||||
* @author 段誉
|
||||
* @date 2021/7/28 17:34
|
||||
*/
|
||||
public function appPay($attach, $order)
|
||||
{
|
||||
$result = $this->pay->app()->optional('passback_params', $attach)->pay(
|
||||
$order['sn'],
|
||||
$order['sn'],
|
||||
$order['order_amount']
|
||||
);
|
||||
return $result->body;
|
||||
}
|
||||
|
||||
/**
|
||||
* @notes 手机网页支付
|
||||
* @param $attach //附加参数(在回调时会返回)
|
||||
* @param $order //订单信息
|
||||
* @return string
|
||||
* @author 段誉
|
||||
* @date 2021/7/28 17:34
|
||||
*/
|
||||
public function wapPay($attach, $order)
|
||||
{
|
||||
$domain = request()->domain();
|
||||
$url = $domain . '/mobile' . $order['redirect_url'] .'?id=' . $order['id'] . '&from='. $attach . '&checkPay=true';;
|
||||
$result = $this->pay->wap()->optional('passback_params', $attach)->pay(
|
||||
'订单:' . $order['sn'],
|
||||
$order['sn'],
|
||||
$order['order_amount'],
|
||||
$url,
|
||||
$url
|
||||
);
|
||||
return $result->body;
|
||||
}
|
||||
|
||||
/**
|
||||
* @notes 查询订单
|
||||
* @param $orderSn
|
||||
* @return \Alipay\EasySDK\Payment\Common\Models\AlipayTradeQueryResponse
|
||||
* @throws \Exception
|
||||
* @author 段誉
|
||||
* @date 2021/7/28 17:36
|
||||
*/
|
||||
public function checkPay($orderSn)
|
||||
{
|
||||
return $this->pay->common()->query($orderSn);
|
||||
}
|
||||
|
||||
/**
|
||||
* @notes 退款
|
||||
* @param $orderSn
|
||||
* @param $orderAmount
|
||||
* @param $outRequestNo
|
||||
* @return \Alipay\EasySDK\Payment\Common\Models\AlipayTradeRefundResponse
|
||||
* @throws \Exception
|
||||
* @author 段誉
|
||||
* @date 2021/7/28 17:37
|
||||
*/
|
||||
public function refund($orderSn, $orderAmount, $outRequestNo)
|
||||
{
|
||||
return $this->pay->common()->optional('out_request_no', $outRequestNo)->refund($orderSn, $orderAmount);
|
||||
}
|
||||
|
||||
/**
|
||||
* @notes 查询退款
|
||||
* @author Tab
|
||||
* @date 2021/9/13 11:38
|
||||
*/
|
||||
public function queryRefund($orderSn, $refundSn)
|
||||
{
|
||||
return $this->pay->common()->queryRefund($orderSn, $refundSn);
|
||||
}
|
||||
|
||||
/**
|
||||
* @notes 捕获错误
|
||||
* @param $result
|
||||
* @throws \Exception
|
||||
* @author 段誉
|
||||
* @date 2023/2/28 12:09
|
||||
*/
|
||||
public function checkResultFail($result)
|
||||
{
|
||||
if (isset($result['alipay_trade_precreate_response']['code']) && 10000 != $result['alipay_trade_precreate_response']['code']) {
|
||||
throw new \Exception('支付宝:' . $result['alipay_trade_precreate_response']['msg']);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @notes 转账到支付宝账号
|
||||
* @param $withdraw
|
||||
* @return mixed
|
||||
* @throws \think\db\exception\DataNotFoundException
|
||||
* @throws \think\db\exception\DbException
|
||||
* @throws \think\db\exception\ModelNotFoundException
|
||||
* @author ljj
|
||||
* @date 2023/10/9 10:58 上午
|
||||
*/
|
||||
public function transfer($withdraw)
|
||||
{
|
||||
//请求参数
|
||||
$data = [
|
||||
'out_biz_no' => $withdraw['sn'],//商家侧唯一订单号,由商家自定义。对于不同转账请求,商家需保证该订单号在自身系统唯一。
|
||||
'trans_amount' => $withdraw['left_money'],//订单总金额,单位为元,不支持千位分隔符,精确到小数点后两位
|
||||
'product_code' => 'TRANS_ACCOUNT_NO_PWD',//销售产品码。单笔无密转账固定为 TRANS_ACCOUNT_NO_PWD。
|
||||
'biz_scene' => 'DIRECT_TRANSFER',//业务场景。单笔无密转账固定为 DIRECT_TRANSFER。
|
||||
'order_title' => '佣金提现',//转账业务的标题
|
||||
'payee_info' => [//收款方信息
|
||||
'identity' => $withdraw['account'],//参与方的标识 ID。当 identity_type=ALIPAY_USER_ID 时,填写支付宝用户 UID;当 identity_type=ALIPAY_LOGON_ID 时,填写支付宝登录号。
|
||||
'identity_type' => 'ALIPAY_LOGON_ID',//参与方的标识类型。ALIPAY_USER_ID:支付宝会员的用户 ID;ALIPAY_LOGON_ID:支付宝登录号;
|
||||
'name' => $withdraw['real_name'],//参与方真实姓名。如果非空,将校验收款支付宝账号姓名一致性。当 identity_type=ALIPAY_LOGON_ID 时,本字段必填。
|
||||
],
|
||||
'remark' => '',//业务备注
|
||||
];
|
||||
|
||||
$result = Factory::util()->generic()->execute("alipay.fund.trans.uni.transfer", [], $data);
|
||||
$result = json_decode($result->httpBody, true);
|
||||
$result = $result['alipay_fund_trans_uni_transfer_response'] ?? [];
|
||||
if ($result['code'] != 10000) {//接口调用失败
|
||||
throw new \Exception($result['sub_msg'] ?? $result['msg']);
|
||||
}
|
||||
return $result;
|
||||
}
|
||||
|
||||
/**
|
||||
* @notes 转账查询
|
||||
* @param $withdraw
|
||||
* @return mixed
|
||||
* @throws \Exception
|
||||
* @author ljj
|
||||
* @date 2023/10/9 11:47 上午
|
||||
*/
|
||||
public function transferQuery($withdraw)
|
||||
{
|
||||
//请求参数
|
||||
$data = [
|
||||
'out_biz_no' => $withdraw['sn'],//商户转账唯一订单号:发起转账来源方定义的转账单据 ID。
|
||||
'product_code' => 'TRANS_ACCOUNT_NO_PWD',//销售产品码,如果传了 out_biz_no,则该字段必传。单笔无密转账固定为TRANS_ACCOUNT_NO_PWD。
|
||||
'biz_scene' => 'DIRECT_TRANSFER',//描述特定的业务场景,如果传递了out_biz_no 则该字段为必传。单笔无密转账固定为DIRECT_TRANSFER。
|
||||
];
|
||||
|
||||
$result = Factory::util()->generic()->execute("alipay.fund.trans.common.query", [], $data);
|
||||
$result = json_decode($result->httpBody, true);
|
||||
return $result['alipay_fund_trans_common_query_response'] ?? [];
|
||||
}
|
||||
}
|
||||
|
||||
98
app/common/service/pay/BasePayService.php
Normal file
98
app/common/service/pay/BasePayService.php
Normal file
@ -0,0 +1,98 @@
|
||||
<?php
|
||||
// +----------------------------------------------------------------------
|
||||
// | likeadmin快速开发前后端分离管理后台(PHP版)
|
||||
// +----------------------------------------------------------------------
|
||||
// | 欢迎阅读学习系统程序代码,建议反馈是我们前进的动力
|
||||
// | 开源版本可自由商用,可去除界面版权logo
|
||||
// | gitee下载:https://gitee.com/likeshop_gitee/likeadmin
|
||||
// | github下载:https://github.com/likeshop-github/likeadmin
|
||||
// | 访问官网:https://www.likeadmin.cn
|
||||
// | likeadmin团队 版权所有 拥有最终解释权
|
||||
// +----------------------------------------------------------------------
|
||||
// | author: likeadminTeam
|
||||
// +----------------------------------------------------------------------
|
||||
|
||||
|
||||
namespace app\common\service\pay;
|
||||
|
||||
|
||||
use think\facade\Log;
|
||||
|
||||
class BasePayService
|
||||
{
|
||||
/**
|
||||
* 错误信息
|
||||
* @var string
|
||||
*/
|
||||
protected $error;
|
||||
|
||||
/**
|
||||
* 返回状态码
|
||||
* @var int
|
||||
*/
|
||||
protected $returnCode = 0;
|
||||
|
||||
|
||||
/**
|
||||
* @notes 获取错误信息
|
||||
* @return string
|
||||
* @author 段誉
|
||||
* @date 2021/7/21 18:23
|
||||
*/
|
||||
public function getError()
|
||||
{
|
||||
if (false === self::hasError()) {
|
||||
return '系统错误';
|
||||
}
|
||||
return $this->error;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @notes 设置错误信息
|
||||
* @param $error
|
||||
* @author 段誉
|
||||
* @date 2021/7/21 18:20
|
||||
*/
|
||||
public function setError($error)
|
||||
{
|
||||
$this->error = $error;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @notes 是否存在错误
|
||||
* @return bool
|
||||
* @author 段誉
|
||||
* @date 2021/7/21 18:32
|
||||
*/
|
||||
public function hasError()
|
||||
{
|
||||
return !empty($this->error);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @notes 设置状态码
|
||||
* @param $code
|
||||
* @author 段誉
|
||||
* @date 2021/7/28 17:05
|
||||
*/
|
||||
public function setReturnCode($code)
|
||||
{
|
||||
$this->returnCode = $code;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @notes 特殊场景返回指定状态码,默认为0
|
||||
* @return int
|
||||
* @author 段誉
|
||||
* @date 2021/7/28 15:14
|
||||
*/
|
||||
public function getReturnCode()
|
||||
{
|
||||
return $this->returnCode;
|
||||
}
|
||||
|
||||
}
|
||||
475
app/common/service/pay/WeChatPayService.php
Normal file
475
app/common/service/pay/WeChatPayService.php
Normal file
@ -0,0 +1,475 @@
|
||||
<?php
|
||||
// +----------------------------------------------------------------------
|
||||
// | likeadmin快速开发前后端分离管理后台(PHP版)
|
||||
// +----------------------------------------------------------------------
|
||||
// | 欢迎阅读学习系统程序代码,建议反馈是我们前进的动力
|
||||
// | 开源版本可自由商用,可去除界面版权logo
|
||||
// | gitee下载:https://gitee.com/likeshop_gitee/likeadmin
|
||||
// | github下载:https://github.com/likeshop-github/likeadmin
|
||||
// | 访问官网:https://www.likeadmin.cn
|
||||
// | likeadmin团队 版权所有 拥有最终解释权
|
||||
// +----------------------------------------------------------------------
|
||||
// | author: likeadminTeam
|
||||
// +----------------------------------------------------------------------
|
||||
|
||||
|
||||
namespace app\common\service\pay;
|
||||
|
||||
|
||||
use app\common\enum\PayEnum;
|
||||
use app\common\enum\user\UserTerminalEnum;
|
||||
use app\common\logic\PayNotifyLogic;
|
||||
use app\common\model\order\Order;
|
||||
use app\common\model\order\OrderGroup;
|
||||
use app\common\model\order\OrderMember;
|
||||
use app\common\model\order\OrderStore;
|
||||
use app\common\model\order\OrderStoreRenew;
|
||||
use app\common\model\recharge\RechargeOrder;
|
||||
use app\common\model\refund\RefundLog;
|
||||
use app\common\model\refund\RefundRecord;
|
||||
use app\common\model\user\UserAuth;
|
||||
use app\common\service\wechat\WeChatConfigService;
|
||||
use app\common\model\order\OrderTeamaster;
|
||||
use app\common\model\order\OrderStoreRecharge;
|
||||
use app\common\model\order\OrderTeamasterRenew;
|
||||
use EasyWeChat\Pay\Application;
|
||||
use EasyWeChat\Pay\Message;
|
||||
use think\facade\Log;
|
||||
|
||||
|
||||
/**
|
||||
* 微信支付
|
||||
* Class WeChatPayService
|
||||
* @package app\common\server
|
||||
*/
|
||||
class WeChatPayService extends BasePayService
|
||||
{
|
||||
/**
|
||||
* 授权信息
|
||||
* @var UserAuth|array|\think\Model
|
||||
*/
|
||||
protected $auth;
|
||||
|
||||
|
||||
/**
|
||||
* 微信配置
|
||||
* @var
|
||||
*/
|
||||
protected $config;
|
||||
|
||||
|
||||
/**
|
||||
* easyWeChat实例
|
||||
* @var
|
||||
*/
|
||||
protected $app;
|
||||
|
||||
|
||||
/**
|
||||
* 当前使用客户端
|
||||
* @var
|
||||
*/
|
||||
protected $terminal;
|
||||
|
||||
|
||||
/**
|
||||
* 初始化微信支付配置
|
||||
* @param $terminal //用户终端
|
||||
* @param null $userId //用户id(获取授权openid)
|
||||
*/
|
||||
public function __construct($terminal, $userId = null)
|
||||
{
|
||||
$this->terminal = $terminal;
|
||||
$this->config = WeChatConfigService::getPayConfigByTerminal($terminal);
|
||||
$this->app = new Application($this->config);
|
||||
if ($userId !== null) {
|
||||
$this->auth = UserAuth::where(['user_id' => $userId, 'terminal' => $terminal])->findOrEmpty();
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @notes 发起微信支付统一下单
|
||||
* @param $from
|
||||
* @param $order
|
||||
* @return array|false|string
|
||||
* @author 段誉
|
||||
* @date 2021/8/4 15:05
|
||||
*/
|
||||
public function pay($order_type,$from, $order)
|
||||
{
|
||||
try {
|
||||
switch ($this->terminal) {
|
||||
case UserTerminalEnum::WECHAT_MMP:
|
||||
$config = WeChatConfigService::getMnpConfig();
|
||||
$result = $this->jsapiPay($order_type,$from, $order, $config['app_id']);
|
||||
break;
|
||||
case UserTerminalEnum::WECHAT_OA:
|
||||
$config = WeChatConfigService::getOaConfig();
|
||||
$result = $this->jsapiPay($from, $order, $config['app_id']);
|
||||
break;
|
||||
case UserTerminalEnum::IOS:
|
||||
case UserTerminalEnum::ANDROID:
|
||||
$config = WeChatConfigService::getOpConfig();
|
||||
$result = $this->appPay($from, $order, $config['app_id']);
|
||||
break;
|
||||
case UserTerminalEnum::H5:
|
||||
$config = WeChatConfigService::getOaConfig();
|
||||
$result = $this->mwebPay($from, $order, $config['app_id']);
|
||||
break;
|
||||
case UserTerminalEnum::PC:
|
||||
$config = WeChatConfigService::getOaConfig();
|
||||
$result = $this->nativePay($from, $order, $config['app_id']);
|
||||
break;
|
||||
default:
|
||||
throw new \Exception('支付方式错误');
|
||||
}
|
||||
|
||||
return [
|
||||
'config' => $result,
|
||||
'pay_way' => PayEnum::WECHAT_PAY
|
||||
];
|
||||
} catch (\Exception $e) {
|
||||
$this->setError($e->getMessage());
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @notes jsapiPay
|
||||
* @param $from
|
||||
* @param $order
|
||||
* @param $appId
|
||||
* @return mixed
|
||||
* @throws \EasyWeChat\Kernel\Exceptions\InvalidArgumentException
|
||||
* @throws \EasyWeChat\Kernel\Exceptions\InvalidConfigException
|
||||
* @author 段誉
|
||||
* @date 2023/2/28 12:12
|
||||
*/
|
||||
public function jsapiPay($order_type,$from, $order, $appId)
|
||||
{
|
||||
$response = $this->app->getClient()->postJson("v3/pay/transactions/jsapi", [
|
||||
"appid" => $appId,
|
||||
"mchid" => $this->config['mch_id'],
|
||||
"description" => $this->payDesc($from),
|
||||
"out_trade_no" => $order['pay_sn'],
|
||||
"notify_url" => $this->config['notify_url'],
|
||||
"amount" => [
|
||||
"total" => intval($order['order_amount'] * 100),
|
||||
],
|
||||
"payer" => [
|
||||
"openid" => $this->auth['openid']
|
||||
],
|
||||
'attach' => json_encode([
|
||||
'order_type' => $order_type,
|
||||
'from' => $from
|
||||
])
|
||||
]);
|
||||
|
||||
$result = $response->toArray(false);
|
||||
$this->checkResultFail($result);
|
||||
return $this->getPrepayConfig($result['prepay_id'], $appId);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @notes 网站native
|
||||
* @param $from
|
||||
* @param $order
|
||||
* @param $appId
|
||||
* @return mixed
|
||||
* @throws \EasyWeChat\Kernel\Exceptions\InvalidArgumentException
|
||||
* @throws \EasyWeChat\Kernel\Exceptions\InvalidConfigException
|
||||
* @author 段誉
|
||||
* @date 2023/2/28 12:12
|
||||
*/
|
||||
public function nativePay($from, $order, $appId)
|
||||
{
|
||||
$response = $this->app->getClient()->postJson('v3/pay/transactions/native', [
|
||||
'appid' => $appId,
|
||||
'mchid' => $this->config['mch_id'],
|
||||
'description' => $this->payDesc($from),
|
||||
'out_trade_no' => $order['pay_sn'],
|
||||
'notify_url' => $this->config['notify_url'],
|
||||
'amount' => [
|
||||
'total' => intval($order['order_amount'] * 100),
|
||||
],
|
||||
'attach' => $from
|
||||
]);
|
||||
$result = $response->toArray(false);
|
||||
$this->checkResultFail($result);
|
||||
return $result['code_url'];
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @notes appPay
|
||||
* @param $from
|
||||
* @param $order
|
||||
* @param $appId
|
||||
* @return mixed
|
||||
* @throws \EasyWeChat\Kernel\Exceptions\InvalidArgumentException
|
||||
* @throws \EasyWeChat\Kernel\Exceptions\InvalidConfigException
|
||||
* @author 段誉
|
||||
* @date 2023/2/28 12:12
|
||||
*/
|
||||
public function appPay($from, $order, $appId)
|
||||
{
|
||||
$response = $this->app->getClient()->postJson('v3/pay/transactions/app', [
|
||||
'appid' => $appId,
|
||||
'mchid' => $this->config['mch_id'],
|
||||
'description' => $this->payDesc($from),
|
||||
'out_trade_no' => $order['pay_sn'],
|
||||
'notify_url' => $this->config['notify_url'],
|
||||
'amount' => [
|
||||
'total' => intval($order['order_amount'] * 100),
|
||||
],
|
||||
'attach' => $from
|
||||
]);
|
||||
$result = $response->toArray(false);
|
||||
$this->checkResultFail($result);
|
||||
return $result['prepay_id'];
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @notes h5
|
||||
* @param $from
|
||||
* @param $order
|
||||
* @param $appId
|
||||
* @param $redirectUrl
|
||||
* @return mixed
|
||||
* @throws \EasyWeChat\Kernel\Exceptions\InvalidArgumentException
|
||||
* @throws \EasyWeChat\Kernel\Exceptions\InvalidConfigException
|
||||
* @author 段誉
|
||||
* @date 2023/2/28 12:13
|
||||
*/
|
||||
public function mwebPay($from, $order, $appId)
|
||||
{
|
||||
$ip = request()->ip();
|
||||
if (!empty(env('project.test_web_ip')) && env('APP_DEBUG')) {
|
||||
$ip = env('project.test_web_ip');
|
||||
}
|
||||
|
||||
$response = $this->app->getClient()->postJson('v3/pay/transactions/h5', [
|
||||
'appid' => $appId,
|
||||
'mchid' => $this->config['mch_id'],
|
||||
'description' => $this->payDesc($from),
|
||||
'out_trade_no' => $order['pay_sn'],
|
||||
'notify_url' => $this->config['notify_url'],
|
||||
'amount' => [
|
||||
'total' => intval(strval($order['order_amount'] * 100)),
|
||||
],
|
||||
'attach' => $from,
|
||||
'scene_info' => [
|
||||
'payer_client_ip' => $ip,
|
||||
'h5_info' => [
|
||||
'type' => 'Wap',
|
||||
]
|
||||
]
|
||||
]);
|
||||
$result = $response->toArray(false);
|
||||
$this->checkResultFail($result);
|
||||
|
||||
$domain = request()->domain();
|
||||
if (!empty(env('project.test_web_domain')) && env('APP_DEBUG')) {
|
||||
$domain = env('project.test_web_domain');
|
||||
}
|
||||
$redirectUrl = $domain . '/mobile'. $order['redirect_url'] .'?id=' . $order['id'] . '&from='. $from . '&checkPay=true';
|
||||
return $result['h5_url'] . '&redirect_url=' . urlencode($redirectUrl);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @notes 退款
|
||||
* @param array $refundData
|
||||
* @return mixed
|
||||
* @throws \EasyWeChat\Kernel\Exceptions\InvalidArgumentException
|
||||
* @throws \EasyWeChat\Kernel\Exceptions\InvalidConfigException
|
||||
* @author 段誉
|
||||
* @date 2023/2/28 16:53
|
||||
*/
|
||||
public function refund(array $refundData)
|
||||
{
|
||||
$response = $this->app->getClient()->postJson('v3/refund/domestic/refunds', [
|
||||
'transaction_id' => $refundData['transaction_id'],
|
||||
'out_refund_no' => $refundData['refund_sn'],
|
||||
"notify_url" =>$refundData['notify_url'],
|
||||
'amount' => [
|
||||
'refund' => intval($refundData['total_amount'] * 100),
|
||||
'total' => intval($refundData['total_amount'] * 100),
|
||||
'currency' => 'CNY',
|
||||
]
|
||||
]);
|
||||
$result = $response->toArray(false);
|
||||
$this->checkResultFail($result);
|
||||
return $result;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @notes 查询退款
|
||||
* @param $refundSn
|
||||
* @throws \EasyWeChat\Kernel\Exceptions\InvalidArgumentException
|
||||
* @throws \EasyWeChat\Kernel\Exceptions\InvalidConfigException
|
||||
* @author 段誉
|
||||
* @date 2023/3/1 11:16
|
||||
*/
|
||||
public function queryRefund($refundSn)
|
||||
{
|
||||
$response = $this->app->getClient()->get("v3/refund/domestic/refunds/{$refundSn}");
|
||||
return $response->toArray(false);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @notes 支付描述
|
||||
* @param $from
|
||||
* @return string
|
||||
* @author 段誉
|
||||
* @date 2023/2/27 17:54
|
||||
*/
|
||||
public function payDesc($from)
|
||||
{
|
||||
$desc = [
|
||||
'order' => '商品',
|
||||
'recharge' => '充值',
|
||||
];
|
||||
return $desc[$from] ?? '商品';
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @notes 捕获错误
|
||||
* @param $result
|
||||
* @throws \Exception
|
||||
* @author 段誉
|
||||
* @date 2023/2/28 12:09
|
||||
*/
|
||||
public function checkResultFail($result)
|
||||
{
|
||||
if (!empty($result['code']) || !empty($result['message'])) {
|
||||
throw new \Exception('微信:'. $result['code'] . '-' . $result['message']);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @notes 预支付配置
|
||||
* @param $prepayId
|
||||
* @param $appId
|
||||
* @return mixed[]
|
||||
* @throws \EasyWeChat\Kernel\Exceptions\InvalidArgumentException
|
||||
* @throws \EasyWeChat\Kernel\Exceptions\InvalidConfigException
|
||||
* @author 段誉
|
||||
* @date 2023/2/28 17:38
|
||||
*/
|
||||
public function getPrepayConfig($prepayId, $appId)
|
||||
{
|
||||
return $this->app->getUtils()->buildBridgeConfig($prepayId, $appId);
|
||||
}
|
||||
public function notifyRefundMnp(){
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* @notes 支付回调
|
||||
* @return \Psr\Http\Message\ResponseInterface
|
||||
* @throws \EasyWeChat\Kernel\Exceptions\InvalidArgumentException
|
||||
* @throws \EasyWeChat\Kernel\Exceptions\RuntimeException
|
||||
* @throws \ReflectionException
|
||||
* @throws \Throwable
|
||||
* @author 段誉
|
||||
* @date 2023/2/28 14:20
|
||||
*/
|
||||
public function notify()
|
||||
{
|
||||
$server = $this->app->getServer();
|
||||
// 支付通知
|
||||
Log::write("支付日志",'2222222222222222222222222222222');
|
||||
$server->handlePaid(function (Message $message) {
|
||||
|
||||
if ($message['trade_state'] === 'SUCCESS') {
|
||||
$extra['transaction_id'] = $message['transaction_id'];
|
||||
$attach = json_decode($message['attach'],true);
|
||||
$message['out_trade_no'] = mb_substr($message['out_trade_no'], 0, 18);
|
||||
switch ($attach['from']) {
|
||||
case 'recharge':
|
||||
$order = RechargeOrder::where(['order_sn' => $message['out_trade_no']])->findOrEmpty();
|
||||
if($order->isEmpty() || $order->pay_status == PayEnum::ISPAID) {
|
||||
return true;
|
||||
}
|
||||
PayNotifyLogic::handle('recharge', $message['out_trade_no'], $extra);
|
||||
break;
|
||||
case 'wx':
|
||||
$name = "";
|
||||
if($attach['order_type'] == 0){
|
||||
$order = Order::where("order_sn",$message['out_trade_no'])->findOrEmpty();
|
||||
$name = "order";
|
||||
}elseif($attach['order_type'] == 1){
|
||||
$order = OrderStore::where("order_sn",$message['out_trade_no'])->findOrEmpty();
|
||||
$name = "orderStore";
|
||||
}elseif($attach['order_type'] == 2) {
|
||||
$order = OrderGroup::where("order_sn", $message['out_trade_no'])->findOrEmpty();
|
||||
$name = "orderGroup";
|
||||
}elseif($attach['order_type'] == 3){
|
||||
$order = OrderMember::where("order_sn", $message['out_trade_no'])->findOrEmpty();
|
||||
$name = "orderMember";
|
||||
}elseif($attach['order_type'] == 7){
|
||||
$order = OrderStoreRenew::where("order_sn", $message['out_trade_no'])->findOrEmpty();
|
||||
$name = "orderStoreRenew";
|
||||
}elseif($attach['order_type'] == 9){
|
||||
$order = OrderStoreRecharge::where("order_sn", $message['out_trade_no'])->findOrEmpty();
|
||||
$name = "OrderStoreRecharge";
|
||||
}elseif($attach['order_type'] == 10){
|
||||
$order = OrderTeamaster::where("order_sn", $message['out_trade_no'])->findOrEmpty();
|
||||
$name = "orderTeamaster";
|
||||
}elseif($attach['order_type'] == 11){
|
||||
Log::write("支付日志",'33333333');
|
||||
$order = OrderTeamasterRenew::where("order_sn", $message['out_trade_no'])->findOrEmpty();
|
||||
$name = "orderTeamaRenw";
|
||||
}
|
||||
if($order->isEmpty() || $order->pay_status == PayEnum::ISPAID) {
|
||||
return true;
|
||||
}
|
||||
if($name != ""){
|
||||
PayNotifyLogic::handle($name, $message['out_trade_no'], $extra);
|
||||
}
|
||||
break;
|
||||
}
|
||||
}
|
||||
return true;
|
||||
});
|
||||
|
||||
// 退款通知
|
||||
$server->handleRefunded(function (Message $message) {
|
||||
if($message['refund_status']=='SUCCESS'){
|
||||
Log::write("回调参数",json_encode($message['out_trade_no']));
|
||||
$message['out_trade_no'] = mb_substr($message['out_trade_no'], 0, 18);
|
||||
$extra['transaction_id'] = $message['transaction_id'];
|
||||
$refund_record = RefundRecord::where("source_sn",$message['out_trade_no'])->find();
|
||||
if($refund_record['order_type'] == 5){
|
||||
$order = OrderGroup::where("order_sn",$refund_record['source_sn'])->findOrEmpty();
|
||||
$name = "refundOrderGroup";
|
||||
}
|
||||
if($refund_record['order_type'] == 1){
|
||||
$order = OrderStore::where("order_sn",$refund_record['source_sn'])->findOrEmpty();
|
||||
$name = "refundOrderStore";
|
||||
}if($refund_record['order_type'] == 10){
|
||||
$order = OrderTeamaster::where("order_sn",$refund_record['source_sn'])->findOrEmpty();
|
||||
$name = "teamasterOrderRefund";
|
||||
}
|
||||
if($order->isEmpty() || $order->pay_status == 6) {
|
||||
return true;
|
||||
}
|
||||
if($name != ""){
|
||||
PayNotifyLogic::handle($name, $message['out_trade_no'], $extra);
|
||||
}
|
||||
}
|
||||
return true;
|
||||
});
|
||||
return $server->serve();
|
||||
}
|
||||
|
||||
}
|
||||
138
app/common/service/pay/WxRefundService.php
Normal file
138
app/common/service/pay/WxRefundService.php
Normal file
@ -0,0 +1,138 @@
|
||||
<?php
|
||||
|
||||
namespace app\common\service\pay;
|
||||
|
||||
use app\common\model\order\OrderTeamaster;
|
||||
use app\common\model\order\OrderTeamasterRenew;
|
||||
use app\common\model\refund\RefundRecord;
|
||||
use app\common\model\teamaster\TeamasterAccountLog;
|
||||
use app\common\model\user\UserCoupon;
|
||||
use app\common\service\FileService;
|
||||
use app\common\service\pay\WeChatPayService;
|
||||
use app\common\model\teamaster\TeamasterUser;
|
||||
use app\common\model\user\User;
|
||||
|
||||
class WxRefundService
|
||||
{
|
||||
|
||||
public function Refund($order,$order_type,$type){
|
||||
|
||||
switch ($order_type) {
|
||||
case '10':
|
||||
self::teamasterRefund($order,$type);
|
||||
break;
|
||||
|
||||
default:
|
||||
|
||||
}
|
||||
|
||||
return [];
|
||||
}
|
||||
|
||||
public static function teamasterRefund($order,$type){
|
||||
|
||||
if($order->user_coupon_id !==0){
|
||||
$r = UserCoupon::where("id",$order['user_coupon_id'])->update(['status'=>0]);
|
||||
}
|
||||
$refund_pricet = $order->order_amount;
|
||||
if($order->order_status >=30){
|
||||
$refund_pricet = $order->order_amount - $order->mileage_server_price;
|
||||
$refund_pricet = $refund_pricet-($order->server_all_price*0.3);
|
||||
|
||||
}
|
||||
|
||||
$recordSn = generate_sn(RefundRecord::class, 'order_sn');
|
||||
$record = RefundRecord::create([
|
||||
'order_sn' => $recordSn,
|
||||
'user_id' => $order->user_id,
|
||||
'store_id'=>0,
|
||||
'order_id' => $order->id,
|
||||
'content_id'=>0,//关联相关id
|
||||
'source_sn' => $order['order_sn'],
|
||||
'order_type' => 10,
|
||||
'order_amount' =>$refund_pricet,
|
||||
'refund_amount' => $refund_pricet,
|
||||
'refund_type' => 1,
|
||||
'refund_way' => 1
|
||||
]);
|
||||
|
||||
switch ($order->pay_way) {
|
||||
case '1':
|
||||
self::teamasterOrderRefund($order,$type);
|
||||
break;
|
||||
case '2':
|
||||
$refundData = [
|
||||
'transaction_id'=>$order->transaction_id,
|
||||
'refund_sn'=>$recordSn,
|
||||
'total_amount'=>$refund_pricet,
|
||||
'notify_url'=>'https://76458.com/'.('api/pay/notifyMnp'),
|
||||
];
|
||||
$payService = (new WeChatPayService(1, $user_id ?? null));
|
||||
$result = $payService->refund($refundData);
|
||||
if($result['status'] !=='PROCESSING'){
|
||||
throw new \Exception('押金退款失败请联系客服');
|
||||
}
|
||||
return true;
|
||||
default:
|
||||
|
||||
}
|
||||
return [];
|
||||
}
|
||||
|
||||
public static function teamasterOrderRefund($order,$type){
|
||||
|
||||
$data = [
|
||||
'uptime'=>time(),
|
||||
'pay_status'=>3
|
||||
];
|
||||
if($order->order_status ==28||$order->order_status ==29){
|
||||
$data['order_status'] = $type;
|
||||
$data['refund_time'] = time();
|
||||
$data['refund_price'] = $order->order_amount;
|
||||
}elseif($order->order_status ==30){
|
||||
$data['order_status'] = 42;
|
||||
$data['refund_time'] = time();
|
||||
$refund_pricet = $order->order_amount - $order->mileage_server_price;
|
||||
$refund_pricet = $refund_pricet-($order->server_all_price*0.3);
|
||||
$data['refund_price'] = $refund_pricet;
|
||||
$data['team_income_price'] = (($order->server_all_price*0.3)*0.65)+$order->mileage_server_price;
|
||||
$user_team =TeamasterUser::where('id',$order->team_user_id)->find();
|
||||
$user_money = $user_team['user_money']+$data['team_income_price'];
|
||||
$user_team->user_money = $user_money;
|
||||
$user_team->save();
|
||||
|
||||
$account_log = [
|
||||
'team_user_id'=>$order->team_user_id,
|
||||
'user_id'=>$order->user_id,
|
||||
'change_object'=>1,
|
||||
'change_type'=>1,
|
||||
'action'=>1,
|
||||
'amount'=>$data['team_income_price'],
|
||||
'before_amount'=>$user_team->user_money,
|
||||
'after_amount'=>$user_team->user_money+$data['team_income_price'],
|
||||
'source_sn'=>$order->order_sn,
|
||||
'sub_sn'=>$order->order_sn,
|
||||
'remark'=>'出发后用户退款——收取30%服务费和全额车马费',
|
||||
'create_time'=>time()
|
||||
];
|
||||
self::getTeamasterAccountLog($account_log);
|
||||
}
|
||||
|
||||
$user = User::where('id',$order->user_id)->find();
|
||||
$money = $user->user_money+$data['refund_price'];
|
||||
|
||||
$user->user_money =$money;
|
||||
$user->save();
|
||||
OrderTeamaster::where('id',$order->id)->update($data);
|
||||
return [];
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
public static function getTeamasterAccountLog($account_log){
|
||||
TeamasterAccountLog::create($account_log);
|
||||
return [];
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user