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.

166 lines
6.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 app\money\service;
use app\money\model\MoneyOrder;
use think\facade\Request;
class Order extends Base
{
/**
* 调起支付
* @param int $order_id 订单ID
* @date 2022-10-31
*/
public function createPayOrder($order_id)
{
$order_model = new MoneyOrder();
// 获取订单信息
$order = $order_model->getOneData([
['id', '=', $order_id]
], 'id,user_id,user_agent,order_number,pay_type,pay_money');
// 调起支付
switch ($order['pay_type']) {
// 微信支付
case 1:
// $fubei_param = get_fubei_config();
// $mp_weixin_param = get_mp_weixin_config();
// $fubei_param['sub_appid'] = $mp_weixin_param['app_id'];
// $pay_class = new \fubei\pay\Pay($fubei_param);
// $openid = $this->openid;
// $notify_url = Request::domain() . '/index.php/money/api/Callback/fubeiNotify/agent/' . $order['user_agent'] . '/uid/' . $this->mid;
// $result = $pay_class->pay($openid, $order['pay_money'], $order['order_number'], $order['order_number'], '余额充值', $notify_url, $fubei_param['store_id'], $order['user_agent']);
//
// // 没有调起支付,返回报错
// if ($result['result_code'] != 200) {
// return sendErrorArray($result['result_code'], $result['result_message']);
// }
// return sendSuccessArray([
// // 是否需要支付
// 'need_pay' => 1,
// // 支付参数
// 'pay_params' => $result['data']['sign_package']
// ]);
// break;
$wxpay_param = get_wxpay_config_with_weixin($order['user_agent']);
$wxpay_class = new \tencent\wechat\pay\Wxpay($wxpay_param);
$openid = $this->openid;
$notify_url = Request::domain() . '/index.php/money/api/Callback/weixinNotify/agent/' . $order['user_agent'] . '/uid/' . $this->mid;
// 生成支付订单
$result = $wxpay_class->createOrder($openid, $order['pay_money'], $order['order_number'], '余额充值', $notify_url, $order['user_agent']);
if ($result['return_code'] == 'FAIL' || $result['result_code'] == 'FAIL') {
return sendErrorArray(2100, $result['return_code'] != 'SUCCESS' ? $result['return_msg'] : $result['err_code_des']);
}
/* --H5平台微信支付--
直接返回 */
if ($order['user_agent'] == 'h5') {
// mweb_url为拉起微信支付收银台的中间页面可通过访问该url来拉起微信客户端完成支付,mweb_url的有效期为5分钟
return sendSuccessArray([
'need_pay' => 1,
'pay_params' => $result['mweb_url']
]);
}
/* --其他平台微信支付--
获取支付参数 */
$result_pay = $wxpay_class->wxPay($result['prepay_id'], $order['user_agent']);
return sendSuccessArray([
// 是否需要支付
'need_pay' => 1,
// 支付参数
'pay_params' => $result_pay
]);
break;
// 支付宝支付
case 2:
switch ($order['user_agent']) {
case 'app':
$ali_pay_param = get_app_alipay_config();
$ali_pay_class = new \ali\alipay\pay\AliPay($ali_pay_param);
$notify_url = Request::domain() . '/index.php/money/api/Callback/alipayNotify/agent/' . $order['user_agent'] . '/uid/' . $this->mid;
// 统一下单
$result = $ali_pay_class->aliPay($order['pay_money'], $order['order_number'], '余额充值订单', $notify_url);
if (empty($result['body'])) {
return sendErrorArray(2101, '生成支付宝订单失败');
}
return sendSuccessArray(['need_pay' => 1, 'pay_params' => $result['body']]);
break;
case 'mp_alipay':
$ali_pay_param = get_mp_alipay_config();
$ali_pay_class = new \ali\alipay\pay\AliPay($ali_pay_param);
$notify_url = Request::domain() . '/index.php/money/api/Callback/alipayNotify/agent/' . $order['user_agent'] . '/uid/' . $this->mid;
// 统一下单
$result = $ali_pay_class->mpAliPay($order['pay_money'], $order['order_number'], $this->openid, '余额充值订单', $notify_url);
if ($result['code'] != 10000) {
return sendErrorArray($result['code'], $result['sub_msg']);
}
return sendSuccessArray(['need_pay' => 1, 'pay_params' => $result['trade_no']]);
break;
// 其他情况
default:
return sendErrorArray(2001, '未知错误');
break;
}
break;
// 其他情况
default:
return sendErrorArray(2002, '未知错误');
break;
}
}
/**
* 订单支付成功
* @param string $order_id 订单编号
* @param string $transaction_id 微信支付订单号
* @date 2022-11-01
*/
public function afterOrderPaySuccess($order_id, $transaction_id = '')
{
$order_model = new MoneyOrder();
$money_service = new \app\money\service\Money();
$order = $order_model->getOneData([
['id', '=', $order_id]
], 'id,order_number,user_id,money');
// 更改订单状态
$data_update = [
'id' => $order['id'],
'is_pay' => 1,
'pay_time' => time(),
'status' => 2,
'transaction_id' => $transaction_id
];
$res = $order_model->dataUpdate($data_update);
if (!$res) {
return sendErrorArray(2001, '订单状态修改失败');
}
// 更改余额
$res = $money_service->change($order['user_id'], 1, $order['money'], '充值到账', $order['order_number'], [
'money_order_id' => $order['id']
]);
if ($res['code'] != 0) {
return $res;
}
return sendSuccessArray();
}
}