buy working

This commit is contained in:
Matthew Fosse 2024-04-02 09:59:10 -07:00
parent a5fc98eca8
commit 95cbba2282

View file

@ -1,16 +1,16 @@
import 'dart:convert';
import 'package:cake_wallet/.secrets.g.dart' as secrets; import 'package:cake_wallet/.secrets.g.dart' as secrets;
import 'package:cake_wallet/buy/buy_provider.dart'; import 'package:cake_wallet/buy/buy_provider.dart';
import 'package:cake_wallet/palette.dart';
import 'package:cake_wallet/routes.dart'; import 'package:cake_wallet/routes.dart';
import 'package:cake_wallet/store/settings_store.dart'; import 'package:cake_wallet/store/settings_store.dart';
import 'package:cake_wallet/themes/theme_base.dart';
import 'package:cake_wallet/utils/device_info.dart'; import 'package:cake_wallet/utils/device_info.dart';
import 'package:cw_core/crypto_currency.dart'; import 'package:cw_core/crypto_currency.dart';
import 'package:cw_core/wallet_base.dart'; import 'package:cw_core/wallet_base.dart';
import 'package:cw_core/wallet_type.dart'; import 'package:cw_core/wallet_type.dart';
import 'package:flutter/material.dart'; import 'package:flutter/material.dart';
import 'package:url_launcher/url_launcher.dart'; import 'package:url_launcher/url_launcher.dart';
import 'package:http/http.dart' as http;
class MeldProvider extends BuyProvider { class MeldProvider extends BuyProvider {
MeldProvider({ MeldProvider({
@ -28,7 +28,6 @@ class MeldProvider extends BuyProvider {
static const _baseSellProductUrl = 'api.meld.io'; static const _baseSellProductUrl = 'api.meld.io';
static const _baseBuyTestUrl = 'api-sb.meld.io'; static const _baseBuyTestUrl = 'api-sb.meld.io';
static const _baseBuyProductUrl = 'api.meld.io'; static const _baseBuyProductUrl = 'api.meld.io';
// static const _cIdBaseUrl = 'exchange-helper.cakewallet.com';
final String baseBuyUrl; final String baseBuyUrl;
final String baseSellUrl; final String baseSellUrl;
@ -43,23 +42,10 @@ class MeldProvider extends BuyProvider {
String get lightIcon => 'assets/images/meld_light.svg'; String get lightIcon => 'assets/images/meld_light.svg';
@override @override
String get darkIcon => 'assets/images/moonpay_dark.png'; String get darkIcon => 'assets/images/meld_light.svg';
static String themeToMoonPayTheme(ThemeBase theme) {
switch (theme.type) {
case ThemeType.bright:
case ThemeType.light:
return 'light';
case ThemeType.dark:
return 'dark';
}
}
static String get _apiKey => secrets.moonPayApiKey;
String get currencyCode => walletTypeToCryptoCurrency(wallet.type).title.toLowerCase(); String get currencyCode => walletTypeToCryptoCurrency(wallet.type).title.toLowerCase();
static String get _exchangeHelperApiKey => secrets.exchangeHelperApiKey; static String get _exchangeHelperApiKey => secrets.exchangeHelperApiKey;
Future<Uri> requestSellUrl({ Future<Uri> requestSellUrl({
@ -71,10 +57,7 @@ class MeldProvider extends BuyProvider {
} }
// BUY: // BUY:
static const _currenciesSuffix = '/v3/currencies'; static const _buyWidgetSuffix = "/crypto/session/widget";
static const _quoteSuffix = '/buy_quote';
static const _transactionsSuffix = '/v1/transactions';
static const _ipAddressSuffix = '/v4/ip_address';
Future<Uri> requestBuyUrl({ Future<Uri> requestBuyUrl({
required CryptoCurrency currency, required CryptoCurrency currency,
@ -82,44 +65,34 @@ class MeldProvider extends BuyProvider {
required String walletAddress, required String walletAddress,
String? amount, String? amount,
}) async { }) async {
final params = { try {
'theme': themeToMoonPayTheme(settingsStore.currentTheme), final headers = {
'language': settingsStore.languageCode, 'Meld-Version': '2023-12-19',
'colorCode': settingsStore.currentTheme.type == ThemeType.dark 'Content-Type': 'application/json',
? '#${Palette.blueCraiola.value.toRadixString(16).substring(2, 8)}' 'Authorization': 'BASIC ${secrets.meldApiKey}',
: '#${Palette.moderateSlateBlue.value.toRadixString(16).substring(2, 8)}', };
'defaultCurrencyCode': _normalizeCurrency(currency), final body = {
'baseCurrencyCode': _normalizeCurrency(currency), "sessionData": {
'baseCurrencyAmount': amount ?? '0', "walletAddress": walletAddress,
'currencyCode': currencyCode, "countryCode": _normalizeCountryCode(settingsStore.fiatCurrency.countryCode),
'walletAddress': walletAddress, "sourceCurrencyCode": settingsStore.fiatCurrency.raw,
'lockAmount': 'true', "sourceAmount": amount ?? '60',
'showAllCurrencies': 'false', "destinationCurrencyCode": currencyCode.toUpperCase(),
'showWalletAddressForm': 'false', "serviceProvider": "STRIPE"
'enabledPaymentMethods': },
'credit_debit_card,apple_pay,google_pay,samsung_pay,sepa_bank_transfer,gbp_bank_transfer,gbp_open_banking_payment', "sessionType": "BUY",
}; "externalCustomerId": "testcustomer",
};
if (_apiKey.isNotEmpty) { final response = await http.post(
params['apiKey'] = _apiKey; Uri.https(baseBuyUrl, _buyWidgetSuffix),
headers: headers,
body: json.encode(body),
);
return Uri.parse(json.decode(response.body)["widgetUrl"] as String);
} catch (e) {
print(e);
rethrow;
} }
final originalUri = Uri.https(
baseBuyUrl,
'',
params,
);
if (isTestEnvironment) {
return originalUri;
}
return originalUri;
// final signature = await getMoonpaySignature('?${originalUri.query}');
// final query = Map<String, dynamic>.from(originalUri.queryParameters);
// query['signature'] = signature;
// final signedUri = originalUri.replace(queryParameters: query);
// return signedUri;
} }
@override @override
@ -150,11 +123,13 @@ class MeldProvider extends BuyProvider {
} }
} }
String _normalizeCurrency(CryptoCurrency currency) { // normalize country codes to ISO-3166:
if (currency == CryptoCurrency.maticpoly) { String _normalizeCountryCode(String countryCode) {
return "MATIC_POLYGON"; countryCode = countryCode.toLowerCase();
switch (countryCode) {
case "usa":
default:
return "US";
} }
return currency.toString().toLowerCase();
} }
} }