Pay.php 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  1. <?php
  2. declare (strict_types=1);
  3. namespace app\api\controller;
  4. use app\Request;
  5. use app\BaseController;
  6. use app\model\api\PayTrade;
  7. use library\services\UtilService;
  8. use library\utils\WxpayV2 as wxpayApi;
  9. use library\utils\weixinPay as wxpayApiV3;
  10. use app\model\api\Sys as SysModel;
  11. use WeChatPay\Transformer;
  12. use app\lib\OrderLib;
  13. use think\Exception;
  14. class Pay extends BaseController{
  15. /**
  16. * 微信支付V2异步反馈
  17. * @param Request $request
  18. * @return boolean
  19. */
  20. public function wxpayNotify(Request $request){
  21. $logfile = app()->getRootPath().'public/log/wxnotify.log';
  22. $xmlData = $request->getInput();
  23. if(empty($xmlData)){
  24. return false;
  25. }
  26. // $weixinConfig = (new SysModel)->getWeixinConfig();
  27. // file_put_contents($logfile, PHP_EOL.date("Y-m-d H:i:s").PHP_EOL.$xmlData.PHP_EOL,FILE_APPEND | LOCK_EX);
  28. $wxpay = new wxpayApi();
  29. // $wxpay = new wxpayApi($weixinConfig);
  30. //回调验签
  31. $res = $wxpay->notifyCheckSign($xmlData);
  32. if(!$res || empty($res["data"])){
  33. return false;
  34. }
  35. @file_put_contents('quanju.txt', json_encode($res). "-支付完成回调信息\r\n", 8);
  36. $result=$res["data"];
  37. //验证返回信息
  38. if(empty($result["return_code"]) || $result["return_code"]!="SUCCESS"){
  39. return false;
  40. }
  41. //验证支付信息
  42. if(empty($result["result_code"]) || $result["result_code"]!="SUCCESS"){
  43. return false;
  44. }
  45. //查询订单
  46. $out_trade_no = $result["out_trade_no"];
  47. $data = $wxpay->searchOrderQuery($out_trade_no);
  48. if(empty($data)){
  49. return false;
  50. }
  51. //支付成功
  52. if($data["trade_state"]=="SUCCESS"){
  53. $payDataInfo = [
  54. 'totalMoney' =>empty($data["total_fee"]) ? "未返回" : $data["total_fee"]/100,
  55. 'payMoney' =>empty($data["cash_fee"]) ? "未返回" : $data["cash_fee"]/100,
  56. 'payTradeNo' =>empty($data["transaction_id"]) ? "未返回" : $data["transaction_id"],
  57. 'outTradeNo' =>empty($data["out_trade_no"]) ? "未返回" : $data["out_trade_no"],
  58. "tradeStatus"=>empty($data["trade_state"]) ? "未返回" : $data["trade_state"],
  59. "payTime" =>empty($data["time_end"]) ? "未返回" : $data["time_end"],
  60. "attach" =>empty($data["attach"]) ? "未返回" : $data["attach"],
  61. ];
  62. $pay_json = json_encode($payDataInfo);
  63. (new OrderLib)->orderPay($out_trade_no, $pay_json);
  64. }
  65. }
  66. /**
  67. * 微信支付V3异步反馈
  68. * @param Request $request
  69. * @return boolean
  70. */
  71. public function wxpayNotifyV3(Request $request){
  72. $post = UtilService::getMore([
  73. ['id', ''],
  74. ['create_time', ''],
  75. ['event_type',''],
  76. ['resource_type', ''],
  77. ['resource',[]],
  78. ['summary', ''],
  79. ], $request);
  80. if(empty($post["event_type"]) || $post["event_type"] != "TRANSACTION.SUCCESS"){
  81. return false;
  82. }
  83. if(empty($post["resource_type"]) || $post["resource_type"]!="encrypt-resource"){
  84. return false;
  85. }
  86. if(empty($post["resource"])){
  87. return false;
  88. }
  89. $wxpay = new wxpayApiV3();
  90. $result = $wxpay->aesGcmDecrypt([
  91. "associated_data"=>$post["resource"]["associated_data"],
  92. "nonce"=>$post["resource"]["nonce"],
  93. "ciphertext"=>$post["resource"]["ciphertext"],
  94. ]);
  95. if(!$result){
  96. return false;
  97. }
  98. //商家内部订单号
  99. $out_trade_no = $result["out_trade_no"];
  100. $res = $wxpay->searchOrder($out_trade_no);
  101. if(!$res){
  102. return false;
  103. }
  104. $data = json_decode($res,true);
  105. //支付成功
  106. if($data["trade_state"]=="SUCCESS"){
  107. $payDataInfo = [
  108. 'totalMoney' =>(empty($data["amount"]) || empty($data["amount"]["total"])) ?"未返回":$data["amount"]["total"]/100,
  109. 'payMoney' =>(empty($data["amount"]) || empty($data["amount"]["payer_total"]))?"未返回":$data["amount"]["payer_total"]/100,
  110. 'payTradeNo' =>empty($data["transaction_id"]) ? "未返回" : $data["transaction_id"],
  111. 'outTradeNo' =>empty($data["out_trade_no"]) ? "未返回" : $data["out_trade_no"],
  112. "tradeStatus"=>empty($data["trade_state"]) ? "未返回" : $data["trade_state"],
  113. "payTime" =>empty($data["success_time"]) ? "未返回" : $data["success_time"],
  114. ];
  115. $pay_json = json_encode($payDataInfo);
  116. (new OrderLib)->orderPay($out_trade_no, $pay_json);
  117. }
  118. }
  119. }