Pay.php 2.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  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 app\lib\OrderLib;
  10. use think\Exception;
  11. class Pay extends BaseController{
  12. /**
  13. * 微信支付异步反馈
  14. * @param Request $request
  15. * @return boolean
  16. */
  17. public function wxpayNotify(Request $request){
  18. $post = UtilService::getMore([
  19. ['id', ''],
  20. ['create_time', ''],
  21. ['event_type',''],
  22. ['resource_type', ''],
  23. ['resource',[]],
  24. ['summary', ''],
  25. ], $request);
  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. if(!$result){
  42. return false;
  43. }
  44. //商家内部订单号
  45. $out_trade_no = $result["out_trade_no"];
  46. $res = $wxpay->searchOrder($out_trade_no);
  47. if(!$res){
  48. return false;
  49. }
  50. $data = json_decode($res,true);
  51. //支付成功
  52. if($data["trade_state"]=="SUCCESS"){
  53. $payDataInfo = [
  54. 'totalMoney' =>(empty($data["amount"]) || empty($data["amount"]["total"])) ?"未返回":$data["amount"]["total"]/100,
  55. 'payMoney' =>(empty($data["amount"]) || empty($data["amount"]["payer_total"]))?"未返回":$data["amount"]["payer_total"]/100,
  56. 'payTradeNo' =>empty($data["transaction_id"]) ? "未返回" : $data["transaction_id"],
  57. 'outTradeNo' =>empty($data["out_trade_no"]) ? "未返回" : $data["out_trade_no"],
  58. "tradeStatus"=>empty($data["trade_state"]) ? "未返回" : $data["trade_state"],
  59. "payTime" =>empty($data["success_time"]) ? "未返回" : $data["success_time"],
  60. ];
  61. $pay_json = json_encode($payDataInfo);
  62. (new OrderLib)->orderPay($out_trade_no, $pay_json);
  63. }
  64. }
  65. }