Skip to content

Commit b75f909

Browse files
authored
[Pay] Allow passing purchaseData with quotes (#104)
1 parent f3b8791 commit b75f909

7 files changed

+80
-63
lines changed

Thirdweb/Thirdweb.Pay/ThirdwebPay.GetBuyWithCryptoQuote.cs

Lines changed: 14 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -16,30 +16,18 @@ public partial class ThirdwebPay
1616
/// <exception cref="Exception">Thrown if the HTTP response is not successful.</exception>
1717
public static async Task<BuyWithCryptoQuoteResult> GetBuyWithCryptoQuote(ThirdwebClient client, BuyWithCryptoQuoteParams buyWithCryptoParams)
1818
{
19-
var queryString = new Dictionary<string, string>
20-
{
21-
{ "fromAddress", buyWithCryptoParams.FromAddress },
22-
{ "fromChainId", buyWithCryptoParams.FromChainId?.ToString() },
23-
{ "fromTokenAddress", buyWithCryptoParams.FromTokenAddress },
24-
{ "fromAmount", buyWithCryptoParams.FromAmount },
25-
{ "fromAmountWei", buyWithCryptoParams.FromAmountWei },
26-
{ "toChainId", buyWithCryptoParams.ToChainId?.ToString() },
27-
{ "toTokenAddress", buyWithCryptoParams.ToTokenAddress },
28-
{ "toAmount", buyWithCryptoParams.ToAmount },
29-
{ "toAmountWei", buyWithCryptoParams.ToAmountWei },
30-
{ "toAddress", buyWithCryptoParams.ToAddress },
31-
{ "maxSlippageBPS", buyWithCryptoParams.MaxSlippageBPS?.ToString() },
32-
{ "intentId", buyWithCryptoParams.IntentId }
33-
};
34-
35-
var queryStringFormatted = string.Join("&", queryString.Where(kv => kv.Value != null).Select(kv => $"{Uri.EscapeDataString(kv.Key)}={Uri.EscapeDataString(kv.Value)}"));
36-
var url = $"{THIRDWEB_PAY_CRYPTO_QUOTE_ENDPOINT}?{queryStringFormatted}";
37-
38-
var getResponse = await client.HttpClient.GetAsync(url);
39-
40-
var content = await getResponse.Content.ReadAsStringAsync();
41-
42-
if (!getResponse.IsSuccessStatusCode)
19+
var response = await client.HttpClient.PostAsync(
20+
THIRDWEB_PAY_CRYPTO_QUOTE_ENDPOINT,
21+
new StringContent(
22+
JsonConvert.SerializeObject(buyWithCryptoParams, new JsonSerializerSettings { NullValueHandling = NullValueHandling.Ignore }),
23+
System.Text.Encoding.UTF8,
24+
"application/json"
25+
)
26+
);
27+
28+
var content = await response.Content.ReadAsStringAsync();
29+
30+
if (!response.IsSuccessStatusCode)
4331
{
4432
ErrorResponse error;
4533
try
@@ -56,14 +44,12 @@ public static async Task<BuyWithCryptoQuoteResult> GetBuyWithCryptoQuote(Thirdwe
5644
Reason = "Unknown",
5745
Code = "Unknown",
5846
Stack = "Unknown",
59-
StatusCode = (int)getResponse.StatusCode
47+
StatusCode = (int)response.StatusCode
6048
}
6149
};
6250
}
6351

64-
throw new Exception(
65-
$"HTTP error! Code: {error.Error.Code} Message: {error.Error.Message} Reason: {error.Error.Reason} StatusCode: {error.Error.StatusCode} Stack: {error.Error.Stack}"
66-
);
52+
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}");
6753
}
6854

6955
var data = JsonConvert.DeserializeObject<GetSwapQuoteResponse>(content);

Thirdweb/Thirdweb.Pay/ThirdwebPay.GetBuyWithFiatQuote.cs

Lines changed: 14 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -16,29 +16,18 @@ public partial class ThirdwebPay
1616
/// <exception cref="Exception">Thrown if the HTTP response is not successful.</exception>
1717
public static async Task<BuyWithFiatQuoteResult> GetBuyWithFiatQuote(ThirdwebClient client, BuyWithFiatQuoteParams buyWithFiatParams)
1818
{
19-
var queryString = new Dictionary<string, string>
20-
{
21-
{ "fromCurrencySymbol", buyWithFiatParams.FromCurrencySymbol },
22-
{ "fromAmount", buyWithFiatParams.FromAmount },
23-
{ "fromAmountUnits", buyWithFiatParams.FromAmountUnits },
24-
{ "toAddress", buyWithFiatParams.ToAddress },
25-
{ "toChainId", buyWithFiatParams.ToChainId },
26-
{ "toTokenAddress", buyWithFiatParams.ToTokenAddress },
27-
{ "toAmount", buyWithFiatParams.ToAmount },
28-
{ "toAmountWei", buyWithFiatParams.ToAmountWei },
29-
{ "preferredProvider", buyWithFiatParams.PreferredProvider },
30-
{ "maxSlippageBPS", buyWithFiatParams.MaxSlippageBPS?.ToString() }
31-
};
32-
33-
var queryStringFormatted = string.Join("&", queryString.Where(kv => kv.Value != null).Select(kv => $"{Uri.EscapeDataString(kv.Key)}={Uri.EscapeDataString(kv.Value)}"));
34-
var url = $"{THIRDWEB_PAY_FIAT_QUOTE_ENDPOINT}?{queryStringFormatted}";
35-
url += buyWithFiatParams.IsTestMode ? "&isTestMode=true" : "&isTestMode=false";
36-
37-
var getResponse = await client.HttpClient.GetAsync(url);
38-
39-
var content = await getResponse.Content.ReadAsStringAsync();
40-
41-
if (!getResponse.IsSuccessStatusCode)
19+
var response = await client.HttpClient.PostAsync(
20+
THIRDWEB_PAY_FIAT_QUOTE_ENDPOINT,
21+
new StringContent(
22+
JsonConvert.SerializeObject(buyWithFiatParams, new JsonSerializerSettings { NullValueHandling = NullValueHandling.Ignore }),
23+
System.Text.Encoding.UTF8,
24+
"application/json"
25+
)
26+
);
27+
28+
var content = await response.Content.ReadAsStringAsync();
29+
30+
if (!response.IsSuccessStatusCode)
4231
{
4332
ErrorResponse error;
4433
try
@@ -55,14 +44,12 @@ public static async Task<BuyWithFiatQuoteResult> GetBuyWithFiatQuote(ThirdwebCli
5544
Reason = "Unknown",
5645
Code = "Unknown",
5746
Stack = "Unknown",
58-
StatusCode = (int)getResponse.StatusCode
47+
StatusCode = (int)response.StatusCode
5948
}
6049
};
6150
}
6251

63-
throw new Exception(
64-
$"HTTP error! Code: {error.Error.Code} Message: {error.Error.Message} Reason: {error.Error.Reason} StatusCode: {error.Error.StatusCode} Stack: {error.Error.Stack}"
65-
);
52+
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}");
6653
}
6754

6855
var data = JsonConvert.DeserializeObject<GetFiatQuoteResponse>(content);

Thirdweb/Thirdweb.Pay/Types.GetBuyWithCryptoQuote.cs

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,8 @@ public class BuyWithCryptoQuoteParams(
2121
string toAmountWei = null,
2222
string toAddress = null,
2323
double? maxSlippageBPS = null,
24-
string intentId = null
24+
string intentId = null,
25+
object purchaseData = null
2526
)
2627
{
2728
/// <summary>
@@ -95,6 +96,12 @@ public class BuyWithCryptoQuoteParams(
9596
/// </summary>
9697
[JsonProperty("intentId")]
9798
public string IntentId { get; set; } = intentId;
99+
100+
/// <summary>
101+
/// Additional data for the purchase. Useful with direct transfer flow.
102+
/// </summary>
103+
[JsonProperty("purchaseData")]
104+
public object PurchaseData { get; set; } = purchaseData;
98105
}
99106

100107
/// <summary>

Thirdweb/Thirdweb.Pay/Types.GetBuyWithCryptoStatus.cs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,12 @@ public class BuyWithCryptoStatusResult
7878
/// </summary>
7979
[JsonProperty("bridge")]
8080
public string Bridge { get; set; }
81+
82+
/// <summary>
83+
/// Additional data for the purchase. Useful with direct transfer flow.
84+
/// </summary>
85+
[JsonProperty("purchaseData")]
86+
public object PurchaseData { get; set; }
8187
}
8288

8389
/// <summary>

Thirdweb/Thirdweb.Pay/Types.GetBuyWithFiatQuote.cs

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,8 @@ public class BuyWithFiatQuoteParams(
1919
string toAmountWei = null,
2020
double? maxSlippageBPS = null,
2121
bool isTestMode = false,
22-
string preferredProvider = null
22+
string preferredProvider = null,
23+
object purchaseData = null
2324
)
2425
{
2526
/// <summary>
@@ -40,11 +41,6 @@ public class BuyWithFiatQuoteParams(
4041
[JsonProperty("fromAmountUnits")]
4142
public string FromAmountUnits { get; set; } = fromAmountUnits;
4243

43-
/// <summary>
44-
/// The provider to use on the application for thirdweb pay
45-
/// </summary>
46-
[JsonProperty("preferredProvider")]
47-
public string PreferredProvider { get; set; } = preferredProvider;
4844
/// <summary>
4945
/// The address to receive the purchased tokens.
5046
/// </summary>
@@ -86,6 +82,18 @@ public class BuyWithFiatQuoteParams(
8682
/// </summary>
8783
[JsonProperty("isTestMode")]
8884
public bool IsTestMode { get; set; } = isTestMode;
85+
86+
/// <summary>
87+
/// The provider to use on the application for thirdweb pay
88+
/// </summary>
89+
[JsonProperty("preferredProvider")]
90+
public string PreferredProvider { get; set; } = preferredProvider;
91+
92+
/// <summary>
93+
/// Additional data for the purchase. Useful with direct transfer flow.
94+
/// </summary>
95+
[JsonProperty("purchaseData")]
96+
public object PurchaseData { get; set; } = purchaseData;
8997
}
9098

9199
/// <summary>

Thirdweb/Thirdweb.Pay/Types.GetBuyWithFiatStatus.cs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,12 @@ public class BuyWithFiatStatusResult
6060
/// </summary>
6161
[JsonProperty("failureMessage")]
6262
public string FailureMessage { get; set; }
63+
64+
/// <summary>
65+
/// Additional data for the purchase. Useful with direct transfer flow.
66+
/// </summary>
67+
[JsonProperty("purchaseData")]
68+
public object PurchaseData { get; set; }
6369
}
6470

6571
/// <summary>

Thirdweb/Thirdweb.Utils/Utils.cs

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -306,7 +306,24 @@ public static string GenerateSIWE(LoginPayloadData loginPayloadData)
306306
/// <returns>True if it is a zkSync chain ID, otherwise false.</returns>
307307
public static async Task<bool> IsZkSync(ThirdwebClient client, BigInteger chainId)
308308
{
309-
if (chainId.Equals(324) || chainId.Equals(300) || chainId.Equals(302) || chainId.Equals(11124) || chainId.Equals(4654) || chainId.Equals(333271) || chainId.Equals(37111))
309+
if (
310+
chainId.Equals(324)
311+
|| chainId.Equals(300)
312+
|| chainId.Equals(302)
313+
|| chainId.Equals(11124)
314+
|| chainId.Equals(282)
315+
|| chainId.Equals(388)
316+
|| chainId.Equals(4654)
317+
|| chainId.Equals(333271)
318+
|| chainId.Equals(37111)
319+
|| chainId.Equals(978658)
320+
|| chainId.Equals(531050104)
321+
|| chainId.Equals(4457845)
322+
|| chainId.Equals(2741)
323+
|| chainId.Equals(240)
324+
|| chainId.Equals(61166)
325+
|| chainId.Equals(555271)
326+
)
310327
{
311328
return true;
312329
}

0 commit comments

Comments
 (0)