2021-03-23 12:34:34 +00:00
|
|
|
/*
|
|
|
|
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 "main/main_session.h"
|
|
|
|
#include "main/main_account.h"
|
2021-03-24 11:30:01 +00:00
|
|
|
#include "main/main_domain.h"
|
|
|
|
#include "storage/storage_domain.h"
|
2021-03-23 12:34:34 +00:00
|
|
|
#include "history/history_item.h"
|
|
|
|
#include "history/history.h"
|
2021-03-26 13:05:31 +00:00
|
|
|
#include "data/data_user.h" // UserData::isBot.
|
2021-03-31 17:15:49 +00:00
|
|
|
#include "boxes/passcode_box.h"
|
2021-03-23 12:34:34 +00:00
|
|
|
#include "core/local_url_handlers.h" // TryConvertUrlToLocal.
|
2021-03-25 16:58:52 +00:00
|
|
|
#include "core/file_utilities.h" // File::OpenUrl.
|
2021-03-31 17:15:49 +00:00
|
|
|
#include "core/core_cloud_password.h" // Core::CloudPasswordState
|
|
|
|
#include "lang/lang_keys.h"
|
2021-03-23 12:34:34 +00:00
|
|
|
#include "apiwrap.h"
|
2021-08-02 19:06:33 +00:00
|
|
|
#include "api/api_cloud_password.h"
|
2021-03-23 12:34:34 +00:00
|
|
|
|
|
|
|
#include <QJsonDocument>
|
|
|
|
#include <QJsonObject>
|
|
|
|
#include <QJsonArray>
|
|
|
|
#include <QJsonValue>
|
|
|
|
|
|
|
|
namespace Payments {
|
|
|
|
namespace {
|
|
|
|
|
|
|
|
struct SessionProcesses {
|
|
|
|
base::flat_map<FullMsgId, std::unique_ptr<CheckoutProcess>> map;
|
2021-04-12 08:50:31 +00:00
|
|
|
base::flat_set<FullMsgId> paymentStarted;
|
2021-03-23 12:34:34 +00:00
|
|
|
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
|
|
|
|
|
2021-03-31 05:56:45 +00:00
|
|
|
void CheckoutProcess::Start(
|
|
|
|
not_null<const HistoryItem*> item,
|
|
|
|
Mode mode,
|
|
|
|
Fn<void()> reactivate) {
|
2021-03-23 12:34:34 +00:00
|
|
|
auto &processes = LookupSessionProcesses(item);
|
2021-03-30 08:16:05 +00:00
|
|
|
const auto media = item->media();
|
|
|
|
const auto invoice = media ? media->invoice() : nullptr;
|
|
|
|
if (mode == Mode::Payment && !invoice) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
const auto id = (invoice && invoice->receiptMsgId)
|
|
|
|
? FullMsgId(item->history()->channelId(), invoice->receiptMsgId)
|
|
|
|
: item->fullId();
|
|
|
|
if (invoice) {
|
|
|
|
mode = invoice->receiptMsgId ? Mode::Receipt : Mode::Payment;
|
|
|
|
} else if (mode == Mode::Payment) {
|
|
|
|
LOG(("API Error: CheckoutProcess Payment start without invoice."));
|
|
|
|
return;
|
|
|
|
}
|
2021-03-23 12:34:34 +00:00
|
|
|
const auto i = processes.map.find(id);
|
|
|
|
if (i != end(processes.map)) {
|
2021-03-31 05:56:45 +00:00
|
|
|
i->second->setReactivateCallback(std::move(reactivate));
|
2021-03-23 12:34:34 +00:00
|
|
|
i->second->requestActivate();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
const auto j = processes.map.emplace(
|
|
|
|
id,
|
2021-03-30 08:16:05 +00:00
|
|
|
std::make_unique<CheckoutProcess>(
|
|
|
|
item->history()->peer,
|
|
|
|
id.msg,
|
|
|
|
mode,
|
2021-03-31 05:56:45 +00:00
|
|
|
std::move(reactivate),
|
2021-03-30 08:16:05 +00:00
|
|
|
PrivateTag{})).first;
|
2021-03-23 12:34:34 +00:00
|
|
|
j->second->requestActivate();
|
|
|
|
}
|
|
|
|
|
2021-04-12 08:50:31 +00:00
|
|
|
bool CheckoutProcess::TakePaymentStarted(
|
|
|
|
not_null<const HistoryItem*> item) {
|
|
|
|
const auto session = &item->history()->session();
|
|
|
|
const auto itemId = item->fullId();
|
|
|
|
const auto i = Processes.find(session);
|
|
|
|
if (i == end(Processes) || !i->second.paymentStarted.contains(itemId)) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
i->second.paymentStarted.erase(itemId);
|
|
|
|
const auto j = i->second.map.find(itemId);
|
|
|
|
if (j != end(i->second.map)) {
|
|
|
|
j->second->closeAndReactivate();
|
|
|
|
} else if (i->second.paymentStarted.empty() && i->second.map.empty()) {
|
|
|
|
Processes.erase(i);
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2021-04-13 12:52:40 +00:00
|
|
|
void CheckoutProcess::ClearAll() {
|
|
|
|
Processes.clear();
|
|
|
|
}
|
|
|
|
|
2021-04-12 08:50:31 +00:00
|
|
|
void CheckoutProcess::RegisterPaymentStart(
|
|
|
|
not_null<CheckoutProcess*> process) {
|
|
|
|
const auto i = Processes.find(process->_session);
|
|
|
|
Assert(i != end(Processes));
|
|
|
|
for (const auto &[itemId, itemProcess] : i->second.map) {
|
|
|
|
if (itemProcess.get() == process) {
|
|
|
|
i->second.paymentStarted.emplace(itemId);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void CheckoutProcess::UnregisterPaymentStart(
|
|
|
|
not_null<CheckoutProcess*> process) {
|
|
|
|
const auto i = Processes.find(process->_session);
|
|
|
|
if (i != end(Processes)) {
|
|
|
|
for (const auto &[itemId, itemProcess] : i->second.map) {
|
|
|
|
if (itemProcess.get() == process) {
|
|
|
|
i->second.paymentStarted.emplace(itemId);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-03-23 12:34:34 +00:00
|
|
|
CheckoutProcess::CheckoutProcess(
|
2021-03-30 08:16:05 +00:00
|
|
|
not_null<PeerData*> peer,
|
|
|
|
MsgId itemId,
|
|
|
|
Mode mode,
|
2021-03-31 05:56:45 +00:00
|
|
|
Fn<void()> reactivate,
|
2021-03-23 12:34:34 +00:00
|
|
|
PrivateTag)
|
2021-03-30 08:16:05 +00:00
|
|
|
: _session(&peer->session())
|
|
|
|
, _form(std::make_unique<Form>(peer, itemId, (mode == Mode::Receipt)))
|
2021-03-31 05:56:45 +00:00
|
|
|
, _panel(std::make_unique<Ui::Panel>(panelDelegate()))
|
|
|
|
, _reactivate(std::move(reactivate)) {
|
2021-03-23 12:34:34 +00:00
|
|
|
_form->updates(
|
|
|
|
) | rpl::start_with_next([=](const FormUpdate &update) {
|
|
|
|
handleFormUpdate(update);
|
|
|
|
}, _lifetime);
|
2021-03-31 17:15:49 +00:00
|
|
|
|
2021-03-24 11:30:01 +00:00
|
|
|
_panel->backRequests(
|
|
|
|
) | rpl::start_with_next([=] {
|
2021-03-31 06:19:14 +00:00
|
|
|
panelCancelEdit();
|
2021-03-24 11:30:01 +00:00
|
|
|
}, _panel->lifetime());
|
2021-03-26 15:23:12 +00:00
|
|
|
showForm();
|
2021-04-12 15:24:36 +00:00
|
|
|
_panel->toggleProgress(true);
|
2021-03-31 17:15:49 +00:00
|
|
|
|
|
|
|
if (mode == Mode::Payment) {
|
2021-08-02 19:06:33 +00:00
|
|
|
_session->api().cloudPassword().state(
|
2021-03-31 17:15:49 +00:00
|
|
|
) | rpl::start_with_next([=](const Core::CloudPasswordState &state) {
|
|
|
|
_form->setHasPassword(!!state.request);
|
|
|
|
}, _lifetime);
|
|
|
|
}
|
2021-03-23 12:34:34 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
CheckoutProcess::~CheckoutProcess() {
|
|
|
|
}
|
|
|
|
|
2021-03-31 05:56:45 +00:00
|
|
|
void CheckoutProcess::setReactivateCallback(Fn<void()> reactivate) {
|
|
|
|
_reactivate = std::move(reactivate);
|
|
|
|
}
|
|
|
|
|
2021-03-23 12:34:34 +00:00
|
|
|
void CheckoutProcess::requestActivate() {
|
|
|
|
_panel->requestActivate();
|
|
|
|
}
|
|
|
|
|
|
|
|
not_null<Ui::PanelDelegate*> CheckoutProcess::panelDelegate() {
|
|
|
|
return static_cast<PanelDelegate*>(this);
|
|
|
|
}
|
|
|
|
|
|
|
|
void CheckoutProcess::handleFormUpdate(const FormUpdate &update) {
|
2021-04-12 15:24:36 +00:00
|
|
|
v::match(update, [&](const ToggleProgress &data) {
|
|
|
|
_panel->toggleProgress(data.shown);
|
|
|
|
}, [&](const FormReady &) {
|
2021-03-24 12:41:46 +00:00
|
|
|
performInitialSilentValidation();
|
|
|
|
if (!_initialSilentValidation) {
|
|
|
|
showForm();
|
|
|
|
}
|
2021-03-31 17:15:49 +00:00
|
|
|
if (_form->paymentMethod().savedCredentials) {
|
2021-08-02 19:06:33 +00:00
|
|
|
_session->api().cloudPassword().reload();
|
2021-03-31 17:15:49 +00:00
|
|
|
}
|
2021-03-26 13:05:31 +00:00
|
|
|
}, [&](const ThumbnailUpdated &data) {
|
|
|
|
_panel->updateFormThumbnail(data.thumbnail);
|
2021-03-23 16:06:59 +00:00
|
|
|
}, [&](const ValidateFinished &) {
|
2021-03-24 12:41:46 +00:00
|
|
|
if (_initialSilentValidation) {
|
|
|
|
_initialSilentValidation = false;
|
|
|
|
}
|
2021-03-23 16:06:59 +00:00
|
|
|
showForm();
|
2021-03-31 17:15:49 +00:00
|
|
|
const auto submitted = (_submitState == SubmitState::Validating);
|
|
|
|
_submitState = SubmitState::Validated;
|
|
|
|
if (submitted) {
|
2021-03-23 16:06:59 +00:00
|
|
|
panelSubmit();
|
|
|
|
}
|
2021-03-31 17:15:49 +00:00
|
|
|
}, [&](const PaymentMethodUpdate &data) {
|
2021-03-25 16:58:52 +00:00
|
|
|
showForm();
|
2021-03-31 17:15:49 +00:00
|
|
|
if (data.requestNewPassword) {
|
|
|
|
requestSetPassword();
|
|
|
|
}
|
|
|
|
}, [&](const TmpPasswordRequired &) {
|
2021-04-12 08:50:31 +00:00
|
|
|
UnregisterPaymentStart(this);
|
2021-03-31 17:15:49 +00:00
|
|
|
_submitState = SubmitState::Validated;
|
|
|
|
requestPassword();
|
2021-04-02 12:59:54 +00:00
|
|
|
}, [&](const BotTrustRequired &data) {
|
2021-04-12 08:50:31 +00:00
|
|
|
UnregisterPaymentStart(this);
|
2021-04-02 12:59:54 +00:00
|
|
|
_submitState = SubmitState::Validated;
|
|
|
|
_panel->showWarning(data.bot->name, data.provider->name);
|
|
|
|
if (const auto box = _enterPasswordBox.data()) {
|
|
|
|
box->closeBox();
|
|
|
|
}
|
2021-03-26 13:05:31 +00:00
|
|
|
}, [&](const VerificationNeeded &data) {
|
2021-04-01 14:39:44 +00:00
|
|
|
auto bottomText = tr::lng_payments_processed_by(
|
|
|
|
lt_provider,
|
|
|
|
rpl::single(_form->invoice().provider));
|
|
|
|
if (!_panel->showWebview(data.url, false, std::move(bottomText))) {
|
2021-03-26 13:05:31 +00:00
|
|
|
File::OpenUrl(data.url);
|
2021-03-31 05:56:45 +00:00
|
|
|
close();
|
2021-03-23 12:34:34 +00:00
|
|
|
}
|
2021-03-26 13:05:31 +00:00
|
|
|
}, [&](const PaymentFinished &data) {
|
2021-03-23 12:34:34 +00:00
|
|
|
const auto weak = base::make_weak(this);
|
2021-03-26 13:05:31 +00:00
|
|
|
_session->api().applyUpdates(data.updates);
|
2021-03-23 12:34:34 +00:00
|
|
|
if (weak) {
|
2021-03-31 05:56:45 +00:00
|
|
|
closeAndReactivate();
|
2021-03-23 12:34:34 +00:00
|
|
|
}
|
2021-03-24 11:30:01 +00:00
|
|
|
}, [&](const Error &error) {
|
|
|
|
handleError(error);
|
2021-03-23 12:34:34 +00:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2021-03-24 11:30:01 +00:00
|
|
|
void CheckoutProcess::handleError(const Error &error) {
|
|
|
|
const auto showToast = [&](const TextWithEntities &text) {
|
2021-03-31 05:56:45 +00:00
|
|
|
_panel->requestActivate();
|
|
|
|
_panel->showToast(text);
|
2021-03-24 11:30:01 +00:00
|
|
|
};
|
|
|
|
const auto &id = error.id;
|
|
|
|
switch (error.type) {
|
|
|
|
case Error::Type::Form:
|
2021-04-02 15:18:49 +00:00
|
|
|
if (id == u"INVOICE_ALREADY_PAID"_q) {
|
|
|
|
_panel->showCriticalError({
|
|
|
|
tr::lng_payments_already_paid(tr::now)
|
|
|
|
});
|
|
|
|
} else if (true
|
2021-03-24 11:30:01 +00:00
|
|
|
|| id == u"PROVIDER_ACCOUNT_INVALID"_q
|
|
|
|
|| id == u"PROVIDER_ACCOUNT_TIMEOUT"_q) {
|
2021-04-02 15:18:49 +00:00
|
|
|
_panel->showCriticalError({ "Error: " + id });
|
2021-03-24 11:30:01 +00:00
|
|
|
}
|
|
|
|
break;
|
2021-03-26 15:23:12 +00:00
|
|
|
case Error::Type::Validate: {
|
2021-03-31 17:15:49 +00:00
|
|
|
if (_submitState == SubmitState::Validating
|
2021-03-30 11:43:47 +00:00
|
|
|
|| _submitState == SubmitState::Validated) {
|
2021-03-24 11:30:01 +00:00
|
|
|
_submitState = SubmitState::None;
|
|
|
|
}
|
2021-03-24 12:41:46 +00:00
|
|
|
if (_initialSilentValidation) {
|
|
|
|
_initialSilentValidation = false;
|
|
|
|
showForm();
|
|
|
|
return;
|
|
|
|
}
|
2021-03-29 12:16:54 +00:00
|
|
|
using InfoField = Ui::InformationField;
|
|
|
|
using CardField = Ui::CardField;
|
2021-03-24 11:30:01 +00:00
|
|
|
if (id == u"REQ_INFO_NAME_INVALID"_q) {
|
2021-03-29 12:16:54 +00:00
|
|
|
showInformationError(InfoField::Name);
|
2021-03-24 11:30:01 +00:00
|
|
|
} else if (id == u"REQ_INFO_EMAIL_INVALID"_q) {
|
2021-03-29 12:16:54 +00:00
|
|
|
showInformationError(InfoField::Email);
|
2021-03-24 11:30:01 +00:00
|
|
|
} else if (id == u"REQ_INFO_PHONE_INVALID"_q) {
|
2021-03-29 12:16:54 +00:00
|
|
|
showInformationError(InfoField::Phone);
|
2021-03-24 11:30:01 +00:00
|
|
|
} else if (id == u"ADDRESS_STREET_LINE1_INVALID"_q) {
|
2021-03-29 12:16:54 +00:00
|
|
|
showInformationError(InfoField::ShippingStreet);
|
2021-03-24 11:30:01 +00:00
|
|
|
} else if (id == u"ADDRESS_CITY_INVALID"_q) {
|
2021-03-29 12:16:54 +00:00
|
|
|
showInformationError(InfoField::ShippingCity);
|
2021-03-24 11:30:01 +00:00
|
|
|
} else if (id == u"ADDRESS_STATE_INVALID"_q) {
|
2021-03-29 12:16:54 +00:00
|
|
|
showInformationError(InfoField::ShippingState);
|
2021-03-24 11:30:01 +00:00
|
|
|
} else if (id == u"ADDRESS_COUNTRY_INVALID"_q) {
|
2021-03-29 12:16:54 +00:00
|
|
|
showInformationError(InfoField::ShippingCountry);
|
2021-03-24 11:30:01 +00:00
|
|
|
} else if (id == u"ADDRESS_POSTCODE_INVALID"_q) {
|
2021-03-29 12:16:54 +00:00
|
|
|
showInformationError(InfoField::ShippingPostcode);
|
|
|
|
} else if (id == u"LOCAL_CARD_NUMBER_INVALID"_q) {
|
|
|
|
showCardError(CardField::Number);
|
|
|
|
} else if (id == u"LOCAL_CARD_EXPIRE_DATE_INVALID"_q) {
|
|
|
|
showCardError(CardField::ExpireDate);
|
|
|
|
} else if (id == u"LOCAL_CARD_CVC_INVALID"_q) {
|
|
|
|
showCardError(CardField::Cvc);
|
|
|
|
} else if (id == u"LOCAL_CARD_HOLDER_NAME_INVALID"_q) {
|
|
|
|
showCardError(CardField::Name);
|
|
|
|
} else if (id == u"LOCAL_CARD_BILLING_COUNTRY_INVALID"_q) {
|
|
|
|
showCardError(CardField::AddressCountry);
|
|
|
|
} else if (id == u"LOCAL_CARD_BILLING_ZIP_INVALID"_q) {
|
|
|
|
showCardError(CardField::AddressZip);
|
2021-03-24 11:30:01 +00:00
|
|
|
} else if (id == u"SHIPPING_BOT_TIMEOUT"_q) {
|
2021-04-02 15:18:49 +00:00
|
|
|
showToast({ "Error: Bot Timeout!" });
|
2021-03-24 11:30:01 +00:00
|
|
|
} else if (id == u"SHIPPING_NOT_AVAILABLE"_q) {
|
2021-04-02 15:18:49 +00:00
|
|
|
showToast({ tr::lng_payments_shipping_not_available(tr::now) });
|
2021-03-24 11:30:01 +00:00
|
|
|
} else {
|
|
|
|
showToast({ "Error: " + id });
|
|
|
|
}
|
2021-03-26 15:23:12 +00:00
|
|
|
} break;
|
|
|
|
case Error::Type::Stripe: {
|
|
|
|
using Field = Ui::CardField;
|
|
|
|
if (id == u"InvalidNumber"_q || id == u"IncorrectNumber"_q) {
|
|
|
|
showCardError(Field::Number);
|
|
|
|
} else if (id == u"InvalidCVC"_q || id == u"IncorrectCVC"_q) {
|
2021-03-29 12:16:54 +00:00
|
|
|
showCardError(Field::Cvc);
|
2021-03-26 15:23:12 +00:00
|
|
|
} else if (id == u"InvalidExpiryMonth"_q
|
|
|
|
|| id == u"InvalidExpiryYear"_q
|
|
|
|
|| id == u"ExpiredCard"_q) {
|
|
|
|
showCardError(Field::ExpireDate);
|
2021-03-29 12:16:54 +00:00
|
|
|
} else if (id == u"CardDeclined"_q) {
|
2021-04-02 15:18:49 +00:00
|
|
|
showToast({ tr::lng_payments_card_declined(tr::now) });
|
2021-03-29 12:16:54 +00:00
|
|
|
} else if (id == u"ProcessingError"_q) {
|
2021-04-02 15:18:49 +00:00
|
|
|
showToast({ "Sorry, a processing error occurred." });
|
2021-03-26 15:23:12 +00:00
|
|
|
} else {
|
2021-04-07 12:50:55 +00:00
|
|
|
showToast({ "Stripe Error: " + id });
|
2021-03-26 15:23:12 +00:00
|
|
|
}
|
|
|
|
} break;
|
2021-04-07 12:50:55 +00:00
|
|
|
case Error::Type::SmartGlocal: {
|
2021-04-12 08:50:31 +00:00
|
|
|
showToast({ "SmartGlocal Error: " + id });
|
2021-04-07 12:50:55 +00:00
|
|
|
} break;
|
2021-03-31 17:15:49 +00:00
|
|
|
case Error::Type::TmpPassword:
|
|
|
|
if (const auto box = _enterPasswordBox.data()) {
|
|
|
|
if (!box->handleCustomCheckError(id)) {
|
|
|
|
showToast({ "Error: Could not generate tmp password." });
|
|
|
|
}
|
|
|
|
}
|
|
|
|
break;
|
2021-03-24 11:30:01 +00:00
|
|
|
case Error::Type::Send:
|
2021-03-31 17:15:49 +00:00
|
|
|
if (const auto box = _enterPasswordBox.data()) {
|
|
|
|
box->closeBox();
|
|
|
|
}
|
2021-03-24 11:30:01 +00:00
|
|
|
if (_submitState == SubmitState::Finishing) {
|
2021-04-12 08:50:31 +00:00
|
|
|
UnregisterPaymentStart(this);
|
2021-03-31 17:15:49 +00:00
|
|
|
_submitState = SubmitState::Validated;
|
2021-03-24 11:30:01 +00:00
|
|
|
}
|
2021-04-02 15:18:49 +00:00
|
|
|
if (id == u"INVOICE_ALREADY_PAID"_q) {
|
|
|
|
showToast({ tr::lng_payments_already_paid(tr::now) });
|
|
|
|
} else if (id == u"PAYMENT_FAILED"_q) {
|
|
|
|
showToast({ tr::lng_payments_payment_failed(tr::now) });
|
2021-03-24 11:30:01 +00:00
|
|
|
} else if (id == u"BOT_PRECHECKOUT_FAILED"_q) {
|
2021-04-02 15:18:49 +00:00
|
|
|
showToast({ tr::lng_payments_precheckout_failed(tr::now) });
|
2021-03-24 11:30:01 +00:00
|
|
|
} else if (id == u"REQUESTED_INFO_INVALID"_q
|
|
|
|
|| id == u"SHIPPING_OPTION_INVALID"_q
|
|
|
|
|| id == u"PAYMENT_CREDENTIALS_INVALID"_q
|
|
|
|
|| id == u"PAYMENT_CREDENTIALS_ID_INVALID"_q) {
|
2021-04-02 15:18:49 +00:00
|
|
|
showToast({ tr::lng_payments_payment_failed(tr::now) });
|
2021-03-24 11:30:01 +00:00
|
|
|
showToast({ "Error: " + id + ". Your card has not been billed." });
|
2021-03-31 17:15:49 +00:00
|
|
|
} else if (id == u"TMP_PASSWORD_INVALID"_q) {
|
2021-04-02 15:18:49 +00:00
|
|
|
requestPassword();
|
2021-03-30 14:49:41 +00:00
|
|
|
} else {
|
|
|
|
showToast({ "Error: " + id });
|
2021-03-24 11:30:01 +00:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
default: Unexpected("Error type in CheckoutProcess::handleError.");
|
2021-03-23 16:06:59 +00:00
|
|
|
}
|
2021-03-23 12:34:34 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void CheckoutProcess::panelRequestClose() {
|
2021-04-02 12:10:23 +00:00
|
|
|
if (_form->hasChanges()) {
|
|
|
|
_panel->showCloseConfirm();
|
|
|
|
} else {
|
|
|
|
panelCloseSure();
|
|
|
|
}
|
2021-03-23 12:34:34 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void CheckoutProcess::panelCloseSure() {
|
2021-03-31 05:56:45 +00:00
|
|
|
closeAndReactivate();
|
|
|
|
}
|
|
|
|
|
|
|
|
void CheckoutProcess::closeAndReactivate() {
|
|
|
|
const auto reactivate = std::move(_reactivate);
|
|
|
|
close();
|
|
|
|
if (reactivate) {
|
|
|
|
reactivate();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void CheckoutProcess::close() {
|
2021-03-23 12:34:34 +00:00
|
|
|
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);
|
2021-04-12 08:50:31 +00:00
|
|
|
if (i->second.map.empty() && i->second.paymentStarted.empty()) {
|
2021-03-23 12:34:34 +00:00
|
|
|
Processes.erase(i);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void CheckoutProcess::panelSubmit() {
|
2021-03-30 11:43:47 +00:00
|
|
|
if (_form->invoice().receipt.paid) {
|
2021-03-31 05:56:45 +00:00
|
|
|
closeAndReactivate();
|
2021-03-30 14:49:41 +00:00
|
|
|
return;
|
2021-03-31 17:15:49 +00:00
|
|
|
} else if (_submitState == SubmitState::Validating
|
2021-03-23 16:06:59 +00:00
|
|
|
|| _submitState == SubmitState::Finishing) {
|
|
|
|
return;
|
|
|
|
}
|
2021-03-25 16:58:52 +00:00
|
|
|
const auto &method = _form->paymentMethod();
|
2021-03-23 16:06:59 +00:00
|
|
|
const auto &invoice = _form->invoice();
|
|
|
|
const auto &options = _form->shippingOptions();
|
|
|
|
if (!options.list.empty() && options.selectedId.isEmpty()) {
|
|
|
|
chooseShippingOption();
|
|
|
|
} else if (_submitState != SubmitState::Validated
|
|
|
|
&& options.list.empty()
|
|
|
|
&& (invoice.isShippingAddressRequested
|
|
|
|
|| invoice.isNameRequested
|
|
|
|
|| invoice.isEmailRequested
|
|
|
|
|| invoice.isPhoneRequested)) {
|
2021-03-31 17:15:49 +00:00
|
|
|
_submitState = SubmitState::Validating;
|
2021-04-02 12:10:23 +00:00
|
|
|
_form->validateInformation(_form->information());
|
2021-03-25 16:58:52 +00:00
|
|
|
} else if (!method.newCredentials && !method.savedCredentials) {
|
2021-03-25 15:27:30 +00:00
|
|
|
editPaymentMethod();
|
2021-03-31 17:15:49 +00:00
|
|
|
} else {
|
2021-04-12 08:50:31 +00:00
|
|
|
RegisterPaymentStart(this);
|
2021-03-31 17:15:49 +00:00
|
|
|
_submitState = SubmitState::Finishing;
|
|
|
|
_form->submit();
|
2021-03-23 16:06:59 +00:00
|
|
|
}
|
2021-03-23 12:34:34 +00:00
|
|
|
}
|
|
|
|
|
2021-04-02 12:59:54 +00:00
|
|
|
void CheckoutProcess::panelTrustAndSubmit() {
|
|
|
|
_form->trustBot();
|
|
|
|
panelSubmit();
|
|
|
|
}
|
|
|
|
|
2021-03-31 17:15:49 +00:00
|
|
|
void CheckoutProcess::panelWebviewMessage(
|
|
|
|
const QJsonDocument &message,
|
|
|
|
bool saveInformation) {
|
2021-03-23 12:34:34 +00:00
|
|
|
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;
|
|
|
|
}
|
2021-03-25 16:58:52 +00:00
|
|
|
crl::on_main(this, [=] {
|
|
|
|
_form->setPaymentCredentials(NewCredentials{
|
|
|
|
.title = title,
|
|
|
|
.data = QJsonDocument(
|
|
|
|
credentials.toObject()
|
|
|
|
).toJson(QJsonDocument::Compact),
|
2021-03-31 17:15:49 +00:00
|
|
|
.saveOnServer = saveInformation,
|
2021-03-25 16:58:52 +00:00
|
|
|
});
|
|
|
|
});
|
2021-03-23 12:34:34 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
bool CheckoutProcess::panelWebviewNavigationAttempt(const QString &uri) {
|
|
|
|
if (Core::TryConvertUrlToLocal(uri) == uri) {
|
|
|
|
return true;
|
|
|
|
}
|
2021-03-31 05:56:45 +00:00
|
|
|
crl::on_main(this, [=] { closeAndReactivate(); });
|
2021-03-23 12:34:34 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2021-03-31 06:19:14 +00:00
|
|
|
void CheckoutProcess::panelCancelEdit() {
|
2021-04-01 09:27:39 +00:00
|
|
|
if (_submitState != SubmitState::None
|
|
|
|
&& _submitState != SubmitState::Validated) {
|
2021-03-31 06:19:14 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
showForm();
|
|
|
|
}
|
|
|
|
|
2021-03-25 15:27:30 +00:00
|
|
|
void CheckoutProcess::panelEditPaymentMethod() {
|
|
|
|
if (_submitState != SubmitState::None
|
|
|
|
&& _submitState != SubmitState::Validated) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
editPaymentMethod();
|
|
|
|
}
|
|
|
|
|
2021-03-31 17:15:49 +00:00
|
|
|
void CheckoutProcess::panelValidateCard(
|
|
|
|
Ui::UncheckedCardDetails data,
|
|
|
|
bool saveInformation) {
|
|
|
|
_form->validateCard(data, saveInformation);
|
2021-03-25 15:27:30 +00:00
|
|
|
}
|
|
|
|
|
2021-03-23 16:06:59 +00:00
|
|
|
void CheckoutProcess::panelEditShippingInformation() {
|
2021-03-25 15:27:30 +00:00
|
|
|
showEditInformation(Ui::InformationField::ShippingStreet);
|
2021-03-23 16:06:59 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void CheckoutProcess::panelEditName() {
|
2021-03-25 15:27:30 +00:00
|
|
|
showEditInformation(Ui::InformationField::Name);
|
2021-03-23 16:06:59 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void CheckoutProcess::panelEditEmail() {
|
2021-03-25 15:27:30 +00:00
|
|
|
showEditInformation(Ui::InformationField::Email);
|
2021-03-23 16:06:59 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void CheckoutProcess::panelEditPhone() {
|
2021-03-25 15:27:30 +00:00
|
|
|
showEditInformation(Ui::InformationField::Phone);
|
2021-03-23 16:06:59 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void CheckoutProcess::showForm() {
|
|
|
|
_panel->showForm(
|
|
|
|
_form->invoice(),
|
2021-04-02 12:10:23 +00:00
|
|
|
_form->information(),
|
2021-03-25 16:58:52 +00:00
|
|
|
_form->paymentMethod().ui,
|
2021-03-23 16:06:59 +00:00
|
|
|
_form->shippingOptions());
|
|
|
|
}
|
|
|
|
|
2021-03-25 15:27:30 +00:00
|
|
|
void CheckoutProcess::showEditInformation(Ui::InformationField field) {
|
2021-04-01 09:27:39 +00:00
|
|
|
if (_submitState != SubmitState::None
|
|
|
|
&& _submitState != SubmitState::Validated) {
|
2021-03-23 16:06:59 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
_panel->showEditInformation(
|
|
|
|
_form->invoice(),
|
2021-04-02 12:10:23 +00:00
|
|
|
_form->information(),
|
2021-03-23 16:06:59 +00:00
|
|
|
field);
|
|
|
|
}
|
|
|
|
|
2021-03-25 15:27:30 +00:00
|
|
|
void CheckoutProcess::showInformationError(Ui::InformationField field) {
|
2021-04-01 09:27:39 +00:00
|
|
|
Expects(_submitState != SubmitState::Validated);
|
|
|
|
|
2021-03-24 11:30:01 +00:00
|
|
|
if (_submitState != SubmitState::None) {
|
|
|
|
return;
|
|
|
|
}
|
2021-03-25 15:27:30 +00:00
|
|
|
_panel->showInformationError(
|
2021-03-24 11:30:01 +00:00
|
|
|
_form->invoice(),
|
2021-04-02 12:10:23 +00:00
|
|
|
_form->information(),
|
2021-03-24 11:30:01 +00:00
|
|
|
field);
|
|
|
|
}
|
|
|
|
|
2021-03-26 15:23:12 +00:00
|
|
|
void CheckoutProcess::showCardError(Ui::CardField field) {
|
2021-04-01 09:27:39 +00:00
|
|
|
if (_submitState != SubmitState::None
|
|
|
|
&& _submitState != SubmitState::Validated) {
|
2021-03-26 15:23:12 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
_panel->showCardError(_form->paymentMethod().ui.native, field);
|
|
|
|
}
|
|
|
|
|
2021-03-23 16:06:59 +00:00
|
|
|
void CheckoutProcess::chooseShippingOption() {
|
|
|
|
_panel->chooseShippingOption(_form->shippingOptions());
|
|
|
|
}
|
|
|
|
|
2021-03-30 14:49:41 +00:00
|
|
|
void CheckoutProcess::chooseTips() {
|
|
|
|
_panel->chooseTips(_form->invoice());
|
|
|
|
}
|
|
|
|
|
2021-03-25 15:27:30 +00:00
|
|
|
void CheckoutProcess::editPaymentMethod() {
|
2021-03-25 16:58:52 +00:00
|
|
|
_panel->choosePaymentMethod(_form->paymentMethod().ui);
|
2021-03-25 15:27:30 +00:00
|
|
|
}
|
|
|
|
|
2021-03-31 17:15:49 +00:00
|
|
|
void CheckoutProcess::requestSetPassword() {
|
2021-08-02 19:06:33 +00:00
|
|
|
_session->api().cloudPassword().reload();
|
2021-03-31 17:15:49 +00:00
|
|
|
_panel->askSetPassword();
|
|
|
|
}
|
|
|
|
|
|
|
|
void CheckoutProcess::requestPassword() {
|
|
|
|
getPasswordState([=](const Core::CloudPasswordState &state) {
|
|
|
|
auto fields = PasscodeBox::CloudFields::From(state);
|
|
|
|
fields.customTitle = tr::lng_payments_password_title();
|
|
|
|
fields.customDescription = tr::lng_payments_password_description(
|
|
|
|
tr::now,
|
|
|
|
lt_card,
|
|
|
|
_form->paymentMethod().savedCredentials.title);
|
|
|
|
fields.customSubmitButton = tr::lng_payments_password_submit();
|
|
|
|
fields.customCheckCallback = [=](
|
|
|
|
const Core::CloudPasswordResult &result) {
|
|
|
|
_form->submit(result);
|
|
|
|
};
|
|
|
|
auto owned = Box<PasscodeBox>(_session, fields);
|
|
|
|
_enterPasswordBox = owned.data();
|
|
|
|
_panel->showBox(std::move(owned));
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
void CheckoutProcess::panelSetPassword() {
|
|
|
|
getPasswordState([=](const Core::CloudPasswordState &state) {
|
|
|
|
if (state.request) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
auto owned = Box<PasscodeBox>(
|
|
|
|
_session,
|
|
|
|
PasscodeBox::CloudFields::From(state));
|
|
|
|
const auto box = owned.data();
|
|
|
|
|
|
|
|
rpl::merge(
|
|
|
|
box->newPasswordSet() | rpl::to_empty,
|
|
|
|
box->passwordReloadNeeded()
|
|
|
|
) | rpl::start_with_next([=] {
|
2021-08-02 19:06:33 +00:00
|
|
|
_session->api().cloudPassword().reload();
|
2021-03-31 17:15:49 +00:00
|
|
|
}, box->lifetime());
|
|
|
|
|
|
|
|
box->clearUnconfirmedPassword(
|
|
|
|
) | rpl::start_with_next([=] {
|
2021-08-02 19:06:33 +00:00
|
|
|
_session->api().cloudPassword().clearUnconfirmedPassword();
|
2021-03-31 17:15:49 +00:00
|
|
|
}, box->lifetime());
|
|
|
|
|
|
|
|
_panel->showBox(std::move(owned));
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2021-04-02 15:18:49 +00:00
|
|
|
void CheckoutProcess::panelOpenUrl(const QString &url) {
|
|
|
|
File::OpenUrl(url);
|
|
|
|
}
|
|
|
|
|
2021-03-31 17:15:49 +00:00
|
|
|
void CheckoutProcess::getPasswordState(
|
|
|
|
Fn<void(const Core::CloudPasswordState&)> callback) {
|
|
|
|
Expects(callback != nullptr);
|
|
|
|
|
|
|
|
if (_gettingPasswordState) {
|
|
|
|
return;
|
|
|
|
}
|
2021-08-02 19:06:33 +00:00
|
|
|
_session->api().cloudPassword().state(
|
2021-03-31 17:15:49 +00:00
|
|
|
) | rpl::start_with_next([=](const Core::CloudPasswordState &state) {
|
|
|
|
_gettingPasswordState.destroy();
|
|
|
|
callback(state);
|
|
|
|
}, _gettingPasswordState);
|
|
|
|
}
|
|
|
|
|
2021-03-23 16:06:59 +00:00
|
|
|
void CheckoutProcess::panelChooseShippingOption() {
|
2021-04-01 05:05:02 +00:00
|
|
|
if (_submitState != SubmitState::None
|
|
|
|
&& _submitState != SubmitState::Validated) {
|
2021-03-23 16:06:59 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
chooseShippingOption();
|
|
|
|
}
|
|
|
|
|
|
|
|
void CheckoutProcess::panelChangeShippingOption(const QString &id) {
|
|
|
|
_form->setShippingOption(id);
|
|
|
|
showForm();
|
|
|
|
}
|
|
|
|
|
2021-03-30 14:49:41 +00:00
|
|
|
void CheckoutProcess::panelChooseTips() {
|
2021-04-01 05:05:02 +00:00
|
|
|
if (_submitState != SubmitState::None
|
|
|
|
&& _submitState != SubmitState::Validated) {
|
2021-03-30 14:49:41 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
chooseTips();
|
|
|
|
}
|
|
|
|
|
|
|
|
void CheckoutProcess::panelChangeTips(int64 value) {
|
|
|
|
_form->setTips(value);
|
|
|
|
showForm();
|
|
|
|
}
|
|
|
|
|
2021-03-23 16:06:59 +00:00
|
|
|
void CheckoutProcess::panelValidateInformation(
|
|
|
|
Ui::RequestedInformation data) {
|
2021-03-31 17:15:49 +00:00
|
|
|
if (_submitState == SubmitState::Validated) {
|
|
|
|
_submitState = SubmitState::None;
|
|
|
|
}
|
2021-03-23 16:06:59 +00:00
|
|
|
_form->validateInformation(data);
|
|
|
|
}
|
|
|
|
|
|
|
|
void CheckoutProcess::panelShowBox(object_ptr<Ui::BoxContent> box) {
|
|
|
|
_panel->showBox(std::move(box));
|
|
|
|
}
|
|
|
|
|
2021-03-24 12:41:46 +00:00
|
|
|
void CheckoutProcess::performInitialSilentValidation() {
|
|
|
|
const auto &invoice = _form->invoice();
|
2021-04-02 12:10:23 +00:00
|
|
|
const auto &saved = _form->information();
|
2021-03-30 06:01:31 +00:00
|
|
|
if (invoice.receipt
|
|
|
|
|| (invoice.isNameRequested && saved.name.isEmpty())
|
2021-03-24 12:41:46 +00:00
|
|
|
|| (invoice.isEmailRequested && saved.email.isEmpty())
|
|
|
|
|| (invoice.isPhoneRequested && saved.phone.isEmpty())
|
|
|
|
|| (invoice.isShippingAddressRequested && !saved.shippingAddress)) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
_initialSilentValidation = true;
|
|
|
|
_form->validateInformation(saved);
|
|
|
|
}
|
|
|
|
|
2021-03-25 16:58:52 +00:00
|
|
|
QString CheckoutProcess::panelWebviewDataPath() {
|
2021-03-24 11:30:01 +00:00
|
|
|
return _session->domain().local().webviewDataPath();
|
|
|
|
}
|
|
|
|
|
2021-03-23 12:34:34 +00:00
|
|
|
} // namespace Payments
|