Pay.php 4.6 KB

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