E企盈小程序营销工具技术服务商 热线:4006-838-530

微信小程序_微信支付_申请退款(PHP语言Laravel框架)

E企盈小程序直播系统

第一步:下载微信支付sdk下载网址:https://pay.weixin.qq.com/wiki/doc/api/jsapi.php?chapter=11_1这是微信支付商户平台页面“公众号支付”模块里面的sdk,app支付的sdk是不能用的。下载好sdk之后,真正需要的文件有5个,在lib文件夹内,复制lib文件夹到你项目的某个位置即可。第二步:下载证书cert微信官方要求退款需要使用证书,下载证书需要登录微信支付商户平台安装并下载,步骤很简单,按照指导或百度搜索即可解决。下载后复制cert文件夹到你项目的某个位置即可,我这里放到了微信支付sdk目录,位置不影响使用即可。第三步:修改配置文件找到微信支付配置文件WxPay.Config.php并进行如下修改:第四步:退款功能实现1.路由Route::put(‘api/:version/refund/refund/:id’, ‘api/:version.Refund/doRefund’,[], [‘id’=>’d+’]);2.控制器方法在控制器的退款类中建立doRefund方法,验证参数id是否满足条件,实例化Refund模型类(这个类中的refund是执行退款的方法);// 管理后台点击退款按钮,执行退款操作    public function doRefund($id){        if(empty($id)){            $id = $_GET[‘id’];        }        (new IDMustBePositiveInt())->goCheck();        $refund = new Refund($id);        return $refund->refund();    }3.模型方法在模型的退款类中建立refund方法,并进行必要验证,通过则执行退款操作。<?php/** * Created by PhpStorm. * User: Admin * Date: 2018-05-22 * Time: 10:51 */namespace appapiservice;use applibenumOrderStatusEnum;use applibexceptionOrderException;use applibexceptionRefundException;use applibexceptionTokenException;use thinkException;use appapimodelRefund as RefundModel;use appapimodelOrder as OrderModel;use thinkLoader;use thinkLog;// 引入WxPayApi.php文件,下单、查询、退款等api方法都在此文件,其他四个文件在WxPayApi.php文件中被引入Loader::import(‘WxPay.WxPay’, EXTEND_PATH, ‘.Api.php’);class Refund{    private $orderNo;    private $orderID;    private $refundNo;    private $refundID;    function __construct($refundID){        if (!$refundID){            throw new Exception(‘退款单号不允许为NULL’);        }        $this->refundID = $refundID;    }    public function refund(){        $this->checkRefundValid();        $refund = RefundModel::where(‘id’, ‘=’, $this->refundID)->find();        $order = $refund->order()->where(‘id’, ‘=’, $refund->oid)->find();        if($order->status != OrderStatusEnum::AUDIT_PASS){            throw new RefundException([                ‘msg’ => ‘退款申请不是审核通过状态,出现异常’,                ‘errorCode’ => 80004,                ‘code’ => 400            ]);        }        return $this->makeWxRefund($order->total_price, $refund->money);    }    // 构建微信退款订单信息    private function makeWxRefund($order_money, $refund_money){        $wxRefundData = new WxPayRefund();        $wxRefundData->SetOut_trade_no($this->orderNo);        $wxRefundData->SetOut_refund_no($this->refundNo);        $wxRefundData->SetTotal_fee($order_money * 100);        $wxRefundData->SetRefund_fee($refund_money * 100);        $wxRefundData->SetOp_user_id(WxPayConfig::MCHID);        $wxRefund = WxPayApi::refund($wxRefundData);        // 失败时不会返回result_code        if($wxRefund[‘return_code’] != ‘SUCCESS’ || $wxRefund[‘result_code’] !=’SUCCESS’){            Log::record($wxRefund,’error’);            Log::record(‘退款失败’,’error’);            // throw new Exception(‘退款失败’);        }elseif($wxRefund[‘return_code’] == ‘SUCCESS’ || $wxRefund[‘result_code’] ==’SUCCESS’){            $this->updateOrderStatus();        }        return  $wxRefund;    }    //退款成功,改变订单状态    private function updateOrderStatus(){        $status = OrderStatusEnum::HAVE_A_REFUND;        OrderModel::where(‘id’, ‘=’, $this->orderID)->update([‘status’ => $status]);    }    //校验订单并进行参数赋值    private function checkRefundValid(){        $refund = RefundModel::where(‘id’, ‘=’, $this->refundID)->find();        $order = $refund->order()->where(‘id’, ‘=’, $refund->oid)->find();        if (!$refund){            throw new RefundException();        }        if(!$order){            throw new OrderException();        }        if($order->user_id != $refund->uid){            throw new TokenException([                ‘msg’ => ‘订单与或申请退款单用户不匹配’,                ‘errorCode’ => 10003            ]);        }        if($order->status != OrderStatusEnum::AUDIT_PASS){            throw new RefundException([                ‘msg’ => ‘退款申请未审核或审核拒绝’,                ‘errorCode’ => 80009,                ‘code’ => 400            ]);        }        $this->orderNo = $order->order_no;        $this->orderID = $refund->oid;        $this->refundNo = $refund->refund_no;        return true;    }}最重要的方法就是makeWxRefund,其他的方法都可以忽略不计,只要微信退款必要的参数能够给到。注意可能会出现的错误:curl错误:58,这是因为证书路径不是绝对路径造成的,在服务器上应该从根目录开始找到文件的绝对路径,如:    E:/wamp/www/medisum/extend/WxPay/cert/apiclient_cert.pem退款功能至此完成,如有问题,评论留言。****************************************只要思想不滑坡,办法总比困难多******************************************微信小程序

赞(0) 打赏
未经允许不得转载:E企盈小程序开发-热线:4006-838-530 » 微信小程序_微信支付_申请退款(PHP语言Laravel框架)
分享到: 更多 (0)
E企盈小程序直播营销卖货系统

评论 抢沙发

E企盈小程序开发

联系我们联系我们

觉得文章有用就打赏一下文章作者

支付宝扫一扫打赏

微信扫一扫打赏