Support additional payment methods.

This commit is contained in:
John Preston 2022-07-19 14:28:23 +03:00
parent de0eef8cc6
commit bb251627a9
5 changed files with 68 additions and 4 deletions

View file

@ -318,6 +318,9 @@ void Form::processForm(const MTPDpayments_paymentForm &data) {
processSavedCredentials(data);
});
}
if (const auto additional = data.vadditional_methods()) {
processAdditionalPaymentMethods(additional->v);
}
fillPaymentMethodInformation();
_updates.fire(FormReady{});
}
@ -470,6 +473,18 @@ void Form::processSavedCredentials(
refreshPaymentMethodDetails();
}
void Form::processAdditionalPaymentMethods(
const QVector<MTPPaymentFormMethod> &list) {
_paymentMethod.ui.additionalMethods = ranges::views::all(
list
) | ranges::views::transform([](const MTPPaymentFormMethod &method) {
return Ui::PaymentMethodAdditional{
.title = qs(method.data().vtitle()),
.url = qs(method.data().vurl()),
};
}) | ranges::to_vector;
}
void Form::refreshPaymentMethodDetails() {
const auto &saved = _paymentMethod.savedCredentials;
const auto &entered = _paymentMethod.newCredentials;

View file

@ -256,6 +256,8 @@ private:
void processSavedInformation(const MTPDpaymentRequestedInfo &data);
void processSavedCredentials(
const MTPDpaymentSavedCredentialsCard &data);
void processAdditionalPaymentMethods(
const QVector<MTPPaymentFormMethod> &list);
void processShippingOptions(const QVector<MTPShippingOption> &data);
void fillPaymentMethodInformation();
void fillStripeNativeMethod(QJsonObject object);

View file

@ -443,6 +443,26 @@ void Panel::showEditPaymentMethod(const PaymentMethodDetails &method) {
}
}
void Panel::showAdditionalMethod(
const QString &provider,
const PaymentMethodAdditional &method) {
auto bottomText = tr::lng_payments_processed_by(
lt_provider,
rpl::single(provider));
setTitle(rpl::single(method.title));
if (!showWebview(method.url, true, std::move(bottomText))) {
const auto available = Webview::Availability();
if (available.error != Webview::Available::Error::None) {
showWebviewError(
tr::lng_payments_webview_no_use(tr::now),
available);
} else {
showCriticalError({ "Error: Could not initialize WebView." });
}
_widget->setBackAllowed(true);
}
}
void Panel::showWebviewProgress() {
if (_webviewProgress && _progress && _progress->shown) {
return;
@ -572,20 +592,37 @@ postEvent: function(eventType, eventData) {
}
void Panel::choosePaymentMethod(const PaymentMethodDetails &method) {
if (!method.ready) {
const auto hasSaved = method.ready;
if (!hasSaved && method.additionalMethods.empty()) {
showEditPaymentMethod(method);
return;
}
showBox(Box([=](not_null<GenericBox*> box) {
const auto save = [=](int option) {
if (option) {
const auto basic = hasSaved ? 1 : 0;
if (option > basic) {
const auto index = option - basic - 1;
Assert(index < method.additionalMethods.size());
showAdditionalMethod(
method.provider,
method.additionalMethods[index]);
} else if (!option) {
showEditPaymentMethod(method);
}
};
auto options = std::vector{
tr::lng_payments_new_card(tr::now),
};
if (hasSaved) {
options.push_back(method.title);
}
for (const auto &additional : method.additionalMethods) {
options.push_back(additional.title);
}
SingleChoiceBox(box, {
.title = tr::lng_payments_payment_method(),
.options = { method.title, tr::lng_payments_new_card(tr::now) },
.initialSelection = 0,
.options = std::move(options),
.initialSelection = hasSaved ? 1 : -1,
.callback = save,
});
}));

View file

@ -36,6 +36,7 @@ class FormSummary;
class EditInformation;
class EditCard;
struct PaymentMethodDetails;
struct PaymentMethodAdditional;
struct NativeMethodDetails;
class Panel final {
@ -61,6 +62,9 @@ public:
const RequestedInformation &current,
InformationField field);
void showEditPaymentMethod(const PaymentMethodDetails &method);
void showAdditionalMethod(
const QString &provider,
const PaymentMethodAdditional &method);
void showEditCard(
const NativeMethodDetails &native,
CardField field);

View file

@ -160,9 +160,15 @@ struct NativeMethodDetails {
bool canSaveInformation = false;
};
struct PaymentMethodAdditional {
QString title;
QString url;
};
struct PaymentMethodDetails {
QString title;
NativeMethodDetails native;
std::vector<PaymentMethodAdditional> additionalMethods;
QString url;
QString provider;
bool ready = false;