-
Notifications
You must be signed in to change notification settings - Fork 73
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Buy With Fiat API // Scene_Pay (#178)
- Loading branch information
1 parent
69ca41f
commit 4b24a66
Showing
32 changed files
with
2,523 additions
and
175 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
31 changes: 31 additions & 0 deletions
31
Assets/Thirdweb/Core/Scripts/Pay/ThirdwebPay.BuyWithFiat.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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.IntentId; | ||
} | ||
} | ||
} |
11 changes: 11 additions & 0 deletions
11
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.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
70 changes: 70 additions & 0 deletions
70
Assets/Thirdweb/Core/Scripts/Pay/ThirdwebPay.GetBuyWithFiatCurrencies.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} | ||
} |
11 changes: 11 additions & 0 deletions
11
Assets/Thirdweb/Core/Scripts/Pay/ThirdwebPay.GetBuyWithFiatCurrencies.cs.meta
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
87 changes: 87 additions & 0 deletions
87
Assets/Thirdweb/Core/Scripts/Pay/ThirdwebPay.GetBuyWithFiatQuote.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,87 @@ | ||
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}"; | ||
url += buyWithFiatParams.IsTestMode ? "&isTestMode=true" : "&isTestMode=false"; | ||
|
||
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; | ||
} | ||
} | ||
} |
11 changes: 11 additions & 0 deletions
11
Assets/Thirdweb/Core/Scripts/Pay/ThirdwebPay.GetBuyWithFiatQuote.cs.meta
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
79 changes: 79 additions & 0 deletions
79
Assets/Thirdweb/Core/Scripts/Pay/ThirdwebPay.GetBuyWithFiatStatus.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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="intentId">Intent ID to get onramp status for</param> | ||
/// <returns>Onramp status object <see cref="BuyWithFiatStatusResult"/></returns> | ||
public static async Task<BuyWithFiatStatusResult> GetBuyWithFiatStatus(string intentId) | ||
{ | ||
if (string.IsNullOrEmpty(Utils.GetClientId())) | ||
{ | ||
throw new Exception("Client ID is not set. Please set it in the ThirdwebManager."); | ||
} | ||
|
||
if (string.IsNullOrEmpty(intentId)) | ||
{ | ||
throw new ArgumentNullException(nameof(intentId)); | ||
} | ||
|
||
var queryString = new Dictionary<string, string> { { "intentId", intentId } }; | ||
|
||
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; | ||
} | ||
} | ||
} |
11 changes: 11 additions & 0 deletions
11
Assets/Thirdweb/Core/Scripts/Pay/ThirdwebPay.GetBuyWithFiatStatus.cs.meta
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Oops, something went wrong.