Pay.php 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  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\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. $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. //
  27. // if(empty($post["event_type"]) || $post["event_type"] != "TRANSACTION.SUCCESS"){
  28. // return false;
  29. // }
  30. // if(empty($post["resource_type"]) || $post["resource_type"]!="encrypt-resource"){
  31. // return false;
  32. // }
  33. // if(empty($post["resource"])){
  34. // return false;
  35. // }
  36. // $wxpay = new wxpayApi();
  37. // $result = $wxpay->aesGcmDecrypt([
  38. // "associated_data"=>$post["resource"]["associated_data"],
  39. // "nonce"=>$post["resource"]["nonce"],
  40. // "ciphertext"=>$post["resource"]["ciphertext"],
  41. // ]);
  42. //
  43. // if(!$result){
  44. // return false;
  45. // }
  46. // //商家内部订单号
  47. // $out_trade_no = $result["out_trade_no"];
  48. // $res = $wxpay->searchOrder($out_trade_no);
  49. // if(!$res){
  50. // return false;
  51. // }
  52. // $data = json_decode($res,true);
  53. // //支付成功
  54. // if($data["trade_state"]=="SUCCESS"){
  55. // $payDataInfo = [
  56. // 'totalMoney' =>(empty($data["amount"]) || empty($data["amount"]["total"])) ?"未返回":$data["amount"]["total"]/100,
  57. // 'payMoney' =>(empty($data["amount"]) || empty($data["amount"]["payer_total"]))?"未返回":$data["amount"]["payer_total"]/100,
  58. // 'payTradeNo' =>empty($data["transaction_id"]) ? "未返回" : $data["transaction_id"],
  59. // 'outTradeNo' =>empty($data["out_trade_no"]) ? "未返回" : $data["out_trade_no"],
  60. // "tradeStatus"=>empty($data["trade_state"]) ? "未返回" : $data["trade_state"],
  61. // "payTime" =>empty($data["success_time"]) ? "未返回" : $data["success_time"],
  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 wxpayApi();
  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. }