53 行
2.7 KiB
PHP
53 行
2.7 KiB
PHP
<?php
|
|
define('APP_PATH', __DIR__ . '/../server/application/');
|
|
require __DIR__ . '/../server/thinkphp/base.php';
|
|
\think\App::initCommon();
|
|
use app\common\library\PaymentService;
|
|
use think\Db;
|
|
|
|
Db::startTrans();
|
|
try {
|
|
$trade = 'TEST' . bin2hex(random_bytes(10));
|
|
$refund = 'RF' . bin2hex(random_bytes(10));
|
|
$id = Db::name('pay_order')->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();
|
|
}
|