Pay.php 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  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. @file_put_contents('quanju.txt', json_encode($data). "-支付完成回调订单\r\n", 8);
  49. if(empty($data)){
  50. return false;
  51. }
  52. //支付成功
  53. if($data["trade_state"]=="SUCCESS"){
  54. $payDataInfo = [
  55. 'totalMoney' =>empty($data["total_fee"]) ? "未返回" : $data["total_fee"]/100,
  56. 'payMoney' =>empty($data["cash_fee"]) ? "未返回" : $data["cash_fee"]/100,
  57. 'payTradeNo' =>empty($data["transaction_id"]) ? "未返回" : $data["transaction_id"],
  58. 'outTradeNo' =>empty($data["out_trade_no"]) ? "未返回" : $data["out_trade_no"],
  59. "tradeStatus"=>empty($data["trade_state"]) ? "未返回" : $data["trade_state"],
  60. "payTime" =>empty($data["time_end"]) ? "未返回" : $data["time_end"],
  61. "attach" =>empty($data["attach"]) ? "未返回" : $data["attach"],
  62. ];
  63. $pay_json = json_encode($payDataInfo);
  64. (new OrderLib)->orderPay($out_trade_no, $pay_json);
  65. }
  66. }
  67. /**
  68. * 微信支付V3异步反馈
  69. * @param Request $request
  70. * @return boolean
  71. */
  72. public function wxpayNotifyV3(Request $request){
  73. $post = UtilService::getMore([
  74. ['id', ''],
  75. ['create_time', ''],
  76. ['event_type',''],
  77. ['resource_type', ''],
  78. ['resource',[]],
  79. ['summary', ''],
  80. ], $request);
  81. if(empty($post["event_type"]) || $post["event_type"] != "TRANSACTION.SUCCESS"){
  82. return false;
  83. }
  84. if(empty($post["resource_type"]) || $post["resource_type"]!="encrypt-resource"){
  85. return false;
  86. }
  87. if(empty($post["resource"])){
  88. return false;
  89. }
  90. $wxpay = new wxpayApiV3();
  91. $result = $wxpay->aesGcmDecrypt([
  92. "associated_data"=>$post["resource"]["associated_data"],
  93. "nonce"=>$post["resource"]["nonce"],
  94. "ciphertext"=>$post["resource"]["ciphertext"],
  95. ]);
  96. if(!$result){
  97. return false;
  98. }
  99. //商家内部订单号
  100. $out_trade_no = $result["out_trade_no"];
  101. $res = $wxpay->searchOrder($out_trade_no);
  102. if(!$res){
  103. return false;
  104. }
  105. $data = json_decode($res,true);
  106. //支付成功
  107. if($data["trade_state"]=="SUCCESS"){
  108. $payDataInfo = [
  109. 'totalMoney' =>(empty($data["amount"]) || empty($data["amount"]["total"])) ?"未返回":$data["amount"]["total"]/100,
  110. 'payMoney' =>(empty($data["amount"]) || empty($data["amount"]["payer_total"]))?"未返回":$data["amount"]["payer_total"]/100,
  111. 'payTradeNo' =>empty($data["transaction_id"]) ? "未返回" : $data["transaction_id"],
  112. 'outTradeNo' =>empty($data["out_trade_no"]) ? "未返回" : $data["out_trade_no"],
  113. "tradeStatus"=>empty($data["trade_state"]) ? "未返回" : $data["trade_state"],
  114. "payTime" =>empty($data["success_time"]) ? "未返回" : $data["success_time"],
  115. ];
  116. $pay_json = json_encode($payDataInfo);
  117. (new OrderLib)->orderPay($out_trade_no, $pay_json);
  118. }
  119. }
  120. }