pay.php 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  1. <?php
  2. header("Content-type: text/json; charset=utf-8");
  3. $phone=str_replace(" ","",$_POST['phone']??"");
  4. $price=$_POST['price'];
  5. $order_sn=uniqid('order_sn');
  6. $admin_id=$_SESSION['admin_id'];
  7. $data=[
  8. "user_id"=>$admin_id,
  9. "price"=>$price,
  10. "phone"=>$phone,
  11. "order_sn"=>$order_sn,
  12. "created_at"=>date("Y-m-d H:i:s"),
  13. "updated_at"=>date("Y-m-d H:i:s"),
  14. ];
  15. $statement=$pdo->prepare("insert into recharge(`user_id`,`price`,`phone`,`order_sn`,`created_at`,`updated_at`) values(?,?,?,?,?,?)");
  16. $res=$statement->execute(array_values($data));
  17. if(!$res)exit(json_encode(['code'=>0,"msg"=>"创建订单失败"]));
  18. $id=$pdo->lastInsertId();
  19. $url = isset($_SERVER['HTTPS']) && $_SERVER['HTTPS'] === 'on' ? "https" : "http";
  20. $url .= "://" . $_SERVER['HTTP_HOST'];
  21. $request_data=[
  22. "mch_id"=>"tongue",
  23. "ptype"=>82,
  24. "from_name"=>"充值",
  25. "order_sn"=>$order_sn,
  26. "money"=>$price,
  27. "goods_desc"=>"充值",
  28. "client_ip"=>$_SERVER['REMOTE_ADDR']??"127.0.0.1",
  29. "format"=>"json",
  30. "notify_url"=>$url."/notify",
  31. "pay_backurl"=>$url,
  32. "time"=>time(),
  33. ];
  34. ksort($request_data);
  35. $request_data['key']="37cd2d09c84f6ff7f1d28960ef82c4e533a3c047";
  36. $request_data['sign']=md5(urldecode(http_build_query($request_data)));
  37. unset($request_data['key']);
  38. //echo "<pre>";
  39. //var_dump(http_build_query($request_data));
  40. //var_dump($request_data);
  41. //echo "</pre>";
  42. //exit();
  43. //正式环境打开
  44. file_put_contents(date("Y-m-d").'pay.log',date("H:i:s")."请求:".http_build_query($request_data)."\r\n",FILE_APPEND);
  45. $res=curl_request("http://47.236.143.199/?c=Pay&".http_build_query($request_data),array(),"get",array("content-type: text/html"),false);
  46. file_put_contents(date("Y-m-d").'pay.log',date("H:i:s")."响应".$res."\r\n",FILE_APPEND);
  47. $res=json_decode($res,true);
  48. //$res=json_decode('{"code":"1","msg":"下单成功","data":{"order_sn":"ZZHF20240126214407639","ptype":"82","ptype_name":"","realname":null,"account":null,"money":"30","bank":"","branch_name":"","qrcode":"https:\/\/hh5ad.hnapay.com\/pay\/order.shtml?orderId=2024012676143559&orderKey=fef8f40cc98edd2dffc4041dedcd1d2b3aac9eb0"}}',true);
  49. if($res['code']!=1)exit(json_encode(['code'=>0,"msg"=>"下单失败"]));
  50. $statement=$pdo->exec("update recharge set `app_order_sn`='{$res['data']['order_sn']}' where id={$id}");
  51. exit(json_encode(['code'=>1,'msg'=>'success',"data"=>['pay_url'=>$res['data']['qrcode']]]));
  52. //exit(json_encode(['code'=>1,"msg"=>"","data"=>[
  53. // "pay_url"=>$res['']
  54. //]]));