Align avatar recharge plan IDs with WeChat virtual products
This commit is contained in:
@@ -57,6 +57,10 @@ def test_seed_synchronizes_requested_recharge_plans():
|
||||
db = SessionLocal()
|
||||
try:
|
||||
plans = db.query(TokenPlan).order_by(TokenPlan.price.asc()).all()
|
||||
assert {plan.id: plan.virtual_product_id for plan in plans} == {
|
||||
"Basic": "Basic", "Standard": "Standard",
|
||||
"Professional": "Professional", "Enterprise": "Enterprise",
|
||||
}
|
||||
assert [(plan.price, plan.amount) for plan in plans] == [
|
||||
(10, 2_000_000),
|
||||
(100, 20_000_000),
|
||||
@@ -67,6 +71,33 @@ def test_seed_synchronizes_requested_recharge_plans():
|
||||
db.close()
|
||||
|
||||
|
||||
def test_seed_preserves_admin_price_and_migrates_old_order_plan_id():
|
||||
seed()
|
||||
db = SessionLocal()
|
||||
order_no = f"TEST-MIGRATION-{uuid.uuid4().hex}"
|
||||
try:
|
||||
plan = db.query(TokenPlan).filter_by(id="Basic").one()
|
||||
original_price = plan.price
|
||||
plan.price = 0.01
|
||||
db.add(TokenPlan(id="1", name="基础套餐", amount=2_000_000, price=10))
|
||||
db.add(TokenPaymentOrder(
|
||||
order_no=order_no, user_id="test-user", plan_id="1",
|
||||
payment_method="wechat", pay_type="WECHAT", pay_way="JSAPI",
|
||||
points_amount=2_000_000, price_cents=1000, status="pending",
|
||||
))
|
||||
db.commit()
|
||||
seed()
|
||||
db.expire_all()
|
||||
assert db.query(TokenPlan).filter_by(id="1").first() is None
|
||||
assert db.query(TokenPlan).filter_by(id="Basic").one().price == 0.01
|
||||
assert db.query(TokenPaymentOrder).filter_by(order_no=order_no).one().plan_id == "Basic"
|
||||
finally:
|
||||
db.query(TokenPaymentOrder).filter_by(order_no=order_no).delete()
|
||||
db.query(TokenPlan).filter_by(id="Basic").update({"price": original_price})
|
||||
db.commit()
|
||||
db.close()
|
||||
|
||||
|
||||
def test_charge_creates_huihui_order_without_early_points(authorization_context):
|
||||
context = authorization_context
|
||||
_enable_huihui_payment_login(context)
|
||||
|
||||
@@ -30,8 +30,7 @@ def test_huihui_virtual_callback_requires_signature_and_exact_order_details(auth
|
||||
user = db.query(User).filter(User.id == context["owner"].id).one()
|
||||
user.wechat_mp_openid = "openid-huihui-callback"
|
||||
user.wechat_mp_session_key = "session-huihui-callback"
|
||||
plan = db.query(TokenPlan).filter(TokenPlan.id == "1").one()
|
||||
plan.virtual_product_id = "points_plan_1"
|
||||
plan = db.query(TokenPlan).filter(TokenPlan.id == "Basic").one()
|
||||
db.commit()
|
||||
finally:
|
||||
db.close()
|
||||
@@ -47,14 +46,14 @@ def test_huihui_virtual_callback_requires_signature_and_exact_order_details(auth
|
||||
created = client.post(
|
||||
"/api/token/charge",
|
||||
headers=context["owner_headers"],
|
||||
json={"planId": "1", "paymentMethod": "wechat", "payScene": "LITE"},
|
||||
json={"planId": "Basic", "paymentMethod": "wechat", "payScene": "LITE"},
|
||||
).json()["data"]
|
||||
payload = {
|
||||
"orderNo": created["orderNo"],
|
||||
"paymentNo": f"huihui-virtual-payment-{context['suffix']}",
|
||||
"status": "SUCCESS",
|
||||
"amountCents": 1000,
|
||||
"productId": "points_plan_1",
|
||||
"productId": "Basic",
|
||||
"openId": "openid-huihui-callback",
|
||||
"env": 1,
|
||||
}
|
||||
@@ -91,8 +90,7 @@ def test_virtual_payment_callback_and_refund_are_idempotent(authorization_contex
|
||||
user = db.query(User).filter(User.id == context["owner"].id).one()
|
||||
user.wechat_mp_openid = "openid-flow"
|
||||
user.wechat_mp_session_key = "session-flow"
|
||||
plan = db.query(TokenPlan).filter(TokenPlan.id == "1").one()
|
||||
plan.virtual_product_id = "points_plan_1"
|
||||
plan = db.query(TokenPlan).filter(TokenPlan.id == "Basic").one()
|
||||
db.commit()
|
||||
finally:
|
||||
db.close()
|
||||
@@ -108,7 +106,7 @@ def test_virtual_payment_callback_and_refund_are_idempotent(authorization_contex
|
||||
created = client.post(
|
||||
"/api/token/charge",
|
||||
headers=context["owner_headers"],
|
||||
json={"planId": "1", "paymentMethod": "wechat", "payScene": "LITE"},
|
||||
json={"planId": "Basic", "paymentMethod": "wechat", "payScene": "LITE"},
|
||||
).json()["data"]
|
||||
assert created["provider"] == "wechat_virtual"
|
||||
params = json.loads(created["payMessage"])
|
||||
@@ -120,7 +118,7 @@ def test_virtual_payment_callback_and_refund_are_idempotent(authorization_contex
|
||||
"OutTradeNo": created["orderNo"],
|
||||
"OpenId": "openid-flow",
|
||||
"Env": 1,
|
||||
"GoodsInfo": json.dumps({"ProductId": "points_plan_1", "ActualPrice": 1000}),
|
||||
"GoodsInfo": json.dumps({"ProductId": "Basic", "ActualPrice": 1000}),
|
||||
"WeChatPayInfo": json.dumps({"TransactionId": "wx-transaction-1"}),
|
||||
}
|
||||
query = {"timestamp": "100", "nonce": "nonce", "signature": _signature("callback-token", "100", "nonce")}
|
||||
|
||||
@@ -9,8 +9,8 @@ from services import wechat_virtual_payment as virtual
|
||||
|
||||
|
||||
def test_build_payment_params_signs_the_exact_compact_payload():
|
||||
order = SimpleNamespace(order_no="AV202609080001", plan_id="plan-1", price_cents=1000)
|
||||
plan = SimpleNamespace(id="plan-1", virtual_product_id="points_plan_1")
|
||||
order = SimpleNamespace(order_no="AV202609080001", plan_id="Basic", price_cents=1000)
|
||||
plan = SimpleNamespace(id="Basic", virtual_product_id="stale-product-id")
|
||||
env = {
|
||||
"WECHAT_VIRTUAL_ENV": "sandbox",
|
||||
"WECHAT_VIRTUAL_SANDBOX_APP_KEY": "sandbox-key",
|
||||
@@ -25,10 +25,10 @@ def test_build_payment_params_signs_the_exact_compact_payload():
|
||||
"buyQuantity": 1,
|
||||
"env": 1,
|
||||
"currencyType": "CNY",
|
||||
"productId": "points_plan_1",
|
||||
"productId": "Basic",
|
||||
"goodsPrice": 1000,
|
||||
"outTradeNo": "AV202609080001",
|
||||
"attach": '{"orderNo":"AV202609080001","planId":"plan-1"}',
|
||||
"attach": '{"orderNo":"AV202609080001","planId":"Basic"}',
|
||||
}, ensure_ascii=False, separators=(",", ":"))
|
||||
assert result["paySig"] == hmac.new(
|
||||
b"sandbox-key", f"requestVirtualPayment&{sign_data}".encode(), hashlib.sha256
|
||||
|
||||
Reference in New Issue
Block a user