From 1d345299f57d83bd7e9b2d576625fc44a4c1dc5e Mon Sep 17 00:00:00 2001
From: John Preston <johnprestonmail@gmail.com>
Date: Fri, 22 Dec 2023 20:51:50 -0400
Subject: [PATCH] Allow smartglocal to customize tokenize url.

---
 Telegram/SourceFiles/payments/payments_form.cpp   |  2 ++
 Telegram/SourceFiles/payments/payments_form.h     |  1 +
 .../smartglocal/smartglocal_api_client.cpp        | 15 +++++++++++++--
 .../payments/smartglocal/smartglocal_api_client.h |  1 +
 4 files changed, 17 insertions(+), 2 deletions(-)

diff --git a/Telegram/SourceFiles/payments/payments_form.cpp b/Telegram/SourceFiles/payments/payments_form.cpp
index 5eea98353..9ff737d1c 100644
--- a/Telegram/SourceFiles/payments/payments_form.cpp
+++ b/Telegram/SourceFiles/payments/payments_form.cpp
@@ -675,6 +675,7 @@ void Form::fillSmartGlocalNativeMethod(QJsonObject object) {
 	_paymentMethod.native = NativePaymentMethod{
 		.data = SmartGlocalPaymentMethod{
 			.publicToken = key,
+			.tokenizeUrl = value(u"tokenize_url").toString(),
 		},
 	};
 	_paymentMethod.ui.native = Ui::NativeMethodDetails{
@@ -1011,6 +1012,7 @@ void Form::validateCard(
 	}
 	auto configuration = SmartGlocal::PaymentConfiguration{
 		.publicToken = method.publicToken,
+		.tokenizeUrl = method.tokenizeUrl,
 		.isTest = _invoice.isTest,
 	};
 	_smartglocal = std::make_unique<SmartGlocal::APIClient>(
diff --git a/Telegram/SourceFiles/payments/payments_form.h b/Telegram/SourceFiles/payments/payments_form.h
index bd95e68ea..414eb0b02 100644
--- a/Telegram/SourceFiles/payments/payments_form.h
+++ b/Telegram/SourceFiles/payments/payments_form.h
@@ -95,6 +95,7 @@ struct StripePaymentMethod {
 
 struct SmartGlocalPaymentMethod {
 	QString publicToken;
+	QString tokenizeUrl;
 };
 
 struct NativePaymentMethod {
diff --git a/Telegram/SourceFiles/payments/smartglocal/smartglocal_api_client.cpp b/Telegram/SourceFiles/payments/smartglocal/smartglocal_api_client.cpp
index 0fca1b7f0..690438c08 100644
--- a/Telegram/SourceFiles/payments/smartglocal/smartglocal_api_client.cpp
+++ b/Telegram/SourceFiles/payments/smartglocal/smartglocal_api_client.cpp
@@ -44,10 +44,21 @@ namespace {
 	}).toJson(QJsonDocument::Compact);
 }
 
+[[nodiscard]] QString ComputeApiUrl(PaymentConfiguration configuration) {
+	const auto url = configuration.tokenizeUrl;
+	if (url.startsWith("https://")
+		&& url.endsWith(".smart-glocal.com/cds/v1/tokenize/card")) {
+		return url;
+	}
+	return QString("https://%1/%2")
+		.arg(APIURLBase(configuration.isTest))
+		.arg(TokenEndpoint());
+}
+
 } // namespace
 
 APIClient::APIClient(PaymentConfiguration configuration)
-: _apiUrl("https://" + APIURLBase(configuration.isTest))
+: _apiUrl(ComputeApiUrl(configuration))
 , _configuration(configuration) {
 	_additionalHttpHeaders = {
 		{ "X-PUBLIC-TOKEN", _configuration.publicToken },
@@ -67,7 +78,7 @@ void APIClient::createTokenWithCard(
 void APIClient::createTokenWithData(
 		QByteArray data,
 		TokenCompletionCallback completion) {
-	const auto url = QUrl(_apiUrl + '/' + TokenEndpoint());
+	const auto url = QUrl(_apiUrl);
 	auto request = QNetworkRequest(url);
 	request.setHeader(
 		QNetworkRequest::ContentTypeHeader,
diff --git a/Telegram/SourceFiles/payments/smartglocal/smartglocal_api_client.h b/Telegram/SourceFiles/payments/smartglocal/smartglocal_api_client.h
index b418188cb..3166b386d 100644
--- a/Telegram/SourceFiles/payments/smartglocal/smartglocal_api_client.h
+++ b/Telegram/SourceFiles/payments/smartglocal/smartglocal_api_client.h
@@ -19,6 +19,7 @@ namespace SmartGlocal {
 
 struct PaymentConfiguration {
 	QString publicToken;
+	QString tokenizeUrl;
 	bool isTest = false;
 };