36 行
2.5 KiB
PHP
36 行
2.5 KiB
PHP
<?php
|
|
namespace app\common\library {
|
|
class WxPayV3Service {
|
|
public static $calls = 0;
|
|
public function refund(array $params) {
|
|
self::$calls++;
|
|
if (self::$calls === 1) throw new \RuntimeException('TIMEOUT');
|
|
return ['out_refund_no'=>$params['out_refund_no'], 'refund_id'=>'WX-TEST', 'status'=>'SUCCESS', 'amount'=>['refund'=>(int)round((float)$params['refund_amount']*100), 'total'=>(int)round((float)$params['total_amount']*100)]];
|
|
}
|
|
public function queryRefundByOutRefundNo($no) { throw new \RuntimeException('RESOURCE_NOT_EXISTS'); }
|
|
public static function extractFailInfo($e) { return ['code'=>$e->getMessage(), 'reason'=>$e->getMessage()]; }
|
|
}
|
|
}
|
|
namespace {
|
|
define('APP_PATH', __DIR__ . '/../server/application/');
|
|
require __DIR__ . '/../server/thinkphp/base.php';
|
|
\think\App::initCommon();
|
|
\think\Db::startTrans();
|
|
try {
|
|
\think\Db::name('user')->where('id',1)->update(['money'=>'50.00']);
|
|
$id=\think\Db::name('pay_order')->insertGetId(['out_trade_no'=>'TEST'.bin2hex(random_bytes(10)), 'order_id'=>0,'order_no'=>'','user_id'=>1,'scene'=>'recharge','openid'=>'TEST','amount'=>'100.00','status'=>'paid']);
|
|
$r=\app\common\library\RechargeRefundService::create($id,'20.00','客服退款',1);
|
|
try { \app\common\library\RechargeRefundService::submit($r['id'],1); throw new \Exception('Expected timeout'); }
|
|
catch (\RuntimeException $e) { if ($e->getMessage() !== 'TIMEOUT') throw $e; }
|
|
$row=\think\Db::name('recharge_refund')->where('id',$r['id'])->find();
|
|
if ($row['status'] !== 'unknown' || (float)\think\Db::name('user')->where('id',1)->value('money') !== 30.0) throw new \Exception('Timeout failed to preserve reservation');
|
|
\app\common\library\RechargeRefundService::send($r['id']);
|
|
$row=\think\Db::name('recharge_refund')->where('id',$r['id'])->find();
|
|
if ($row['status'] !== 'success' || (float)\think\Db::name('pay_order')->where('id',$id)->value('refund_amount') !== 20.0) throw new \Exception('Retry failed');
|
|
$next = \app\common\library\RechargeRefundService::create($id,'10.00','客服退款',1);
|
|
$done = \app\common\library\RechargeRefundService::submit($next['id'],1);
|
|
if ($done['status'] !== 'success' || (float)\think\Db::name('pay_order')->where('id',$id)->value('refund_amount') !== 30.0) throw new \Exception('Direct submission failed');
|
|
echo "Recharge gateway timeout and retry passed\n";
|
|
} finally { \think\Db::rollback(); }
|
|
}
|