RefundAmount.php 3.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  1. <?php
  2. namespace App\Handlers;
  3. use App\Models\OrderRefundMoneyW;
  4. use App\Models\OrderW;
  5. use App\Models\Store;
  6. use App\Models\DwbsUser;
  7. use Illuminate\Http\Request;
  8. use Illuminate\Support\Collection;
  9. use Illuminate\Support\Facades\Log;
  10. Class RefundAmount
  11. {
  12. public function refundAmount($order_id){
  13. $order=OrderW::where('id',$order_id)->first();
  14. $order_no=$order->order_no;
  15. $sub_mchid=Store::withTrashed()->where('is_failure',0)->where('id',$order->store_id)->value('sub_mchid');
  16. if(empty($sub_mchid)){
  17. return ['450001','店铺信息有误,暂不能退款'];
  18. }
  19. if(in_array($order->status,[1,2,3]) && $order->is_pay==1){
  20. $amount=["refund"=> (int) ($order->pay_money*100),"total"=> (int)($order->pay_money*100),"currency"=> "CNY"];
  21. }else{
  22. return ['450001','订单状态有误,暂不能退款'];
  23. }
  24. $handler=new SignHandler();
  25. $url='https://api.mch.weixin.qq.com/v3/ecommerce/refunds/apply';//申请退款地址
  26. $merchant_id=config('wechat.payment.default.mch_id');//商户号
  27. $serial_no=config('wechat.payment.default.serial_no');//不是平台证书序列号
  28. $mch_private_key=$handler->getPublicKey();//读取商户api证书公钥 getPublicKey()获取方法 见下文
  29. $timestamp=time();//时间戳
  30. $nonce=$handler->nonce_str();//随机字符串
  31. if($order->rand_no){
  32. $out_trade_no=$order->rand_no.'_'.$order_no;
  33. }else{
  34. $out_trade_no=$order_no;
  35. }
  36. $data=[
  37. 'sub_mchid'=>$sub_mchid,
  38. 'sp_appid'=>config('wechat.payment.default.app_id'),
  39. 'transaction_id'=>$order->wechat_pay_no,
  40. // 'out_trade_no'=> $out_trade_no,
  41. 'out_refund_no'=> 'TK'.substr($order_no,2),
  42. 'amount'=> $amount,
  43. 'notify_url'=>url('api/miniSub/refund_notify')
  44. ];
  45. Log::info(json_encode($data));
  46. $sign=$handler->sign($url,'POST',$timestamp,$nonce,json_encode($data),$mch_private_key,$merchant_id,$serial_no);//签名
  47. $header=[
  48. 'Authorization:WECHATPAY2-SHA256-RSA2048 ' . $sign,
  49. 'Accept:application/json',
  50. 'User-Agent:' . $merchant_id,
  51. 'Content-Type:application/json',
  52. 'Wechatpay-Serial:' . $handler->getzhengshu()//获取平台证书序列号
  53. ];
  54. $result=$handler->curl($url,json_encode($data),$header,'POST');
  55. $result=json_decode($result,true);
  56. Log::info(json_encode($result));
  57. if(isset($result['refund_id']) && !empty($result['refund_id'])){
  58. // OrderW::where('order_no',$order_no)
  59. // ->update(['refund_status'=>'START','refund_no'=>$result['refund_id'],'refund_time'=>$result['create_time']]);
  60. OrderRefundMoneyW::create([
  61. 'order_no'=>$order_no,
  62. 'refund_status'=>'START',
  63. 'refund_time'=>$result['create_time'],
  64. 'refund_no'=>$result['refund_id'],
  65. 'refund_account'=>null,
  66. 'refund_amount'=>null
  67. ]);
  68. }else{
  69. return ['450000',$result['message']];
  70. }
  71. }
  72. }