You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

159 lines
4.6 KiB

This file contains ambiguous Unicode characters!

This file contains ambiguous Unicode characters that may be confused with others in your current locale. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to highlight these characters.

<?php
namespace tencent\wechat\pay;
use EasyWeChat\Factory;
use think\Request;
class Wxpay extends Base
{
/**
* 微信支付
* @param string $openid 微信号APP该参数传空
* @param double $money 支付金额
* @param string $order_number 订单编号
* @param string $body 商品描述
* @param string $notify_url 回调函数地址
* @param string $user_agent 用户代理
* @date 2022-10-31
*/
public function createOrder($openid, $money, $order_number, $body = '', $notify_url = '', $user_agent = 'weixin')
{
switch ($user_agent) {
case 'app':
$trade_type = 'APP';
break;
case 'mp_weixin':
$trade_type = 'JSAPI';
break;
case 'weixin':
$trade_type = 'JSAPI';
break;
case 'h5':
$trade_type = 'MWEB';
$scene_info = [
'h5_info' => [
'type' => 'Wap'
]
];
break;
default:
$trade_type = '';
break;
}
$app = Factory::payment($this->wxpayConfig);
$data = [
'body' => $body,
'out_trade_no' => $order_number,
'total_fee' => (int)($money * 100),
// 'spbill_create_ip' => '123.12.12.123', // 可选如不传该参数SDK 将会自动获取相应 IP 地址
'notify_url' => $notify_url, // 支付结果通知网址,如果不设置则会使用配置里的默认地址
'trade_type' => $trade_type, // 请对应换成你的支付方式对应的值类型
];
if (!empty($openid)) {
$data['openid'] = $openid;
}
if (!empty($scene_info)) {
$data['scene_info'] = (string)json_encode($scene_info);
}
//统一下单
$result = $app->order->unify($data);
// 记录日志
$uid = defined('UID') ? UID : '';
platformLog($data, $result, 'easywechat_wxpay_create_order_uid_' . $uid);
return $result;
}
/**
* 微信支付
* @param string $prepay_id 微信号APP该参数传空
* @param string $user_agent 用户代理
* @date 2022-10-31
*/
public function wxPay($prepay_id, $user_agent = 'weixin')
{
$app = Factory::payment($this->wxpayConfig);
// 生成支付 JS 配置
$jssdk = $app->jssdk;
switch ($user_agent) {
case 'weixin':
// JSSDK
$config = $jssdk->sdkConfig($prepay_id); // 返回数组
break;
case 'mp_weixin':
// 小程序
$config = $jssdk->bridgeConfig($prepay_id, false); // 返回数组
break;
case 'app':
// 生成 APP 支付配置
$config = $jssdk->appConfig($prepay_id); // 返回数组
break;
default:
break;
}
// 记录日志
$uid = defined('UID') ? UID : '';
platformLog($prepay_id, $config, 'easywechat_wxpay_wxpay_uid_' . $uid);
return $config;
}
/**
* 验证签名
* @date 2022-03-28
*/
public function getPayClass()
{
$app = Factory::payment($this->wxpayConfig);
return $app;
}
/**
* 查询订单
* @param string 订单编号
* @date 2022-10-28
*/
public function queryByOutTradeNumber($order_number)
{
$app = Factory::payment($this->wxpayConfig);
$result = $app->order->queryByOutTradeNumber($order_number);
// 记录日志
$uid = defined('UID') ? UID : '';
platformLog($order_number, $result, 'easywechat_wxpay_query_by_out_trace_number_uid_' . $uid);
return $result;
}
/**
* 微信订单退款
* @param string $order_number 订单编号
* @param string $refund_number 商户退款单号,自己生成用于自己识别即可
* @param float $total_money 订单总金额
* @param float $refund_money 订单退款金额
* @param string $refund_desc 退款说明
* @date 2021-03-01
*/
public function refund($order_number, $refund_number, $total_money = 0, $refund_money = 0, $refund_desc = '退款-订单')
{
$app = Factory::payment($this->wxpayConfig);
//根据商户订单号退款
$result = $app->refund->byOutTradeNumber($order_number, $refund_number, (int)($total_money * 100), (int)($refund_money * 100), [
'refund_desc' => $refund_desc,
]);
return $result;
}
}