diff --git a/packages/connector-alipay-native/src/index.ts b/packages/connector-alipay-native/src/index.ts index 6e83850b6..5f1aa70d6 100644 --- a/packages/connector-alipay-native/src/index.ts +++ b/packages/connector-alipay-native/src/index.ts @@ -41,6 +41,20 @@ import { signingParameters } from './utils'; export type { AlipayNativeConfig } from './types'; +type CodePayload = { + auth_code: string; +}; + +const parseCodeFromJson = (json: string): string => { + try { + const { auth_code } = JSON.parse(json) as CodePayload; + + return auth_code; + } catch { + return json; + } +}; + export default class AlipayNativeConnector implements SocialConnector { public metadata: ConnectorMetadata = defaultMetadata; @@ -72,7 +86,7 @@ export default class AlipayNativeConnector implements SocialConnector { timestamp: dayjs().format('YYYY-MM-DD HH:mm:ss'), version: '1.0', grant_type: 'authorization_code', - code, + code: parseCodeFromJson(code), charset: 'UTF8', ...config, }; diff --git a/packages/connector-alipay/src/index.ts b/packages/connector-alipay/src/index.ts index 6ca1530cd..50654ddd9 100644 --- a/packages/connector-alipay/src/index.ts +++ b/packages/connector-alipay/src/index.ts @@ -35,6 +35,20 @@ import { signingParameters } from './utils'; export type { AlipayConfig } from './types'; +type CodePayload = { + auth_code: string; +}; + +const parseCodeFromJson = (json: string): string => { + try { + const { auth_code } = JSON.parse(json) as CodePayload; + + return auth_code; + } catch { + return json; + } +}; + export default class AlipayConnector implements SocialConnector { public metadata: ConnectorMetadata = defaultMetadata; @@ -73,7 +87,7 @@ export default class AlipayConnector implements SocialConnector { timestamp: this.getTimestamp(), version: '1.0', grant_type: 'authorization_code', - code, + code: parseCodeFromJson(code), charset: 'UTF8', ...config, };