赞
踩
//1 首先要安装fastadmin的支付插件 退款需配置证书 此方法使用后不能更新支付插件 升级后方法会被覆盖掉
//2 在addons\epay\library\Service 支付的同文件下放此方法 基本和支付参数参数差不多
/**
* 提交退款订单
* @param array|float $amount 订单金额
* @param array|float $refund_money 退款金额
* @param string $orderid 订单号
* @param string $refund_sn 退款订单号
* @param string $type 支付类型,可选alipay或wechat
* @param string $remark 退款原因
* @param string $notifyurl 通知回调URL
* @param string $returnurl 跳转返回URL
* @param string $method 支付方式
* @return Response|RedirectResponse|Collection
* @throws Exception
*/
public static function submitRefund($amount=null,$refund_money,$orderid,$refund_sn,$type,$remark = null,$notifyurl = null,$returnurl = null,$method = 'app'){
if (!is_array($amount)) {
$params = [
'amount' => $amount,
'type' => $type,
'notifyurl' => $notifyurl,
'returnurl' => $returnurl,
'method' => $method,
];
} else {
$params = $amount;
}
$type = isset($params['type']) && in_array($params['type'], ['alipay', 'wechat']) ? $params['type'] : 'wechat';
$request = request();
$notifyurl = isset($params['notifyurl']) ? $params['notifyurl'] : $request->root(true) . '/addons/epay/index/' . $type . 'notify';
// $returnurl = isset($params['returnurl']) ? $params['returnurl'] : $request->root(true) . '/addons/epay/index/' . $type . 'return/out_trade_no/' . $orderid;
$config = Service::getConfig($type);
$config['notify_url'] = $notifyurl;
$config['return_url'] = $returnurl;
$result = null;
//退款参数
$order_data = [
'out_trade_no' => $orderid//原订单号
];
if ($type == 'wechat') {
//创建支付对象
$pay = Pay::wechat($config);
$total_fee = $amount * 100;
$refund_fee = $refund_money * 100;
$order_data = array_merge($order_data, [
'out_refund_no' => $refund_sn,//退款订单号
'total_fee' => $total_fee,//支付金额
'refund_fee' => $refund_fee,//退款金额
'refund_desc' => $remark,//退款原因
'type' => $method //支付方式
]);
} else {
$pay = Pay::alipay($config);
$order_data = array_merge($order_data, [
'out_request_no' => $refund_sn,//退款订单号
'refund_amount' => $refund_money,
]);
}
$result = $pay->refund($order_data);
//使用重写的Response类、RedirectResponse、Collection类
if ($result instanceof \Symfony\Component\HttpFoundation\RedirectResponse) {
$result = RedirectResponse::create($result->getTargetUrl());
} elseif ($result instanceof \Symfony\Component\HttpFoundation\Response) {
$result = Response::create($result->getContent());
} elseif ($result instanceof \Yansongda\Supports\Collection) {
$result = Collection::make($result->all());
}
return $result;
}
//3 在需要退款的地方 use addons\epay\library\Service;
$orderInfo=$this->model::find($id);//退款订单信息
$notifyurl = $this->request->domain().'/api/' . $this->request->controller() . '/refundNotifyx/paytype/' . $orderInfo->pay_type;//退款回调地址
//直接调用退款方法传参即可
$response = Service::submitRefund($orderInfo->pay_fee,$orderInfo->refund_fee, $orderInfo->order_sn,getRefundSn($userId),$orderInfo->pay_type, $orderInfo->reason, $notifyurl,'', 'app');
//4生成退款订单号 此方法可自写
if(!function_exists('getRefundSn')) {
function getRefundSn($user_id)
{
$rand = $user_id < 9999 ? mt_rand(100000, 99999999) : mt_rand(100, 99999);
$order_sn = date('Yhis') . $rand;
$id = str_pad($user_id, (24 - strlen($order_sn)), '0', STR_PAD_BOTH);
return 'R' . $order_sn . $id;
}
}
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。