Pay.php 5.1 KB

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