Laravel8微信支付实现后端退款

一、下载官方sdk下载官方sdk,也可用这个,经过博主修改的laravel版的:https://www.aliyundrive.com/s/mf1jnwagKuk支付可以参考这篇文章:Laravel8+微信小程序接入微信支付案例二、使用微信支付退款2.1、新增路由R

一、下载官方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]);
    }
  }

本文标题为:Laravel8微信支付实现后端退款

基础教程推荐