Pay.php 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  1. <?php
  2. declare (strict_types=1);
  3. namespace app\api\controller\v1;
  4. use app\Request;
  5. use app\BaseController;
  6. use app\model\api\PayTrade;
  7. use library\services\UtilService;
  8. use library\utils\weixinPay 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. $xmlData = $request->getInput();
  20. if(empty($xmlData)){
  21. return false;
  22. }
  23. $logfile = app()->getRootPath().'public/log/wxnotify.log';
  24. file_put_contents($logfile, $xmlData.PHP_EOL,FILE_APPEND | LOCK_EX);
  25. //
  26. // if(empty($post["event_type"]) || $post["event_type"] != "TRANSACTION.SUCCESS"){
  27. // return false;
  28. // }
  29. // if(empty($post["resource_type"]) || $post["resource_type"]!="encrypt-resource"){
  30. // return false;
  31. // }
  32. // if(empty($post["resource"])){
  33. // return false;
  34. // }
  35. // $wxpay = new wxpayApi();
  36. // $result = $wxpay->aesGcmDecrypt([
  37. // "associated_data"=>$post["resource"]["associated_data"],
  38. // "nonce"=>$post["resource"]["nonce"],
  39. // "ciphertext"=>$post["resource"]["ciphertext"],
  40. // ]);
  41. //
  42. // if(!$result){
  43. // return false;
  44. // }
  45. // //商家内部订单号
  46. // $out_trade_no = $result["out_trade_no"];
  47. // $res = $wxpay->searchOrder($out_trade_no);
  48. // if(!$res){
  49. // return false;
  50. // }
  51. // $data = json_decode($res,true);
  52. // //支付成功
  53. // if($data["trade_state"]=="SUCCESS"){
  54. // $payDataInfo = [
  55. // 'totalMoney' =>(empty($data["amount"]) || empty($data["amount"]["total"])) ?"未返回":$data["amount"]["total"]/100,
  56. // 'payMoney' =>(empty($data["amount"]) || empty($data["amount"]["payer_total"]))?"未返回":$data["amount"]["payer_total"]/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["success_time"]) ? "未返回" : $data["success_time"],
  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 wxpayApi();
  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. }