where('id', 1)->find(); if (!$user) throw new RuntimeException('Local test user missing'); $order = Db::name('order')->find(); if (!$order) throw new RuntimeException('Local test order template missing'); $amount = 0.37; $trade = 'BALTEST' . bin2hex(random_bytes(8)); unset($order['id']); $order['order_no'] = 'CANCELTEST' . bin2hex(random_bytes(6)); $order['user_id'] = 1; $order['status'] = 9; $order['pay_status'] = 1; $order['pay_amount'] = $amount; $order['refund_amount'] = 0; $orderId = Db::name('order')->insertGetId($order); $id = Db::name('pay_order')->insertGetId([ 'out_trade_no' => $trade, 'order_id' => $orderId, 'order_no' => $order['order_no'], 'user_id' => 1, 'scene' => 'order', 'openid' => 'BALANCE', 'amount' => $amount, 'status' => 'paid', 'refund_status' => 'none', ]); PaymentService::refundOrder($orderId, '取消订单退款测试'); PaymentService::refundOrder($orderId, '取消订单退款测试'); $summary = PaymentService::syncCancelledOrderRefund($orderId); $payment = Db::name('pay_order')->where('id', $id)->find(); $after = Db::name('user')->where('id', 1)->find(); $orderAfter = Db::name('order')->where('id', $orderId)->find(); if ($payment['status'] !== 'refunded' || abs((float)$payment['refund_amount'] - $amount) > 0.001 || abs((float)$after['money'] - (float)$user['money'] - $amount) > 0.001 || (int)$orderAfter['pay_status'] !== 2 || $summary['state'] !== 'success') { throw new RuntimeException('Balance refund was not returned exactly once'); } echo "Balance refund checks passed\n"; } finally { Db::rollback(); }