168 lines
5.5 KiB
PHP
168 lines
5.5 KiB
PHP
<?php
|
|
|
|
namespace app\common\service;
|
|
|
|
namespace app\common\service;
|
|
|
|
use EasyWeChat\Factory;
|
|
|
|
use think\Log;
|
|
|
|
class EasyWeChatService
|
|
{
|
|
public $config;
|
|
public $app;
|
|
public $payApp;
|
|
|
|
public function __construct()
|
|
{
|
|
$this->config = config('common.wechat');
|
|
$this->app = Factory::miniProgram($this->config);
|
|
$this->payApp = Factory::payment($this->config);
|
|
}
|
|
|
|
public function login($code)
|
|
{
|
|
$res = $this->app->auth->session($code);
|
|
Log::info('login-res' . json_encode([$res]));
|
|
return $res;
|
|
}
|
|
|
|
|
|
/**
|
|
* @param $session
|
|
* @param $iv
|
|
* @param $encryptedData
|
|
* @return array
|
|
*/
|
|
public function decryData($session, $iv, $encryptedData)
|
|
{
|
|
$decryptedData = $this->app->encryptor->decryptData($session, $iv, $encryptedData);
|
|
|
|
return $decryptedData;
|
|
}
|
|
|
|
/**
|
|
* 统一下单接口
|
|
* @param $body
|
|
* @param $out_trade_no
|
|
* @param $total_fee float 单位元
|
|
* @param $openid
|
|
* @param string $notify_url
|
|
* @return array|\EasyWeChat\Kernel\Support\Collection|object|\Psr\Http\Message\ResponseInterface|string
|
|
*/
|
|
public function unify($body, $out_trade_no, $total_fee, $openid, $notify_url = '')
|
|
{
|
|
$params = [
|
|
'body' => $body,
|
|
'out_trade_no' => $out_trade_no,
|
|
'total_fee' => $total_fee * 100, //记得转为分
|
|
'spbill_create_ip' => '', // 可选,如不传该参数,SDK 将会自动获取相应 IP 地址
|
|
'notify_url' => $notify_url ?: null, // 支付结果通知网址,如果不设置则会使用配置里的默认地址
|
|
'trade_type' => 'JSAPI', // 请对应换成你的支付方式对应的值类型
|
|
'openid' => $openid,
|
|
];
|
|
Log::info('unify-params' . json_encode($params));
|
|
$result = $this->payApp->order->unify($params);
|
|
Log::info('unify-res' . json_encode($result));
|
|
|
|
return $result;
|
|
}
|
|
|
|
/**
|
|
* @param $prepayId
|
|
* @return array|string
|
|
*/
|
|
public function sdkConfig($prepayId)
|
|
{
|
|
$jssdk = $this->payApp->jssdk;
|
|
$config = $jssdk->bridgeConfig($prepayId, false); // 返回数组
|
|
Log::info('bridgeConfig-res' . json_encode($config));
|
|
return $config;
|
|
}
|
|
|
|
/**
|
|
* 发送数据消息
|
|
* @param $order_id
|
|
* @param $user_id
|
|
* @param $template_id
|
|
* @param array $msgData
|
|
*/
|
|
public function sendMsg($user_id, $template_id, array $msgData = [])
|
|
{
|
|
$data = SubscribeMessageService::packMsg($user_id, $template_id, $msgData);
|
|
|
|
$res = $this->app->subscribe_message->send($data);
|
|
Log::info('发送订阅消息-res' . json_encode(['data' => $data, 'res' => $res], JSON_UNESCAPED_UNICODE));
|
|
}
|
|
|
|
/**
|
|
* @param $order_sn
|
|
* @param $openid
|
|
* @param $name
|
|
* @param $amount
|
|
* @param $desc
|
|
* @return array|mixed
|
|
* @throws \Exception
|
|
*/
|
|
public function mchPay($order_sn, $openid, $name, $amount, $desc = '用户提现')
|
|
{
|
|
$data = [
|
|
'partner_trade_no' => $order_sn, // 商户订单号,需保持唯一性(只能是字母或者数字,不能包含有符号)
|
|
'openid' => $openid,
|
|
'check_name' => 'FORCE_CHECK', // NO_CHECK:不校验真实姓名, FORCE_CHECK:强校验真实姓名
|
|
're_user_name' => $name, // 如果 check_name 设置为FORCE_CHECK,则必填用户真实姓名
|
|
'amount' => $amount * 100, // 企业付款金额,单位为分
|
|
'desc' => $desc, // 企业付款操作说明信息。必填
|
|
];
|
|
$res = $this->payApp->transfer->toBalance($data);
|
|
Log::info('企业付款-res' . json_encode(['data' => $data, 'res' => $res], JSON_UNESCAPED_UNICODE));
|
|
return $res;
|
|
}
|
|
|
|
/**
|
|
* @param $apply_sn
|
|
* @return array|\EasyWeChat\Kernel\Support\Collection|object|\Psr\Http\Message\ResponseInterface|string
|
|
*/
|
|
public function queryBalance($apply_sn)
|
|
{
|
|
$res = $this->payApp->transfer->queryBalanceOrder($apply_sn);
|
|
Log::info('企业付款查询-res' . json_encode(['apply_sn' => $apply_sn, 'res' => $res]));
|
|
|
|
return $res;
|
|
}
|
|
|
|
/**
|
|
* @param $order_sn
|
|
* @param $refund_no
|
|
* @param $actual_amount
|
|
* @param $refund_amount
|
|
* @param $desc
|
|
* @return array|\EasyWeChat\Kernel\Support\Collection|object|\Psr\Http\Message\ResponseInterface|string
|
|
*/
|
|
public function refund($order_sn, $refund_no, $actual_amount, $refund_amount, $desc = '出票失败退款')
|
|
{
|
|
//商户订单号、商户退款单号、订单金额、退款金额、其他参数
|
|
$res = $this->payApp->refund->byOutTradeNumber($order_sn, $refund_no, bcmul($actual_amount, 100), bcmul($refund_amount, 100), [
|
|
// 可在此处传入其他参数,详细参数见微信支付文档
|
|
'refund_desc' => $desc,
|
|
]);
|
|
Log::info('微信订单退款-res' . json_encode(['param' => func_get_args(), 'res' => $res, [bcmul($actual_amount, 100), bcmul($refund_amount, 100)]]));
|
|
|
|
return $res;
|
|
}
|
|
|
|
/**
|
|
* @param $refund_no
|
|
* @return array|\EasyWeChat\Kernel\Support\Collection|object|\Psr\Http\Message\ResponseInterface|string
|
|
*/
|
|
public function refundQuery($refund_no)
|
|
{
|
|
//商户订单号、商户退款单号、订单金额、退款金额、其他参数
|
|
$res = $this->payApp->refund->queryByOutRefundNumber($refund_no);
|
|
Log::info('微信订单退款查询-res' . json_encode(['param' => func_get_args(), 'res' => $res]));
|
|
|
|
return $res;
|
|
}
|
|
}
|