insertGetId([ 'out_trade_no' => $trade, 'order_id' => 2147483000, 'order_no' => 'REFUND-TEST', 'user_id' => 1, 'scene' => 'order', 'openid' => 'TEST', 'amount' => '10.00', 'status' => 'refunding', 'refund_no' => $refund, 'refund_status' => 'processing', ]); $data = ['status' => 'SUCCESS', 'refund_id' => 'WXREFUNDTEST', 'out_refund_no' => $refund, 'amount' => ['refund' => 400]]; $old = Db::name('pay_order')->where('id', $id)->find(); PaymentService::applyWxRefundResult($old, $data); $current = Db::name('pay_order')->where('id', $id)->find(); PaymentService::applyWxRefundResult($current, $data); $after = Db::name('pay_order')->where('id', $id)->find(); if ((float)$after['refund_amount'] !== 4.0 || $after['status'] !== 'paid') { throw new RuntimeException('Duplicate refund callback counted more than once'); } PaymentService::applyWxRefundResult($old, $data); $processing = $data; $processing['status'] = 'PROCESSING'; PaymentService::applyWxRefundResult($old, $processing); $after = Db::name('pay_order')->where('id', $id)->find(); if ((float)$after['refund_amount'] !== 4.0 || $after['status'] !== 'paid') { throw new RuntimeException('Stale callback changed an already completed refund'); } $id2 = Db::name('pay_order')->insertGetId([ 'out_trade_no' => $trade . 'B', 'order_id' => 2147483000, 'order_no' => 'REFUND-TEST', 'user_id' => 1, 'scene' => 'order', 'openid' => 'TEST', 'amount' => '10.00', 'status' => 'refunding', 'refund_no' => $refund . 'B', 'refund_status' => 'processing', ]); $failed = ['status' => 'ABNORMAL', 'refund_id' => 'WXFAILTEST', 'out_refund_no' => $refund . 'B']; try { PaymentService::applyWxRefundResult(Db::name('pay_order')->where('id', $id2)->find(), $failed); throw new RuntimeException('Abnormal refund was accepted'); } catch (RuntimeException $e) { if ($e->getMessage() === 'Abnormal refund was accepted') throw $e; } $failedRow = Db::name('pay_order')->where('id', $id2)->find(); if ($failedRow['status'] !== 'refund_failed' || (float)$failedRow['refund_amount'] !== 0.0) { throw new RuntimeException('Abnormal refund was not recorded correctly'); } echo "Refund callback idempotency checks passed\n"; } finally { Db::rollback(); }