where('id',1)->update(['money'=>'70.00']); $pay = 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 = Service::create($pay, '30.00', '退款测试', 1); Service::begin($r['id'], 1, '同意'); $data = ['out_refund_no'=>$r['refund_no'],'refund_id'=>'WXTEST','refund_status'=>'SUCCESS','amount'=>['refund'=>3000,'total'=>10000]]; Service::applyResult($r['refund_no'], $data); Service::applyResult($r['refund_no'], $data); $bad = $data; $bad['amount']['refund'] = 1; try { Service::applyResult($r['refund_no'], $bad); throw new RuntimeException('终态接受不一致金额'); } catch (InvalidArgumentException $e) {} if ((float)Db::name('user')->where('id',1)->value('money') !== 40.0) throw new RuntimeException('退款成功后余额错误'); if ((float)Db::name('pay_order')->where('id',$pay)->value('refund_amount') !== 30.0) throw new RuntimeException('退款重复入账'); $r2 = Service::create($pay, '20.00', '退款测试', 1); Service::begin($r2['id'], 1, '同意'); Service::applyResult($r2['refund_no'], ['out_refund_no'=>$r2['refund_no'],'refund_id'=>'WXTEST2','refund_status'=>'ABNORMAL']); if ((float)Db::name('user')->where('id',1)->value('money') !== 20.0) throw new RuntimeException('异常退款不得释放余额'); try { Service::create($pay, '1.00', '重复退款', 1); throw new RuntimeException('异常退款期间允许新申请'); } catch (InvalidArgumentException $e) {} $closed = ['out_refund_no'=>$r2['refund_no'],'refund_id'=>'WXTEST2','refund_status'=>'CLOSED']; Service::applyResult($r2['refund_no'], $closed); Service::applyResult($r2['refund_no'], $closed); if ((float)Db::name('user')->where('id',1)->value('money') !== 40.0) throw new RuntimeException('关闭退款未正确释放余额'); echo "Recharge refund callback passed\n"; } finally { Db::rollback(); }