1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374 |
- <?php
- namespace App\Handlers;
- use App\Models\OrderRefundMoneyW;
- use App\Models\OrderW;
- use App\Models\Store;
- use App\Models\DwbsUser;
- use Illuminate\Http\Request;
- use Illuminate\Support\Collection;
- use Illuminate\Support\Facades\Log;
- Class RefundAmount
- {
- public function refundAmount($order_id){
- $order=OrderW::where('id',$order_id)->first();
- $order_no=$order->order_no;
- $sub_mchid=Store::withTrashed()->where('is_failure',0)->where('id',$order->store_id)->value('sub_mchid');
- if(empty($sub_mchid)){
- return ['450001','店铺信息有误,暂不能退款'];
- }
- if(in_array($order->status,[1,2,3]) && $order->is_pay==1){
- $amount=["refund"=> (int) ($order->pay_money*100),"total"=> (int)($order->pay_money*100),"currency"=> "CNY"];
- }else{
- return ['450001','订单状态有误,暂不能退款'];
- }
- $handler=new SignHandler();
- $url='https://api.mch.weixin.qq.com/v3/ecommerce/refunds/apply';//申请退款地址
- $merchant_id=config('wechat.payment.default.mch_id');//商户号
- $serial_no=config('wechat.payment.default.serial_no');//不是平台证书序列号
- $mch_private_key=$handler->getPublicKey();//读取商户api证书公钥 getPublicKey()获取方法 见下文
- $timestamp=time();//时间戳
- $nonce=$handler->nonce_str();//随机字符串
- if($order->rand_no){
- $out_trade_no=$order->rand_no.'_'.$order_no;
- }else{
- $out_trade_no=$order_no;
- }
- $data=[
- 'sub_mchid'=>$sub_mchid,
- 'sp_appid'=>config('wechat.payment.default.app_id'),
- 'transaction_id'=>$order->wechat_pay_no,
- // 'out_trade_no'=> $out_trade_no,
- 'out_refund_no'=> 'TK'.substr($order_no,2),
- 'amount'=> $amount,
- 'notify_url'=>url('api/miniSub/refund_notify')
- ];
- Log::info(json_encode($data));
- $sign=$handler->sign($url,'POST',$timestamp,$nonce,json_encode($data),$mch_private_key,$merchant_id,$serial_no);//签名
- $header=[
- 'Authorization:WECHATPAY2-SHA256-RSA2048 ' . $sign,
- 'Accept:application/json',
- 'User-Agent:' . $merchant_id,
- 'Content-Type:application/json',
- 'Wechatpay-Serial:' . $handler->getzhengshu()//获取平台证书序列号
- ];
- $result=$handler->curl($url,json_encode($data),$header,'POST');
- $result=json_decode($result,true);
- Log::info(json_encode($result));
- if(isset($result['refund_id']) && !empty($result['refund_id'])){
- // OrderW::where('order_no',$order_no)
- // ->update(['refund_status'=>'START','refund_no'=>$result['refund_id'],'refund_time'=>$result['create_time']]);
- OrderRefundMoneyW::create([
- 'order_no'=>$order_no,
- 'refund_status'=>'START',
- 'refund_time'=>$result['create_time'],
- 'refund_no'=>$result['refund_id'],
- 'refund_account'=>null,
- 'refund_amount'=>null
- ]);
- }else{
- return ['450000',$result['message']];
- }
- }
- }
|