Fix PayPal hosted checkout flow

This commit is contained in:
QLHazyCoder
2026-05-20 11:34:34 +08:00
parent 9dbbce77ad
commit 0b58e1116e
10 changed files with 328 additions and 52 deletions
+49
View File
@@ -408,6 +408,11 @@ function createHostedPayPalHarness(options = {}) {
text: 'Agree & Create Account',
attrs: { 'data-testid': 'createAccountButton' },
});
const nextButton = createDomElement({
tagName: 'BUTTON',
id: 'btnNext',
text: '下一页',
});
function setElements(nextElements) {
elements = nextElements;
@@ -450,6 +455,15 @@ function createHostedPayPalHarness(options = {}) {
setElements([createAccountButton]);
}
function showPayEmail() {
location.href = 'https://www.paypal.com/pay?token=demo';
location.host = 'www.paypal.com';
location.pathname = '/pay';
body.innerText = '请输入您的电子邮箱地址。 下一页 或 创建账户';
body.textContent = body.innerText;
setElements([emailInput, nextButton, createAccountButton]);
}
const context = {
console: { log() {}, warn() {}, error() {}, info() {} },
location,
@@ -541,6 +555,7 @@ function createHostedPayPalHarness(options = {}) {
return {
events,
send,
showPayEmail,
showCreateAccount,
showGuestCheckout,
};
@@ -610,6 +625,40 @@ test('PayPal hosted guest checkout blocks submit when rendered phone differs fro
assert.equal(harness.events.some((event) => event.type === 'click' && event.id === 'hostedSubmit'), false);
});
test('PayPal hosted /pay email page fills email and clicks Next instead of Create Account', async () => {
const harness = createHostedPayPalHarness();
harness.showPayEmail();
const state = await harness.send({
type: 'PAYPAL_HOSTED_GET_STATE',
source: 'test',
payload: {},
});
assert.equal(state.ok, true);
assert.equal(state.hostedStage, 'pay_login');
assert.equal(state.hasHostedEmailInput, true);
const result = await harness.send({
type: 'PAYPAL_RUN_HOSTED_CHECKOUT_STEP',
source: 'test',
payload: {
expectedStage: 'pay_login',
email: 'guest@example.com',
},
});
assert.equal(result.ok, true);
assert.equal(result.stage, 'pay_login');
assert.equal(result.submitted, true);
assert.equal(harness.events.some((event) => event.type === 'fill' && event.id === 'email' && event.value === 'guest@example.com'), true);
assert.equal(harness.events.some((event) => event.type === 'click' && event.id === 'btnNext'), true);
assert.equal(harness.events.some((event) => event.type === 'click' && event.id === 'createAccountButton'), false);
assert.deepEqual(
JSON.parse(JSON.stringify(harness.events.filter((event) => event.type === 'operation').map((event) => event.metadata))),
[{ stepKey: 'paypal-hosted-email', kind: 'click', label: 'hosted-paypal-email-next' }]
);
});
test('PayPal hosted create account page is detected and handled as its own step', async () => {
const harness = createHostedPayPalHarness();
harness.showCreateAccount();
+43
View File
@@ -566,6 +566,49 @@ return { findSubscribeButton };
assert.equal(api.findSubscribeButton(), submitButton);
});
test('getSubscribeButtonState does not treat processing subscribe text as clickable', () => {
const bundle = [
extractFunction('normalizeText'),
extractFunction('getActionText'),
extractFunction('getSearchText'),
extractFunction('getFieldText'),
extractFunction('getCombinedSearchText'),
extractFunction('isEnabledControl'),
extractFunction('isBusySubscribeButton'),
'const SUBSCRIBE_READY_TEXT_PATTERN = /\\u8ba2\\u9605|\\u7ee7\\u7eed|\\u786e\\u8ba4|\\u652f\\u4ed8|subscribe|continue|confirm|pay|\\u8d2d\\u4e70\\s*ChatGPT\\s*Plus|start\\s*subscription|place\\s*order/i;',
'const SUBSCRIBE_PROCESSING_TEXT_PATTERN = /\\u6b63\\u5728\\u5904\\u7406|\\u5904\\u7406\\u4e2d|\\u8bf7\\u7a0d\\u5019|\\u52a0\\u8f7d\\u4e2d|loading|processing|submitting/i;',
extractFunction('getSubscribeButtonState'),
].join('\n');
const processingButton = createElement({
tagName: 'BUTTON',
text: '订阅 正在处理',
attrs: { type: 'submit' },
});
const readyButton = createElement({
tagName: 'BUTTON',
text: '订阅',
attrs: { type: 'submit' },
});
const api = new Function(`
${bundle}
return { getSubscribeButtonState };
`)();
assert.deepEqual(
{
ready: api.getSubscribeButtonState(processingButton).ready,
status: api.getSubscribeButtonState(processingButton).status,
},
{
ready: false,
status: 'processing',
}
);
assert.equal(api.getSubscribeButtonState(readyButton).ready, true);
});
test('humanLikeClick submits a detached submit button through its form attribute', async () => {
const bundle = [
'function throwIfStopped() {}',
@@ -84,6 +84,7 @@ function createExecutorHarness({
getState = null,
queryTabsInAutomationWindow = null,
markCurrentRegistrationAccountUsed = async () => {},
onClickSubscribe = null,
probeIpProxyExit = null,
onSetState = null,
sleepWithStop = null,
@@ -137,7 +138,14 @@ function createExecutorHarness({
return stateByFrame[frameId] || { hasPayPal: false, paypalCandidates: [] };
}
if (message.type === 'PLUS_CHECKOUT_CLICK_SUBSCRIBE') {
checkoutTab.url = submitRedirectUrl;
if (typeof onClickSubscribe === 'function') {
const clickResult = await onClickSubscribe({ checkoutTab, events, frameId, message, tabId });
if (clickResult !== undefined) {
return clickResult;
}
} else {
checkoutTab.url = submitRedirectUrl;
}
}
return createSuccessfulBillingResult();
},
@@ -242,6 +250,57 @@ test('Plus checkout billing uses the current checkout tab when step 6 did not re
assert.equal(events.logs.some((entry) => /当前已在 Plus Checkout 页面/.test(entry.message)), true);
});
test('Plus checkout billing waits on processing subscribe text before clicking a ready subscribe button again', async () => {
const originalNow = Date.now;
let now = 0;
let clickCalls = 0;
Date.now = () => now;
try {
const { events, executor } = createExecutorHarness({
frames: [{ frameId: 0, url: 'https://chatgpt.com/checkout/openai_ie/cs_test' }],
stateByFrame: {
0: {
hasPayPal: true,
paypalCandidates: [{ tag: 'button', text: 'PayPal' }],
billingFieldsVisible: true,
hasSubscribeButton: true,
},
},
onClickSubscribe: async ({ checkoutTab }) => {
clickCalls += 1;
if (clickCalls === 1) {
return {
clicked: false,
subscribeButtonStatus: 'processing',
subscribeButtonText: '订阅正在处理',
};
}
checkoutTab.url = 'https://www.paypal.com/checkoutnow';
return {
clicked: true,
subscribeButtonStatus: 'clicked',
subscribeButtonText: '订阅',
};
},
sleepWithStop: async (ms) => {
events.sleeps.push(ms);
now += ms;
},
});
await executor.executePlusCheckoutBilling({});
const subscribeMessages = events.messages.filter((entry) => entry.message.type === 'PLUS_CHECKOUT_CLICK_SUBSCRIBE');
assert.equal(subscribeMessages.length, 2);
assert.equal(subscribeMessages.some((entry) => entry.message.payload.allowBusySubscribeButton !== undefined), false);
assert.equal(events.sleeps.filter((ms) => ms === 500).length >= 20, true);
assert.equal(events.logs.some((entry) => /本轮未点击/.test(entry.message)), true);
assert.equal(events.completed[0].step, 'plus-checkout-billing');
} finally {
Date.now = originalNow;
}
});
test('Plus checkout billing searches checkout tabs inside the locked automation window', async () => {
const queries = [];
const { checkoutTab, executor } = createExecutorHarness({
+33 -6
View File
@@ -288,7 +288,7 @@ test('GoPay plus checkout create forwards gopay payment method to the checkout c
assert.deepStrictEqual(events[0]?.payload, { paymentMethod: 'gopay' });
});
test('PayPal no-card binding create only opens hosted checkout and leaves page steps to later nodes', async () => {
test('PayPal no-card binding create opens and submits hosted OpenAI checkout before completing', async () => {
const events = [];
let currentUrl = 'https://chatgpt.com/';
const executor = api.createPlusCheckoutCreateExecutor({
@@ -315,8 +315,27 @@ test('PayPal no-card binding create only opens hosted checkout and leaves page s
ensureContentScriptReadyOnTabUntilStopped: async (source, tabId, options) => {
events.push({ type: 'ready', source, tabId, options });
},
fetch: async () => {
throw new Error('create node should not fetch address or run PayPal page automation');
fetch: async (url) => {
events.push({ type: 'fetch', url });
assert.equal(url, 'https://www.meiguodizhi.com/api/v1/dz');
return {
ok: true,
status: 200,
json: async () => ({
address: {
Address: '1 Main St',
City: 'New York',
State: 'New York',
Zip_Code: '10001',
},
}),
};
},
getState: async () => {
events.push({ type: 'get-state' });
return {
hostedCheckoutPhoneNumber: '4155551234',
};
},
registerTab: async (source, tabId) => {
events.push({ type: 'register', source, tabId });
@@ -332,6 +351,10 @@ test('PayPal no-card binding create only opens hosted checkout and leaves page s
currency: 'USD',
};
}
if (message.type === 'RUN_PAYPAL_HOSTED_OPENAI_CHECKOUT_STEP') {
currentUrl = 'https://www.paypal.com/pay?token=BA-hosted';
return { clicked: true };
}
throw new Error(`unexpected message type ${message.type}`);
},
setState: async (payload) => {
@@ -358,13 +381,17 @@ test('PayPal no-card binding create only opens hosted checkout and leaves page s
events.find((event) => event.type === 'tab-update')?.payload?.url,
'https://pay.openai.com/c/pay/cs_hosted'
);
const statePayload = events.find((event) => event.type === 'set-state')?.payload || {};
const statePayload = events.filter((event) => event.type === 'set-state').at(-1)?.payload || {};
assert.equal(statePayload.plusCheckoutSource, 'paypal-hosted');
assert.equal(statePayload.plusCheckoutCountry, 'US');
assert.equal(statePayload.plusCheckoutCurrency, 'USD');
assert.equal(statePayload.plusReturnUrl, '');
assert.equal(events.some((event) => event.type === 'tab-message' && event.message.type === 'FILL_PLUS_BILLING_AND_SUBMIT'), false);
assert.equal(events.some((event) => event.type === 'tab-message' && event.message.type === 'RUN_PAYPAL_HOSTED_OPENAI_CHECKOUT_STEP'), false);
assert.equal(events.some((event) => event.type === 'tab-message' && event.message.type === 'RUN_PAYPAL_HOSTED_OPENAI_CHECKOUT_STEP'), true);
assert.equal(
events.find((event) => event.type === 'tab-message' && event.message.type === 'RUN_PAYPAL_HOSTED_OPENAI_CHECKOUT_STEP')?.message?.payload?.address?.street,
'1 Main St'
);
assert.deepStrictEqual(events.find((event) => event.type === 'complete'), {
type: 'complete',
step: 'plus-checkout-create',
@@ -372,7 +399,7 @@ test('PayPal no-card binding create only opens hosted checkout and leaves page s
plusCheckoutCountry: 'US',
plusCheckoutCurrency: 'USD',
plusCheckoutSource: 'paypal-hosted',
plusCheckoutUrl: 'https://pay.openai.com/c/pay/cs_hosted',
plusCheckoutUrl: 'https://www.paypal.com/pay?token=BA-hosted',
plusReturnUrl: '',
plusHostedCheckoutCompleted: false,
},
+5 -6
View File
@@ -221,7 +221,6 @@ test('step definitions module exposes ordered normal and Plus step metadata', ()
'fetch-signup-code',
'fill-profile',
'plus-checkout-create',
'paypal-hosted-openai-checkout',
'paypal-hosted-email',
'paypal-hosted-verification',
'paypal-hosted-card',
@@ -236,10 +235,10 @@ test('step definitions module exposes ordered normal and Plus step metadata', ()
assert.equal(hostedSteps.some((step) => step.key === 'plus-checkout-billing'), false);
assert.equal(hostedSteps.some((step) => step.key === 'paypal-approve'), false);
assert.equal(hostedSteps.some((step) => step.key === 'plus-checkout-return'), false);
assert.equal(hostedSteps.find((step) => step.key === 'paypal-hosted-openai-checkout')?.title, '无卡直绑提交 OpenAI Checkout');
assert.equal(hostedSteps.some((step) => step.key === 'paypal-hosted-openai-checkout'), false);
assert.equal(hostedSteps.find((step) => step.key === 'paypal-hosted-card')?.title, '无卡直绑填写 PayPal 资料');
assert.deepStrictEqual(api.getStepIds({ plusModeEnabled: true, plusPaymentMethod: 'paypal-hosted' }), [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16]);
assert.equal(api.getLastStepId({ plusModeEnabled: true, plusPaymentMethod: 'paypal-hosted' }), 16);
assert.deepStrictEqual(api.getStepIds({ plusModeEnabled: true, plusPaymentMethod: 'paypal-hosted' }), [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15]);
assert.equal(api.getLastStepId({ plusModeEnabled: true, plusPaymentMethod: 'paypal-hosted' }), 15);
assert.deepStrictEqual(
goPaySteps.map((step) => step.key),
@@ -317,7 +316,7 @@ test('Plus session strategy swaps the OAuth tail for a single SUB2API import nod
plusAccountAccessStrategy: 'sub2api_codex_session',
},
previousNodeId: 'paypal-hosted-review',
expectedStepIds: [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13],
expectedStepIds: [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12],
},
{
label: 'gopay',
@@ -408,7 +407,7 @@ test('Plus session strategy swaps the OAuth tail for a single CPA import node',
plusAccountAccessStrategy: 'cpa_codex_session',
},
previousNodeId: 'paypal-hosted-review',
expectedStepIds: [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13],
expectedStepIds: [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12],
},
{
label: 'gopay',