PayNotifyCallBack.php 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. <?php
  2. require_once "WxPay.Api.php";
  3. require_once 'WxPay.Notify.php';
  4. class PayNotifyCallBack extends WxPayNotify
  5. {
  6. /**
  7. * 查询订单
  8. * @param $transactionId string
  9. * @param string $orderSn string
  10. * @return bool
  11. */
  12. public function queryOrder($transactionId, $orderSn = '')
  13. {
  14. $input = new WxPayOrderQuery();
  15. if ($transactionId) {
  16. $input->SetTransaction_id($transactionId);
  17. } else if ($orderSn) {
  18. $input->SetOut_trade_no($orderSn);
  19. }
  20. $result = WxPayApi::orderQuery($input);
  21. if(array_key_exists("return_code", $result)
  22. && array_key_exists("result_code", $result)
  23. && array_key_exists('trade_state', $result)
  24. && $result["return_code"] == "SUCCESS"
  25. && $result["result_code"] == "SUCCESS"
  26. )
  27. {
  28. $orderSn = $result['out_trade_no']; // 订单号
  29. $ttbPay = model('TtbAutoList');
  30. $orderInfo = $ttbPay->fetchOrderInfo($orderSn);
  31. if (empty($orderInfo)) {
  32. return false;
  33. }
  34. if ($result["trade_state"] == "SUCCESS") {
  35. // 支付成功
  36. if ($orderInfo['pay_status'] == 1) {
  37. return true;
  38. }
  39. //if (array_key_exists('total_fee', $result) && $result['total_fee'] == $orderInfo['pay_amount'] * 100) {
  40. // 支付金额
  41. $payAmount = sprintf('%.2f', $result['total_fee'] / 100);
  42. // 更新订单状态,用户账户余额,账户变化日志
  43. return $ttbPay->paySuccess($orderSn, $result['transaction_id']);
  44. //}
  45. } elseif ($result["trade_state"] == "CLOSED") {
  46. // 已关闭
  47. if ($orderInfo['pay_status'] == 3) {
  48. return 'CLOSED';
  49. }
  50. $ttbPay->updateOrder(
  51. ['pay_status' => 3, 'update_time' => NOW_TIMESTAMP],
  52. ['order_sn' => $orderSn]
  53. );
  54. return 'CLOSED';
  55. } elseif ($result["trade_state"] == "PAYERROR") {
  56. // 支付失败
  57. if ($orderInfo['pay_status'] == 2) {
  58. return 'CLOSED';
  59. }
  60. $ttbPay->updateOrder(
  61. ['pay_status' => 2, 'update_time' => NOW_TIMESTAMP],
  62. ['order_sn' => $orderSn]
  63. );
  64. return 'CLOSED';
  65. }
  66. }
  67. return false;
  68. }
  69. //重写回调处理函数
  70. public function NotifyProcess($data, &$msg)
  71. {
  72. if (! array_key_exists("transaction_id", $data)) {
  73. return false;
  74. }
  75. //查询订单,判断订单真实性
  76. if (! $this->queryOrder($data["transaction_id"])) {
  77. return false;
  78. }
  79. return true;
  80. }
  81. }