Skip to content

Commit

Permalink
Buy With Fiat API // Scene_Pay
Browse files Browse the repository at this point in the history
  • Loading branch information
0xFirekeeper committed Apr 18, 2024
1 parent 869df33 commit fa4b7b0
Show file tree
Hide file tree
Showing 22 changed files with 2,304 additions and 7 deletions.
7 changes: 6 additions & 1 deletion Assets/Thirdweb/Core/Scripts/Pay/Constants.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,15 @@ namespace Thirdweb.Pay
{
public static class Constants
{
public const string THIRDWEB_PAY_BASE_URL = "https://pay.thirdweb.com";
public const string THIRDWEB_PAY_BASE_URL = "https://pay.thirdweb-dev.com";

public const string THIRDWEB_PAY_QUOTE_ENDPOINT = THIRDWEB_PAY_BASE_URL + "/buy-with-crypto/quote/v1";
public const string THIRDWEB_PAY_STATUS_ENDPOINT = THIRDWEB_PAY_BASE_URL + "/buy-with-crypto/status/v1";
public const string THIRDWEB_PAY_HISTORY_ENDPOINT = THIRDWEB_PAY_BASE_URL + "/buy-with-crypto/history/v1";

public const string THIRDWEB_PAY_FIAT_QUOTE_ENDPOINT = THIRDWEB_PAY_BASE_URL + "/buy-with-fiat/quote/v1";
public const string THIRDWEB_PAY_FIAT_STATUS_ENDPOINT = THIRDWEB_PAY_BASE_URL + "/buy-with-fiat/status/v1";

public const string THIRDWEB_PAY_FIAT_CURRENCIES_ENDPOINT = THIRDWEB_PAY_BASE_URL + "/buy-with-fiat/currency/v1";
}
}
31 changes: 31 additions & 0 deletions Assets/Thirdweb/Core/Scripts/Pay/ThirdwebPay.BuyWithFiat.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
using System;
using System.Threading.Tasks;
using UnityEngine;

namespace Thirdweb.Pay
{
public static partial class ThirdwebPay
{
/// <summary>
/// Buy crypto with fiat using the onramp link from the quote and get a quote ID to poll for the onramp status.
/// </summary>
/// <param name="buyWithFiatQuote">Quote containing onramp details</param>
/// <param name="sdk">Optional SDK instance, defaults to ThirdwebManager instance</param>
/// <returns>Quote ID to poll for the onramp status</returns>
public static string BuyWithFiat(BuyWithFiatQuoteResult buyWithFiatQuote, ThirdwebSDK sdk = null)
{
sdk ??= ThirdwebManager.Instance.SDK;

if (string.IsNullOrEmpty(buyWithFiatQuote.OnRampLink))
{
throw new Exception("OnRampLink is required to buy with fiat.");
}

var onRampLink = buyWithFiatQuote.OnRampLink;

Application.OpenURL(onRampLink);

return buyWithFiatQuote.QuoteId;
}
}
}
11 changes: 11 additions & 0 deletions Assets/Thirdweb/Core/Scripts/Pay/ThirdwebPay.BuyWithFiat.cs.meta

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
using System;
using System.Collections.Generic;
using UnityEngine.Networking;
using Newtonsoft.Json;
using System.Linq;
using System.Threading.Tasks;
using Thirdweb.Redcode.Awaiting;

namespace Thirdweb.Pay
{
public static partial class ThirdwebPay
{
/// <summary>
/// Get supported fiat currencies for Buy with Fiat.
/// </summary>
/// <returns>List of supported Fiat currency symbols.</returns>
public static async Task<List<string>> GetBuyWithFiatCurrencies()
{
if (string.IsNullOrEmpty(Utils.GetClientId()))
{
throw new Exception("Client ID is not set. Please set it in the ThirdwebManager.");
}

var url = $"{Constants.THIRDWEB_PAY_FIAT_CURRENCIES_ENDPOINT}";

using var request = UnityWebRequest.Get(url);

request.SetRequestHeader("x-sdk-name", "UnitySDK");
request.SetRequestHeader("x-sdk-os", Utils.GetRuntimePlatform());
request.SetRequestHeader("x-sdk-platform", "unity");
request.SetRequestHeader("x-sdk-version", ThirdwebSDK.version);
request.SetRequestHeader("x-client-id", ThirdwebManager.Instance.SDK.Session.Options.clientId);
if (!Utils.IsWebGLBuild())
request.SetRequestHeader("x-bundle-id", ThirdwebManager.Instance.SDK.Session.Options.bundleId);

await request.SendWebRequest();

if (request.result != UnityWebRequest.Result.Success)
{
ErrorResponse error;
try
{
error = JsonConvert.DeserializeObject<ErrorResponse>(request.downloadHandler.text);
}
catch
{
error = new ErrorResponse
{
Error = new ErrorDetails
{
Message = "Unknown error",
Reason = "Unknown",
Code = "Unknown",
Stack = "Unknown",
StatusCode = (int)request.responseCode
}
};
}

throw new Exception(
$"HTTP error! Code: {error.Error.Code} Message: {error.Error.Message} Reason: {error.Error.Reason} StatusCode: {error.Error.StatusCode} Stack: {error.Error.Stack}"
);
}

var content = request.downloadHandler.text;
var data = JsonConvert.DeserializeObject<FiatCurrenciesResponse>(content);
return data.Result.FiatCurrencies;
}
}
}

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
using System;
using System.Collections.Generic;
using UnityEngine.Networking;
using Newtonsoft.Json;
using System.Linq;
using Thirdweb.Redcode.Awaiting;
using System.Threading.Tasks;
using System.Diagnostics;

namespace Thirdweb.Pay
{
public static partial class ThirdwebPay
{
/// <summary>
/// Get a quote containing an onramp link for a fiat to crypto swap.
/// </summary>
/// <param name="buyWithFiatParams">Fiat onramp parameters <see cref="BuyWithFiatQuoteParams"/></param>
/// <returns>Fiat quote object <see cref="BuyWithFiatQuoteResult"/></returns>
public static async Task<BuyWithFiatQuoteResult> GetBuyWithFiatQuote(BuyWithFiatQuoteParams buyWithFiatParams)
{
if (string.IsNullOrEmpty(Utils.GetClientId()))
{
throw new Exception("Client ID is not set. Please set it in the ThirdwebManager.");
}

var queryString = new Dictionary<string, string>
{
{ "fromCurrencySymbol", buyWithFiatParams.FromCurrencySymbol },
{ "fromAmount", buyWithFiatParams.FromAmount },
{ "fromAmountUnits", buyWithFiatParams.FromAmountUnits },
{ "toAddress", buyWithFiatParams.ToAddress },
{ "toChainId", buyWithFiatParams.ToChainId },
{ "toTokenAddress", buyWithFiatParams.ToTokenAddress },
{ "toAmount", buyWithFiatParams.ToAmount },
{ "toAmountWei", buyWithFiatParams.ToAmountWei },
{ "maxSlippageBPS", buyWithFiatParams.MaxSlippageBPS?.ToString() }
};

var queryStringFormatted = string.Join("&", queryString.Where(kv => kv.Value != null).Select(kv => $"{Uri.EscapeDataString(kv.Key)}={Uri.EscapeDataString(kv.Value)}"));
var url = $"{Constants.THIRDWEB_PAY_FIAT_QUOTE_ENDPOINT}?{queryStringFormatted}";

using var request = UnityWebRequest.Get(url);

request.SetRequestHeader("x-sdk-name", "UnitySDK");
request.SetRequestHeader("x-sdk-os", Utils.GetRuntimePlatform());
request.SetRequestHeader("x-sdk-platform", "unity");
request.SetRequestHeader("x-sdk-version", ThirdwebSDK.version);
request.SetRequestHeader("x-client-id", Utils.GetClientId());
if (!Utils.IsWebGLBuild())
request.SetRequestHeader("x-bundle-id", Utils.GetBundleId());

await request.SendWebRequest();

if (request.result != UnityWebRequest.Result.Success)
{
ErrorResponse error;
try
{
error = JsonConvert.DeserializeObject<ErrorResponse>(request.downloadHandler.text);
}
catch
{
error = new ErrorResponse
{
Error = new ErrorDetails
{
Message = "Unknown error",
Reason = "Unknown",
Code = "Unknown",
Stack = "Unknown",
StatusCode = (int)request.responseCode
}
};
}

throw new Exception(
$"HTTP error! Code: {error.Error.Code} Message: {error.Error.Message} Reason: {error.Error.Reason} StatusCode: {error.Error.StatusCode} Stack: {error.Error.Stack}"
);
}

var content = request.downloadHandler.text;
var data = JsonConvert.DeserializeObject<GetFiatQuoteResponse>(content);
return data.Result;
}
}
}

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
using System;
using System.Collections.Generic;
using UnityEngine.Networking;
using Newtonsoft.Json;
using System.Linq;
using System.Threading.Tasks;
using Thirdweb.Redcode.Awaiting;

namespace Thirdweb.Pay
{
public static partial class ThirdwebPay
{
/// <summary>
/// Get onramp status for a quote id.
/// </summary>
/// <param name="quoteId">Quote ID to get onramp status for</param>
/// <returns>Onramp status object <see cref="BuyWithFiatStatusResult"/></returns>
public static async Task<BuyWithFiatStatusResult> GetBuyWithFiatStatus(string quoteId)
{
if (string.IsNullOrEmpty(Utils.GetClientId()))
{
throw new Exception("Client ID is not set. Please set it in the ThirdwebManager.");
}

if (string.IsNullOrEmpty(quoteId))
{
throw new ArgumentNullException(nameof(quoteId));
}

var queryString = new Dictionary<string, string> { { "transactionId", quoteId } };

var queryStringFormatted = string.Join("&", queryString.Where(kv => kv.Value != null).Select(kv => $"{Uri.EscapeDataString(kv.Key)}={Uri.EscapeDataString(kv.Value)}"));
var url = $"{Constants.THIRDWEB_PAY_FIAT_STATUS_ENDPOINT}?{queryStringFormatted}";

using var request = UnityWebRequest.Get(url);

request.SetRequestHeader("x-sdk-name", "UnitySDK");
request.SetRequestHeader("x-sdk-os", Utils.GetRuntimePlatform());
request.SetRequestHeader("x-sdk-platform", "unity");
request.SetRequestHeader("x-sdk-version", ThirdwebSDK.version);
request.SetRequestHeader("x-client-id", ThirdwebManager.Instance.SDK.Session.Options.clientId);
if (!Utils.IsWebGLBuild())
request.SetRequestHeader("x-bundle-id", ThirdwebManager.Instance.SDK.Session.Options.bundleId);

await request.SendWebRequest();

if (request.result != UnityWebRequest.Result.Success)
{
ErrorResponse error;
try
{
error = JsonConvert.DeserializeObject<ErrorResponse>(request.downloadHandler.text);
}
catch
{
error = new ErrorResponse
{
Error = new ErrorDetails
{
Message = "Unknown error",
Reason = "Unknown",
Code = "Unknown",
Stack = "Unknown",
StatusCode = (int)request.responseCode
}
};
}

throw new Exception(
$"HTTP error! Code: {error.Error.Code} Message: {error.Error.Message} Reason: {error.Error.Reason} StatusCode: {error.Error.StatusCode} Stack: {error.Error.Stack}"
);
}

var content = request.downloadHandler.text;
var data = JsonConvert.DeserializeObject<OnRampStatusResponse>(content);
return data.Result;
}
}
}

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ public class BuyWithCryptoQuoteParams
public string ToAmountWei { get; set; }

[JsonProperty("maxSlippageBPS")]
public int? MaxSlippageBPS { get; set; }
public double? MaxSlippageBPS { get; set; }

public BuyWithCryptoQuoteParams(
string fromAddress,
Expand All @@ -46,7 +46,7 @@ public BuyWithCryptoQuoteParams(
BigInteger? toChainId = null,
string toAmount = null,
string toAmountWei = null,
int? maxSlippageBPS = null
double? maxSlippageBPS = null
)
{
FromAddress = fromAddress;
Expand Down Expand Up @@ -182,7 +182,7 @@ public class BuyWithCryptoQuoteResult
public Estimated Estimated { get; set; }

[JsonProperty("maxSlippageBPS")]
public int MaxSlippageBPS { get; set; }
public double MaxSlippageBPS { get; set; }

[JsonProperty("bridge")]
public string Bridge { get; set; }
Expand Down
18 changes: 18 additions & 0 deletions Assets/Thirdweb/Core/Scripts/Pay/Types.GetBuyWithFiatCurrencies.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
using System;
using System.Collections.Generic;
using Newtonsoft.Json;

namespace Thirdweb.Pay
{
public class FiatCurrenciesResponse
{
[JsonProperty("result")]
public FiatCurrenciesResult Result { get; set; }
}

public class FiatCurrenciesResult
{
[JsonProperty("fiatCurrencies")]
public List<string> FiatCurrencies { get; set; }
}
}

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading

0 comments on commit fa4b7b0

Please sign in to comment.