Laravel8微信支付实现后端退款

2021-11-09   阅读:1912   分类:后端    标签: Laravel

一、下载官方sdk

下载官方sdk,也可用这个,经过博主修改的laravel版的:https://www.aliyundrive.com/s/mf1jnwagKuk

支付可以参考这篇文章:Laravel8+微信小程序接入微信支付案例

二、使用微信支付退款

2.1、新增路由

Route::post('/order/refund', [App\Http\Controllers\Api\OrderController::class, 'refund'])->name('api.order.refund');

2.2、引入sdk文件:

require_once "../vendor/wxpaysdk/lib/WxPay.Api.php";
require_once "../vendor/wxpaysdk/lib/WxPay.NativePay.php";
require_once "../vendor/wxpaysdk/lib/phpqrcode/phpqrcode.php";
require_once "../vendor/wxpaysdk/lib/WxPay.JsApiPay.php";
require_once "../vendor/wxpaysdk/lib/WxPay.Config.php";
require_once "../vendor/wxpaysdk/lib/WxPay.Notify.php";

2.3、订单退款方法

  //订单退款
  public function refund(Request $request){
    if(isset($request->transaction_id) && $request->transaction_id != ""){
      if(isset($request->out_trade_no) && $request->out_trade_no != ""){
        try{
          $transaction_id = $request->transaction_id;
          $total_fee = $request->total_fee*100;
          $refund_fee = $request->refund_fee*100;
          $input = new \WxPayRefund();
          $input->SetTransaction_id($transaction_id);
          $input->SetTotal_fee($total_fee);
          $input->SetRefund_fee($refund_fee);

          $config = new \WxPayConfig();
          $input->SetOut_refund_no("sdkphp".date("YmdHis"));
          $input->SetOp_user_id($config->GetMerchantId());
          $res = \WxPayApi::refund($config, $input);
          // print_r($res);
          if($res['result_code']=='SUCCESS' && $res['return_code']=='SUCCESS'){
            $order = Order::where(array('out_trade_no'=>$request->transaction_id))->update(['refund_status'=>1]);
            return $this->jsonData(ApiErrDesc::REFUND_SUCCESS[0],ApiErrDesc::REFUND_SUCCESS[1]);
          }else{
            return $this->jsonData(ApiErrDesc::REFUND_ERROR[0],$res['err_code_des']); 
          }
        } catch(Exception $e) {
          // Log::ERROR(json_encode($e));
          Log::channel('payrefund')->debug(json_encode($e));
        }
      }
    }else{
      return $this->jsonData(ApiErrDesc::ERR_PARAMS[0],ApiErrDesc::ERR_PARAMS[1]);
    }
  }
【腾讯云】 爆款2核2G3M云服务器首年 61元,2核2G4M云服务器新老同享 99元/年,续费同价

‘简忆博客’微信公众号 扫码关注‘简忆博客’微信公众号,获取最新文章动态
转载:请说明文章出处“来源简忆博客”。http://tpxhm.com/adetail/852.html

×
觉得文章有用就打赏一下文章作者
微信扫一扫打赏 微信扫一扫打赏
支付宝扫一扫打赏 支付宝扫一扫打赏

文章评论(0)

登录
简忆博客壁纸一
简忆博客壁纸二
简忆博客壁纸三
简忆博客壁纸四
简忆博客壁纸五
简忆博客壁纸六
简忆博客壁纸七
简忆博客壁纸八
头像

简忆博客
勤于学习,乐于分享

置顶推荐

打赏本站

如果你觉得本站很棒,可以通过扫码支付打赏哦!
微信扫码:你说多少就多少~
微信扫码
支付宝扫码:你说多少就多少~
支付宝扫码
×