summaryrefslogtreecommitdiffstats
path: root/Telegram/SourceFiles/payments/payments_checkout_process.cpp
blob: 916150a204702e388e7b47def6caad3eb6d0b88e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
/*
This file is part of Telegram Desktop,
the official desktop application for the Telegram messaging service.

For license and copyright information please follow this link:
https://github.com/telegramdesktop/tdesktop/blob/master/LEGAL
*/
#include "payments/payments_checkout_process.h"

#include "payments/payments_form.h"
#include "payments/ui/payments_panel.h"
#include "payments/ui/payments_webview.h"
#include "main/main_session.h"
#include "main/main_account.h"
#include "history/history_item.h"
#include "history/history.h"
#include "core/local_url_handlers.h" // TryConvertUrlToLocal.
#include "apiwrap.h"

// #TODO payments errors
#include "mainwindow.h"
#include "ui/toast/toast.h"

#include <QJsonDocument>
#include <QJsonObject>
#include <QJsonArray>
#include <QJsonValue>

namespace Payments {
namespace {

struct SessionProcesses {
	base::flat_map<FullMsgId, std::unique_ptr<CheckoutProcess>> map;
	rpl::lifetime lifetime;
};

base::flat_map<not_null<Main::Session*>, SessionProcesses> Processes;

[[nodiscard]] SessionProcesses &LookupSessionProcesses(
		not_null<const HistoryItem*> item) {
	const auto session = &item->history()->session();
	const auto i = Processes.find(session);
	if (i != end(Processes)) {
		return i->second;
	}
	const auto j = Processes.emplace(session).first;
	auto &result = j->second;
	session->account().sessionChanges(
	) | rpl::start_with_next([=] {
		Processes.erase(session);
	}, result.lifetime);
	return result;
}

} // namespace

void CheckoutProcess::Start(not_null<const HistoryItem*> item) {
	auto &processes = LookupSessionProcesses(item);
	const auto session = &item->history()->session();
	const auto id = item->fullId();
	const auto i = processes.map.find(id);
	if (i != end(processes.map)) {
		i->second->requestActivate();
		return;
	}
	const auto j = processes.map.emplace(
		id,
		std::make_unique<CheckoutProcess>(session, id, PrivateTag{})).first;
	j->second->requestActivate();
}

CheckoutProcess::CheckoutProcess(
	not_null<Main::Session*> session,
	FullMsgId itemId,
	PrivateTag)
: _session(session)
, _form(std::make_unique<Form>(session, itemId))
, _panel(std::make_unique<Ui::Panel>(panelDelegate())) {
	_form->updates(
	) | rpl::start_with_next([=](const FormUpdate &update) {
		handleFormUpdate(update);
	}, _lifetime);
}

CheckoutProcess::~CheckoutProcess() {
}

void CheckoutProcess::requestActivate() {
	_panel->requestActivate();
}

not_null<Ui::PanelDelegate*> CheckoutProcess::panelDelegate() {
	return static_cast<PanelDelegate*>(this);
}

void CheckoutProcess::handleFormUpdate(const FormUpdate &update) {
	v::match(update.data, [&](const FormReady &) {
		_panel->showForm(_form->invoice());
	}, [&](const FormError &error) {
		handleFormError(error);
	}, [&](const SendError &error) {
		handleSendError(error);
	}, [&](const VerificationNeeded &info) {
		if (_webviewWindow) {
			_webviewWindow->navigate(info.url);
		} else {
			_webviewWindow = std::make_unique<Ui::WebviewWindow>(
				info.url,
				panelDelegate());
			if (!_webviewWindow->shown()) {
				// #TODO payments errors
			}
		}
	}, [&](const PaymentFinished &result) {
		const auto weak = base::make_weak(this);
		_session->api().applyUpdates(result.updates);
		if (weak) {
			panelCloseSure();
		}
	});
}

void CheckoutProcess::handleFormError(const FormError &error) {
	// #TODO payments errors
	const auto &type = error.type;
	if (type == u"PROVIDER_ACCOUNT_INVALID"_q) {

	} else if (type == u"PROVIDER_ACCOUNT_TIMEOUT"_q) {

	} else if (type == u"INVOICE_ALREADY_PAID"_q) {

	}
	App::wnd()->activate();
	Ui::Toast::Show("payments.getPaymentForm: " + type);
}

void CheckoutProcess::handleSendError(const SendError &error) {
	// #TODO payments errors
	const auto &type = error.type;
	if (type == u"REQUESTED_INFO_INVALID"_q) {

	} else if (type == u"SHIPPING_OPTION_INVALID"_q) {

	} else if (type == u"PAYMENT_FAILED"_q) {

	} else if (type == u"PAYMENT_CREDENTIALS_INVALID"_q) {

	} else if (type == u"PAYMENT_CREDENTIALS_ID_INVALID"_q) {

	} else if (type == u"BOT_PRECHECKOUT_FAILED"_q) {

	}
	App::wnd()->activate();
	Ui::Toast::Show("payments.sendPaymentForm: " + type);
}

void CheckoutProcess::panelRequestClose() {
	panelCloseSure(); // #TODO payments confirmation
}

void CheckoutProcess::panelCloseSure() {
	const auto i = Processes.find(_session);
	if (i == end(Processes)) {
		return;
	}
	const auto j = ranges::find(i->second.map, this, [](const auto &pair) {
		return pair.second.get();
	});
	if (j == end(i->second.map)) {
		return;
	}
	i->second.map.erase(j);
	if (i->second.map.empty()) {
		Processes.erase(i);
	}
}

void CheckoutProcess::panelSubmit() {
	_webviewWindow = std::make_unique<Ui::WebviewWindow>(
		_form->details().url,
		panelDelegate());
	if (!_webviewWindow->shown()) {
		// #TODO payments errors
	}
}

void CheckoutProcess::panelWebviewMessage(const QJsonDocument &message) {
	if (!message.isArray()) {
		LOG(("Payments Error: "
			"Not an array received in buy_callback arguments."));
		return;
	}
	const auto list = message.array();
	if (list.at(0).toString() != "payment_form_submit") {
		return;
	} else if (!list.at(1).isString()) {
		LOG(("Payments Error: "
			"Not a string received in buy_callback result."));
		return;
	}

	auto error = QJsonParseError();
	const auto document = QJsonDocument::fromJson(
		list.at(1).toString().toUtf8(),
		&error);
	if (error.error != QJsonParseError::NoError) {
		LOG(("Payments Error: "
			"Failed to parse buy_callback arguments, error: %1."
			).arg(error.errorString()));
		return;
	} else if (!document.isObject()) {
		LOG(("Payments Error: "
			"Not an object decoded in buy_callback result."));
		return;
	}
	const auto root = document.object();
	const auto title = root.value("title").toString();
	const auto credentials = root.value("credentials");
	if (!credentials.isObject()) {
		LOG(("Payments Error: "
			"Not an object received in payment credentials."));
		return;
	}
	const auto serializedCredentials = QJsonDocument(
		credentials.toObject()
	).toJson(QJsonDocument::Compact);

	_form->send(serializedCredentials);
}

bool CheckoutProcess::panelWebviewNavigationAttempt(const QString &uri) {
	if (Core::TryConvertUrlToLocal(uri) == uri) {
		return true;
	}
	panelCloseSure();
	App::wnd()->activate();
	return false;
}

} // namespace Payments