@alipay/ams-checkout 2.0.16 → 2.0.18

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -33,6 +33,7 @@ import { parseSessionData } from "../../index";
33
33
  import { oneAccount, sdkAction } from "../mock";
34
34
  import { generateIframeSrc as _generateIframeSrc, isElementPad, isElementPC, safeStringify } from "../util";
35
35
  import BaseElementProcessor from "./baseElementProcessor";
36
+ import { executeWithTimeout, BEFORE_CONFIRM_TIMEOUT_MS } from "../../../../util/beforeConfirm";
36
37
  var logger = new Logger(LogConfig, true);
37
38
  var isExpressCheckout = function isExpressCheckout(extendInfo) {
38
39
  try {
@@ -56,9 +57,32 @@ var PaymentProcessor = /*#__PURE__*/function (_BaseElementProcessor) {
56
57
  // parameterInitAndCheck(): void {}
57
58
  // apple pay
58
59
  _defineProperty(_assertThisInitialized(_this), "ApplePayService", void 0);
60
+ /**
61
+ * Merchant's onBeforeConfirm callback reference, used in Apple Pay scenario only.
62
+ * Set by elementController.submitPayment before payment,
63
+ * triggered via beforeConfirm event after Apple Pay authorization in iframe.
64
+ */
65
+ _defineProperty(_assertThisInitialized(_this), "_onBeforeConfirm", null);
66
+ _defineProperty(_assertThisInitialized(_this), "_merchantAmount", null);
67
+ _defineProperty(_assertThisInitialized(_this), "_merchantCurrency", null);
59
68
  return _this;
60
69
  }
61
70
  _createClass(PaymentProcessor, [{
71
+ key: "setOnBeforeConfirm",
72
+ value: function setOnBeforeConfirm(callback) {
73
+ this._onBeforeConfirm = callback;
74
+ // 展码链路:同步设置 onBeforeConfirm 到 ApplePaySdk,确保 Chrome 展码支付时能调用商户回调
75
+ if (this.ApplePayService) {
76
+ this.ApplePayService.setOnBeforeConfirm(callback);
77
+ }
78
+ }
79
+ }, {
80
+ key: "setMerchantAmountOverride",
81
+ value: function setMerchantAmountOverride(amount, currency) {
82
+ this._merchantAmount = amount;
83
+ this._merchantCurrency = currency;
84
+ }
85
+ }, {
62
86
  key: "onReady",
63
87
  value: function onReady(extraParam) {
64
88
  var _this$elementContaine, _this$elementContaine2, _this$elementContaine3, _this$elementContaine4, _this$elementContaine5;
@@ -143,8 +167,12 @@ var PaymentProcessor = /*#__PURE__*/function (_BaseElementProcessor) {
143
167
  parseData: paymentSessionObj,
144
168
  logger: logger,
145
169
  env: _this2.options.env.environment,
146
- shipping: shipping
170
+ shipping: shipping,
171
+ merchantAmount: _this2._merchantAmount,
172
+ merchantCurrency: _this2._merchantCurrency
147
173
  });
174
+ // Chrome 展码链路:同步 onBeforeConfirm 到当前 ApplePaySdk 实例
175
+ applePayService.setOnBeforeConfirm(_this2._onBeforeConfirm);
148
176
  applePayService.startPay().then(function (res) {
149
177
  _this2.eventCenter.dispatchToApp({
150
178
  event: 'getApplePayToken',
@@ -163,19 +191,107 @@ var PaymentProcessor = /*#__PURE__*/function (_BaseElementProcessor) {
163
191
  });
164
192
  });
165
193
  });
194
+ // Apple Pay event bridge: after user authorization (FaceID/TouchID) in iframe, before actual charge,
195
+ // requests SDK layer to execute merchant's onBeforeConfirm callback via beforeConfirm event,
196
+ // and returns the result (continue/abort) to iframe via dispatchToApp.
197
+ this.eventCenter.listen(EVENT.beforeConfirm.name, /*#__PURE__*/_asyncToGenerator( /*#__PURE__*/_regeneratorRuntime().mark(function _callee() {
198
+ var paymentMethodType, startTime, _yield$executeWithTim, rawResult, timedOut, shouldContinue;
199
+ return _regeneratorRuntime().wrap(function _callee$(_context) {
200
+ while (1) switch (_context.prev = _context.next) {
201
+ case 0:
202
+ paymentMethodType = 'APPLEPAY';
203
+ if (_this2._onBeforeConfirm) {
204
+ _context.next = 5;
205
+ break;
206
+ }
207
+ logger.logInfo({
208
+ title: 'sdk_before_confirm_skip'
209
+ }, {
210
+ paymentMethodType: paymentMethodType,
211
+ path: 'applePay_eventBridge'
212
+ });
213
+ _this2.eventCenter.dispatchToApp({
214
+ event: EVENT.beforeConfirm.name,
215
+ data: {
216
+ success: true,
217
+ result: true
218
+ }
219
+ });
220
+ return _context.abrupt("return");
221
+ case 5:
222
+ logger.logInfo({
223
+ title: 'sdk_before_confirm_start'
224
+ }, {
225
+ paymentMethodType: paymentMethodType,
226
+ path: 'applePay_eventBridge'
227
+ });
228
+ _context.prev = 6;
229
+ startTime = Date.now();
230
+ _context.next = 10;
231
+ return executeWithTimeout(function () {
232
+ return _this2._onBeforeConfirm();
233
+ });
234
+ case 10:
235
+ _yield$executeWithTim = _context.sent;
236
+ rawResult = _yield$executeWithTim.result;
237
+ timedOut = _yield$executeWithTim.timedOut;
238
+ shouldContinue = rawResult; // 双重校验:timedOut(Promise race) + startTime(实际时间差),防止竞态误判
239
+ if (timedOut || Date.now() - startTime >= BEFORE_CONFIRM_TIMEOUT_MS) {
240
+ logger.logError({
241
+ title: 'sdk_before_confirm_timeout'
242
+ });
243
+ shouldContinue = false;
244
+ }
245
+ logger.logInfo({
246
+ title: 'sdk_element_beforeConfirm_callback_result'
247
+ }, {
248
+ shouldContinue: shouldContinue
249
+ });
250
+ _this2.eventCenter.dispatchToApp({
251
+ event: EVENT.beforeConfirm.name,
252
+ data: {
253
+ success: true,
254
+ result: !!shouldContinue
255
+ }
256
+ });
257
+ _context.next = 23;
258
+ break;
259
+ case 19:
260
+ _context.prev = 19;
261
+ _context.t0 = _context["catch"](6);
262
+ logger.logError({
263
+ title: 'sdk_before_confirm_error'
264
+ }, {
265
+ paymentMethodType: paymentMethodType,
266
+ errorMessage: String((_context.t0 === null || _context.t0 === void 0 ? void 0 : _context.t0.message) || _context.t0).slice(0, 200),
267
+ path: 'applePay_eventBridge'
268
+ });
269
+ _this2.eventCenter.dispatchToApp({
270
+ event: EVENT.beforeConfirm.name,
271
+ data: {
272
+ success: false,
273
+ result: false
274
+ }
275
+ });
276
+ case 23:
277
+ case "end":
278
+ return _context.stop();
279
+ }
280
+ }, _callee, null, [[6, 19]]);
281
+ })));
166
282
  }
167
283
  }, {
168
284
  key: "initApplePaySdk",
169
285
  value: function () {
170
- var _initApplePaySdk = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime().mark(function _callee() {
286
+ var _initApplePaySdk = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime().mark(function _callee2() {
171
287
  var _this$elementContaine6;
172
- var _ref, paymentSessionObj, paymentSession;
173
- return _regeneratorRuntime().wrap(function _callee$(_context) {
174
- while (1) switch (_context.prev = _context.next) {
288
+ var _ref2, paymentSessionObj, paymentSession;
289
+ return _regeneratorRuntime().wrap(function _callee2$(_context2) {
290
+ while (1) switch (_context2.prev = _context2.next) {
175
291
  case 0:
176
- _ref = ((_this$elementContaine6 = this.elementContainer) === null || _this$elementContaine6 === void 0 ? void 0 : _this$elementContaine6.getPaymentContext()) || {}, paymentSessionObj = _ref.paymentSessionObj, paymentSession = _ref.paymentSession;
292
+ _ref2 = ((_this$elementContaine6 = this.elementContainer) === null || _this$elementContaine6 === void 0 ? void 0 : _this$elementContaine6.getPaymentContext()) || {}, paymentSessionObj = _ref2.paymentSessionObj, paymentSession = _ref2.paymentSession;
177
293
  if (!isSkipRenderPaymentMethod(paymentSessionObj)) {
178
- _context.next = 13;
294
+ _context2.next = 13;
179
295
  break;
180
296
  }
181
297
  this.ApplePayService = new ApplePaySdk({
@@ -184,29 +300,29 @@ var PaymentProcessor = /*#__PURE__*/function (_BaseElementProcessor) {
184
300
  logger: logger,
185
301
  env: this.options.env.environment
186
302
  });
187
- _context.prev = 3;
303
+ _context2.prev = 3;
188
304
  logger.logInfo({
189
305
  title: 'a3753.b107385.c398110'
190
306
  });
191
- _context.next = 7;
307
+ _context2.next = 7;
192
308
  return this.ApplePayService.initApplePaySession();
193
309
  case 7:
194
310
  logger.logInfo({
195
311
  title: 'a3753.b107385.c398112'
196
312
  });
197
- _context.next = 13;
313
+ _context2.next = 13;
198
314
  break;
199
315
  case 10:
200
- _context.prev = 10;
201
- _context.t0 = _context["catch"](3);
316
+ _context2.prev = 10;
317
+ _context2.t0 = _context2["catch"](3);
202
318
  logger.logError({
203
319
  title: 'a3753.b107385.c398111'
204
320
  });
205
321
  case 13:
206
322
  case "end":
207
- return _context.stop();
323
+ return _context2.stop();
208
324
  }
209
- }, _callee, this, [[3, 10]]);
325
+ }, _callee2, this, [[3, 10]]);
210
326
  }));
211
327
  function initApplePaySdk() {
212
328
  return _initApplePaySdk.apply(this, arguments);
@@ -227,20 +343,20 @@ var PaymentProcessor = /*#__PURE__*/function (_BaseElementProcessor) {
227
343
  }
228
344
  }, {
229
345
  key: "generateIframeSrc",
230
- value: function generateIframeSrc(_ref2) {
231
- var link = _ref2.link,
232
- instanceId = _ref2.instanceId;
346
+ value: function generateIframeSrc(_ref3) {
347
+ var link = _ref3.link,
348
+ instanceId = _ref3.instanceId;
233
349
  this.instanceId = instanceId;
234
- var _ref3 = this.elementContainer.getPaymentContext() || {},
235
- paymentSession = _ref3.paymentSession,
236
- paymentSessionObj = _ref3.paymentSessionObj,
237
- sdkMetaData = _ref3.sdkMetaData;
350
+ var _ref4 = this.elementContainer.getPaymentContext() || {},
351
+ paymentSession = _ref4.paymentSession,
352
+ paymentSessionObj = _ref4.paymentSessionObj,
353
+ sdkMetaData = _ref4.sdkMetaData;
238
354
  var _this$options = this.options,
239
355
  environment = _this$options.env.environment,
240
356
  analytics = _this$options.analytics,
241
357
  locale = _this$options.locale;
242
- var _ref4 = paymentSessionObj || {},
243
- extendInfo = _ref4.extendInfo;
358
+ var _ref5 = paymentSessionObj || {},
359
+ extendInfo = _ref5.extendInfo;
244
360
  var url = _generateIframeSrc({
245
361
  paymentSession: paymentSession,
246
362
  paymentSessionObj: paymentSessionObj,
@@ -258,19 +374,19 @@ var PaymentProcessor = /*#__PURE__*/function (_BaseElementProcessor) {
258
374
  }, {
259
375
  key: "obtainData",
260
376
  value: function () {
261
- var _obtainData = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime().mark(function _callee2() {
377
+ var _obtainData = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime().mark(function _callee3() {
262
378
  var _paymentSessionObj$co,
263
379
  _this3 = this;
264
- var requestService, _this$elementContaine7, paymentSession, paymentSessionObj, displayInfo, _displayInfo, debugProps, _this$options2, environment, locale, _ref5, paymentSessionConfig, hostSign, isConnect, LOCAL_MOCK, _queryParse, appType, terminalType, generateActionQueryPromise, generateOneAccountQueryPromise, _yield$Promise$all, _yield$Promise$all2, originActionQueryResult, originOneAccountQueryResult;
265
- return _regeneratorRuntime().wrap(function _callee2$(_context2) {
266
- while (1) switch (_context2.prev = _context2.next) {
380
+ var requestService, _this$elementContaine7, paymentSession, paymentSessionObj, displayInfo, _displayInfo, debugProps, _this$options2, environment, locale, _ref6, paymentSessionConfig, hostSign, isConnect, LOCAL_MOCK, _queryParse, appType, terminalType, generateActionQueryPromise, generateOneAccountQueryPromise, _yield$Promise$all, _yield$Promise$all2, originActionQueryResult, originOneAccountQueryResult;
381
+ return _regeneratorRuntime().wrap(function _callee3$(_context3) {
382
+ while (1) switch (_context3.prev = _context3.next) {
267
383
  case 0:
268
384
  requestService = ServiceProvider.getInstance(this.instanceId).getService('Requester');
269
385
  _this$elementContaine7 = this.elementContainer.getPaymentContext(), paymentSession = _this$elementContaine7.paymentSession, paymentSessionObj = _this$elementContaine7.paymentSessionObj, displayInfo = _this$elementContaine7.displayInfo;
270
386
  _displayInfo = displayInfo;
271
387
  debugProps = _displayInfo.debugProps;
272
388
  _this$options2 = this.options, environment = _this$options2.env.environment, locale = _this$options2.locale;
273
- _ref5 = paymentSessionObj || {}, paymentSessionConfig = _ref5.paymentSessionConfig;
389
+ _ref6 = paymentSessionObj || {}, paymentSessionConfig = _ref6.paymentSessionConfig;
274
390
  hostSign = paymentSession.split('&&')[1] || '';
275
391
  isConnect = paymentSessionObj === null || paymentSessionObj === void 0 || (_paymentSessionObj$co = paymentSessionObj.connectFactor) === null || _paymentSessionObj$co === void 0 ? void 0 : _paymentSessionObj$co.enableConnect;
276
392
  LOCAL_MOCK = (debugProps === null || debugProps === void 0 ? void 0 : debugProps.isDebug) && (debugProps === null || debugProps === void 0 ? void 0 : debugProps.local_mock);
@@ -407,10 +523,10 @@ var PaymentProcessor = /*#__PURE__*/function (_BaseElementProcessor) {
407
523
  });
408
524
  });
409
525
  };
410
- _context2.next = 15;
526
+ _context3.next = 15;
411
527
  return Promise.all(isConnect ? [generateActionQueryPromise(), generateOneAccountQueryPromise()] : [generateActionQueryPromise()]);
412
528
  case 15:
413
- _yield$Promise$all = _context2.sent;
529
+ _yield$Promise$all = _context3.sent;
414
530
  _yield$Promise$all2 = _slicedToArray(_yield$Promise$all, 2);
415
531
  originActionQueryResult = _yield$Promise$all2[0];
416
532
  originOneAccountQueryResult = _yield$Promise$all2[1];
@@ -418,15 +534,15 @@ var PaymentProcessor = /*#__PURE__*/function (_BaseElementProcessor) {
418
534
  originActionQueryResult: originActionQueryResult,
419
535
  originOneAccountQueryResult: originOneAccountQueryResult
420
536
  };
421
- return _context2.abrupt("return", {
537
+ return _context3.abrupt("return", {
422
538
  originActionQueryResult: originActionQueryResult,
423
539
  originOneAccountQueryResult: originOneAccountQueryResult
424
540
  });
425
541
  case 21:
426
542
  case "end":
427
- return _context2.stop();
543
+ return _context3.stop();
428
544
  }
429
- }, _callee2, this);
545
+ }, _callee3, this);
430
546
  }));
431
547
  function obtainData() {
432
548
  return _obtainData.apply(this, arguments);
@@ -441,9 +557,9 @@ var PaymentProcessor = /*#__PURE__*/function (_BaseElementProcessor) {
441
557
  }
442
558
  }, {
443
559
  key: "update",
444
- value: function update(_ref6) {
445
- var data = _ref6.data,
446
- paymentSessionData = _ref6.paymentSessionData;
560
+ value: function update(_ref7) {
561
+ var data = _ref7.data,
562
+ paymentSessionData = _ref7.paymentSessionData;
447
563
  var _parseSessionData = parseSessionData(paymentSessionData),
448
564
  _parseSessionData2 = _slicedToArray(_parseSessionData, 2),
449
565
  parseData = _parseSessionData2[0],
@@ -287,6 +287,26 @@ export interface AddressSubmitData {
287
287
  export interface SubmitFuncParams {
288
288
  handleActions?: boolean;
289
289
  shippingInfo?: ShippingInfo;
290
+ /**
291
+ * Amount displayed on the Apple Pay payment sheet, e.g. '10.99'.
292
+ * Display only; actual charge amount is controlled by server.
293
+ * Must be provided together with `currency`. Only effective for Apple Pay.
294
+ */
295
+ amount?: string;
296
+ /**
297
+ * Currency code displayed on the Apple Pay payment sheet, e.g. 'USD'.
298
+ * Follows ISO 4217 standard. Must be provided together with `amount`. Only effective for Apple Pay.
299
+ */
300
+ currency?: string;
301
+ /**
302
+ * Pre-confirmation callback. Executed after user authorization but before actual charge,
303
+ * allowing merchant to create order at this point.
304
+ * - Only effective when payment method is Apple Pay
305
+ * - Invoked via event bridge after user FaceID/TouchID authorization on the payment sheet
306
+ * - Return true: proceed with charge
307
+ * - Return false / timeout / throw error: abort payment, returns MERCHANT_CONFIRM_ABORT error code
308
+ */
309
+ onBeforeConfirm?: () => Promise<boolean>;
290
310
  }
291
311
  export interface SubmitServiceParams extends PaymentSubmitData {
292
312
  accountInfo: AuthSubmitData['accountInfo'];
@@ -1,4 +1,4 @@
1
- import { COMPONENT_CONTAINER_ID } from "../../../constant";
1
+ import { COMPONENT_CONTAINER_ID, DEFAULT_IFRAME_COLOR_SCHEME } from "../../../constant";
2
2
 
3
3
  /**
4
4
  * @author 谦彧 <zhangmian.zm@alipay.com>
@@ -14,6 +14,7 @@ export var createPreloadIframe = function createPreloadIframe(containerId) {
14
14
  iframe.style.border = none;
15
15
  iframe.style.opacity = '0';
16
16
  iframe.style.overflow = 'hidden';
17
+ iframe.style.colorScheme = DEFAULT_IFRAME_COLOR_SCHEME;
17
18
  return iframe;
18
19
  };
19
20
  export var createIframe = function createIframe(containerId) {
@@ -27,6 +28,7 @@ export var createIframe = function createIframe(containerId) {
27
28
  iframe.style.border = none;
28
29
  iframe.style.opacity = none;
29
30
  iframe.style.overflow = 'hidden';
31
+ iframe.style.colorScheme = DEFAULT_IFRAME_COLOR_SCHEME;
30
32
  return iframe;
31
33
  };
32
34
  export var startSizeChangeAnim = function startSizeChangeAnim(target, height, animationStyleId) {
@@ -15,7 +15,7 @@ import { createLoader } from '@antglobal/create-sdk-loader';
15
15
 
16
16
  import { stageName } from "./stageName";
17
17
  var SDKURL = {
18
- DEV: "https://sdk-dev.marmot-cloud.com/package/ams-checkout/".concat("2.0.16", "/ams-checkout.js"),
18
+ DEV: "https://sdk-dev.marmot-cloud.com/package/ams-checkout/".concat("2.0.18", "/ams-checkout.js"),
19
19
  LOCAL: "http://localhost:3000/ams-checkout.min.js",
20
20
  PROD: 'https://js.antom.com/v2/ams-checkout.js'
21
21
  };
@@ -31,16 +31,27 @@ export declare class ApplePaySdk {
31
31
  * @description 支付状态,pending:支付中,normal:正常
32
32
  */
33
33
  private paymentStatus;
34
+ /**
35
+ * @description oncancel 原因标记,用于区分用户主动取消 vs 商户通过 onBeforeConfirm 中止
36
+ */
37
+ private cancelReason;
34
38
  private logger;
35
39
  private env;
36
40
  private shipping?;
37
- constructor({ paymentSessionData, parseData, logger, env, shipping, }: {
41
+ private merchantAmount;
42
+ private merchantCurrency;
43
+ private onBeforeConfirm?;
44
+ constructor({ paymentSessionData, parseData, logger, env, shipping, merchantAmount, merchantCurrency, }: {
38
45
  paymentSessionData: string;
39
46
  parseData: IPaymentSessionMetaData;
40
47
  logger: Logger;
41
48
  env?: string;
42
49
  shipping?: CKPShipping;
50
+ merchantAmount?: string | null;
51
+ merchantCurrency?: string | null;
43
52
  });
53
+ /** 设置商户 onBeforeConfirm 回调(展码链路使用) */
54
+ setOnBeforeConfirm(callback: (() => Promise<boolean>) | null): void;
44
55
  startPay(): Promise<unknown>;
45
56
  begin(): Promise<unknown>;
46
57
  private submitPay;