@metamask-previews/ramps-controller 10.0.0-preview-5111712 → 10.0.0-preview-685dbf46b

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.
package/CHANGELOG.md CHANGED
@@ -14,6 +14,10 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
14
14
  - Added `RampsController:orderStatusChanged` event, published when a polled order's status transitions ([#8045](https://github.com/MetaMask/core/pull/8045))
15
15
  - Add messenger actions for `RampsController:setSelectedToken`, `RampsController:getQuotes`, and `RampsController:getOrder`, register their handlers in `RampsController`, and export the action types from the package index ([#8081](https://github.com/MetaMask/core/pull/8081))
16
16
 
17
+ ### Changed
18
+
19
+ - **BREAKING:** Replace `getWidgetUrl` with `getBuyWidgetData` (returns `BuyWidget | null`); add `addPrecreatedOrder` for custom-action ramp flows (e.g., PayPal) ([#8100](https://github.com/MetaMask/core/pull/8100))
20
+
17
21
  ## [10.0.0]
18
22
 
19
23
  ### Changed
@@ -12,7 +12,7 @@ var __classPrivateFieldSet = (this && this.__classPrivateFieldSet) || function (
12
12
  };
13
13
  var _RampsController_instances, _RampsController_requestCacheTTL, _RampsController_requestCacheMaxSize, _RampsController_pendingRequests, _RampsController_pendingResourceCount, _RampsController_orderPollingMeta, _RampsController_orderPollingTimer, _RampsController_isPolling, _RampsController_clearPendingResourceCountForDependentResources, _RampsController_abortDependentRequests, _RampsController_registerActionHandlers, _RampsController_mutateRequests, _RampsController_removeRequestState, _RampsController_cleanupState, _RampsController_fireAndForget, _RampsController_requireRegion, _RampsController_isRegionCurrent, _RampsController_isTokenCurrent, _RampsController_isProviderCurrent, _RampsController_updateResourceField, _RampsController_setResourceLoading, _RampsController_setResourceError, _RampsController_updateRequestState, _RampsController_refreshOrder, _RampsController_pollPendingOrders, _RampsController_syncTransakAuthOnError;
14
14
  Object.defineProperty(exports, "__esModule", { value: true });
15
- exports.RampsController = exports.getDefaultRampsControllerState = exports.RAMPS_CONTROLLER_REQUIRED_SERVICE_ACTIONS = exports.controllerName = void 0;
15
+ exports.RampsController = exports.normalizeProviderCode = exports.getDefaultRampsControllerState = exports.RAMPS_CONTROLLER_REQUIRED_SERVICE_ACTIONS = exports.controllerName = void 0;
16
16
  const base_controller_1 = require("@metamask/base-controller");
17
17
  const RampsService_1 = require("./RampsService.cjs");
18
18
  const RequestCache_1 = require("./RequestCache.cjs");
@@ -250,6 +250,10 @@ function findRegionFromCode(regionCode, countries) {
250
250
  regionCode: normalizedCode,
251
251
  };
252
252
  }
253
+ function normalizeProviderCode(providerCode) {
254
+ return providerCode.replace(/^\/providers\//u, '');
255
+ }
256
+ exports.normalizeProviderCode = normalizeProviderCode;
253
257
  // === ORDER POLLING CONSTANTS ===
254
258
  const TERMINAL_ORDER_STATUSES = new Set([
255
259
  RampsService_1.RampsOrderStatus.Completed,
@@ -935,25 +939,74 @@ class RampsController extends base_controller_1.BaseController {
935
939
  super.destroy();
936
940
  }
937
941
  /**
938
- * Fetches the widget URL from a quote for redirect providers.
942
+ * Fetches the widget data from a quote for redirect providers.
939
943
  * Makes a request to the buyURL endpoint via the RampsService to get the
940
- * actual provider widget URL.
944
+ * actual provider widget URL and optional order ID for polling.
941
945
  *
942
946
  * @param quote - The quote to fetch the widget URL from.
943
- * @returns Promise resolving to the widget URL string, or null if not available.
947
+ * @returns Promise resolving to the full BuyWidget (url, browser, orderId), or null if not available (missing buyURL or empty url in response).
948
+ * @throws Rethrows errors from the RampsService (e.g. HttpError, network failures) so clients can react to fetch failures.
944
949
  */
945
- async getWidgetUrl(quote) {
950
+ async getBuyWidgetData(quote) {
946
951
  const buyUrl = quote.quote?.buyURL;
947
952
  if (!buyUrl) {
948
953
  return null;
949
954
  }
950
- try {
951
- const buyWidget = await this.messenger.call('RampsService:getBuyWidgetUrl', buyUrl);
952
- return buyWidget.url ?? null;
953
- }
954
- catch {
955
+ const buyWidget = await this.messenger.call('RampsService:getBuyWidgetUrl', buyUrl);
956
+ if (!buyWidget?.url) {
955
957
  return null;
956
958
  }
959
+ return buyWidget;
960
+ }
961
+ /**
962
+ * Registers an order ID for polling until the order is created or resolved.
963
+ * Adds a minimal stub order to controller state; the existing order polling
964
+ * will fetch the full order when the provider has created it.
965
+ *
966
+ * @param params - Object containing order identifiers and wallet info.
967
+ * @param params.orderId - Full order ID (e.g. "/providers/paypal/orders/abc123") or order code.
968
+ * @param params.providerCode - Provider code (e.g. "paypal", "transak"), with or without /providers/ prefix.
969
+ * @param params.walletAddress - Wallet address for the order.
970
+ * @param params.chainId - Optional chain ID for the order.
971
+ */
972
+ addPrecreatedOrder(params) {
973
+ const { orderId, providerCode, walletAddress, chainId } = params;
974
+ const orderCode = orderId.includes('/orders/')
975
+ ? orderId.split('/orders/')[1]
976
+ : orderId;
977
+ if (!orderCode?.trim()) {
978
+ return;
979
+ }
980
+ const normalizedProviderCode = normalizeProviderCode(providerCode);
981
+ const stubOrder = {
982
+ providerOrderId: orderCode,
983
+ provider: {
984
+ id: `/providers/${normalizedProviderCode}`,
985
+ name: '',
986
+ environmentType: '',
987
+ description: '',
988
+ hqAddress: '',
989
+ links: [],
990
+ logos: { light: '', dark: '', height: 0, width: 0 },
991
+ },
992
+ walletAddress,
993
+ status: RampsService_1.RampsOrderStatus.Precreated,
994
+ orderType: 'buy',
995
+ createdAt: Date.now(),
996
+ isOnlyLink: false,
997
+ success: false,
998
+ cryptoAmount: 0,
999
+ fiatAmount: 0,
1000
+ providerOrderLink: '',
1001
+ totalFeesFiat: 0,
1002
+ txHash: '',
1003
+ network: chainId ? { chainId, name: '' } : { chainId: '', name: '' },
1004
+ canBeUpdated: true,
1005
+ idHasExpired: false,
1006
+ excludeFromPurchases: false,
1007
+ timeDescriptionPending: '',
1008
+ };
1009
+ this.addOrder(stubOrder);
957
1010
  }
958
1011
  /**
959
1012
  * Fetches an order from the unified V2 API endpoint.
@@ -1486,7 +1539,7 @@ async function _RampsController_refreshOrder(order) {
1486
1539
  if (!providerCode || !order.providerOrderId || !order.walletAddress) {
1487
1540
  return;
1488
1541
  }
1489
- const providerCodeSegment = providerCode.replace('/providers/', '');
1542
+ const providerCodeSegment = normalizeProviderCode(providerCode);
1490
1543
  const previousStatus = order.status;
1491
1544
  try {
1492
1545
  const updatedOrder = await this.getOrder(providerCodeSegment, order.providerOrderId, order.walletAddress);