| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899 |
- <?php
- require_once "WxPay.Api.php";
- require_once 'WxPay.Notify.php';
- class PayNotifyCallBack extends WxPayNotify
- {
- /**
- * 查询订单
- * @param $transactionId string
- * @param string $orderSn string
- * @return bool
- */
- public function queryOrder($transactionId, $orderSn = '')
- {
- $input = new WxPayOrderQuery();
- if ($transactionId) {
- $input->SetTransaction_id($transactionId);
- } else if ($orderSn) {
- $input->SetOut_trade_no($orderSn);
- }
- $result = WxPayApi::orderQuery($input);
- if(array_key_exists("return_code", $result)
- && array_key_exists("result_code", $result)
- && array_key_exists('trade_state', $result)
- && $result["return_code"] == "SUCCESS"
- && $result["result_code"] == "SUCCESS"
- )
- {
- $orderSn = $result['out_trade_no']; // 订单号
- $ttbPay = model('TtbAutoList');
- $orderInfo = $ttbPay->fetchOrderInfo($orderSn);
- if (empty($orderInfo)) {
- return false;
- }
- if ($result["trade_state"] == "SUCCESS") {
- // 支付成功
- if ($orderInfo['pay_status'] == 1) {
- return true;
- }
- //if (array_key_exists('total_fee', $result) && $result['total_fee'] == $orderInfo['pay_amount'] * 100) {
- // 支付金额
- $payAmount = sprintf('%.2f', $result['total_fee'] / 100);
- // 更新订单状态,用户账户余额,账户变化日志
- return $ttbPay->paySuccess($orderSn, $result['transaction_id']);
- //}
- } elseif ($result["trade_state"] == "CLOSED") {
- // 已关闭
- if ($orderInfo['pay_status'] == 3) {
- return 'CLOSED';
- }
- $ttbPay->updateOrder(
- ['pay_status' => 3, 'update_time' => NOW_TIMESTAMP],
- ['order_sn' => $orderSn]
- );
- return 'CLOSED';
- } elseif ($result["trade_state"] == "PAYERROR") {
- // 支付失败
- if ($orderInfo['pay_status'] == 2) {
- return 'CLOSED';
- }
- $ttbPay->updateOrder(
- ['pay_status' => 2, 'update_time' => NOW_TIMESTAMP],
- ['order_sn' => $orderSn]
- );
- return 'CLOSED';
- }
- }
- return false;
- }
- //重写回调处理函数
- public function NotifyProcess($data, &$msg)
- {
- if (! array_key_exists("transaction_id", $data)) {
- return false;
- }
- //查询订单,判断订单真实性
- if (! $this->queryOrder($data["transaction_id"])) {
- return false;
- }
- return true;
- }
- }
|