fastadmin 支付插件退款操作

3 篇文章 1 订阅
1 篇文章 0 订阅
 

//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;
                 }
            }
  • 2
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值