tdesktop/Telegram/SourceFiles/payments/payments_form.h

319 lines
7.3 KiB
C
Raw Normal View History

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
*/
#pragma once
#include "payments/ui/payments_panel_data.h"
#include "base/weak_ptr.h"
#include "mtproto/sender.h"
2021-03-23 12:34:34 +00:00
2021-03-26 13:05:31 +00:00
class Image;
class QJsonObject;
2021-03-26 13:05:31 +00:00
namespace Core {
struct CloudPasswordResult;
} // namespace Core
namespace Stripe {
class APIClient;
} // namespace Stripe
namespace SmartGlocal {
class APIClient;
} // namespace SmartGlocal
2021-03-23 12:34:34 +00:00
namespace Main {
class Session;
} // namespace Main
2021-03-26 13:05:31 +00:00
namespace Data {
class PhotoMedia;
} // namespace Data
2021-03-23 12:34:34 +00:00
namespace Payments {
2021-03-30 08:16:05 +00:00
enum class Mode;
2021-03-23 12:34:34 +00:00
struct FormDetails {
2021-03-30 08:16:05 +00:00
uint64 formId = 0;
2021-03-23 12:34:34 +00:00
QString url;
QString nativeProvider;
QString termsBotUsername;
2021-03-23 12:34:34 +00:00
QByteArray nativeParamsJson;
UserId botId = 0;
UserId providerId = 0;
bool canSaveCredentials = false;
bool passwordMissing = false;
bool termsAccepted = false;
2021-03-23 12:34:34 +00:00
[[nodiscard]] bool valid() const {
return !url.isEmpty();
}
[[nodiscard]] explicit operator bool() const {
return valid();
}
};
2021-03-26 13:05:31 +00:00
struct ThumbnailLoadProcess {
std::shared_ptr<Data::PhotoMedia> view;
bool blurredSet = false;
rpl::lifetime lifetime;
};
struct SavedCredentials {
QString id;
QString title;
[[nodiscard]] bool valid() const {
return !id.isEmpty();
}
[[nodiscard]] explicit operator bool() const {
return valid();
}
};
struct NewCredentials {
QString title;
QByteArray data;
bool saveOnServer = false;
[[nodiscard]] bool empty() const {
return data.isEmpty();
}
[[nodiscard]] explicit operator bool() const {
return !empty();
}
};
struct StripePaymentMethod {
QString publishableKey;
};
struct SmartGlocalPaymentMethod {
QString publicToken;
};
struct NativePaymentMethod {
std::variant<
v::null_t,
StripePaymentMethod,
SmartGlocalPaymentMethod> data;
[[nodiscard]] bool valid() const {
return !v::is_null(data);
}
[[nodiscard]] explicit operator bool() const {
return valid();
}
};
struct PaymentMethod {
NativePaymentMethod native;
std::vector<SavedCredentials> savedCredentials;
int savedCredentialsIndex = 0;
NewCredentials newCredentials;
Ui::PaymentMethodDetails ui;
};
struct ToggleProgress {
bool shown = true;
};
2021-03-23 12:34:34 +00:00
struct FormReady {};
2021-03-26 13:05:31 +00:00
struct ThumbnailUpdated {
QImage thumbnail;
};
struct ValidateFinished {};
struct PaymentMethodUpdate {
bool requestNewPassword = false;
};
struct VerificationNeeded {
QString url;
};
struct TmpPasswordRequired {};
2021-04-02 12:59:54 +00:00
struct BotTrustRequired {
not_null<UserData*> bot;
not_null<UserData*> provider;
};
struct PaymentFinished {
MTPUpdates updates;
};
struct Error {
enum class Type {
2021-03-26 17:09:09 +00:00
None,
Form,
Validate,
Stripe,
SmartGlocal,
TmpPassword,
Send,
};
2021-03-26 17:09:09 +00:00
Type type = Type::None;
QString id;
2021-03-26 17:09:09 +00:00
[[nodiscard]] bool empty() const {
return (type == Type::None);
}
[[nodiscard]] explicit operator bool() const {
return !empty();
}
};
2021-03-23 12:34:34 +00:00
struct FormUpdate : std::variant<
ToggleProgress,
FormReady,
2021-03-26 13:05:31 +00:00
ThumbnailUpdated,
ValidateFinished,
PaymentMethodUpdate,
VerificationNeeded,
TmpPasswordRequired,
2021-04-02 12:59:54 +00:00
BotTrustRequired,
PaymentFinished,
Error> {
using variant::variant;
2021-03-23 12:34:34 +00:00
};
2022-05-05 08:45:29 +00:00
struct InvoiceMessage {
not_null<PeerData*> peer;
MsgId itemId = 0;
};
struct InvoiceSlug {
not_null<Main::Session*> session;
QString slug;
};
struct InvoiceId {
std::variant<InvoiceMessage, InvoiceSlug> value;
};
[[nodiscard]] not_null<Main::Session*> SessionFromId(const InvoiceId &id);
class Form final : public base::has_weak_ptr {
2021-03-23 12:34:34 +00:00
public:
2022-05-05 08:45:29 +00:00
Form(InvoiceId id, bool receipt);
~Form();
2021-03-23 12:34:34 +00:00
[[nodiscard]] const Ui::Invoice &invoice() const {
return _invoice;
}
[[nodiscard]] const FormDetails &details() const {
return _details;
}
2021-04-02 12:10:23 +00:00
[[nodiscard]] const Ui::RequestedInformation &information() const {
return _information;
2021-03-23 12:34:34 +00:00
}
[[nodiscard]] const PaymentMethod &paymentMethod() const {
return _paymentMethod;
2021-03-23 12:34:34 +00:00
}
[[nodiscard]] const Ui::ShippingOptions &shippingOptions() const {
return _shippingOptions;
}
2021-04-02 12:10:23 +00:00
[[nodiscard]] bool hasChanges() const;
2021-03-23 12:34:34 +00:00
[[nodiscard]] rpl::producer<FormUpdate> updates() const {
return _updates.events();
}
void validateInformation(const Ui::RequestedInformation &information);
void validateCard(
const Ui::UncheckedCardDetails &details,
bool saveInformation);
void setPaymentCredentials(const NewCredentials &credentials);
void chooseSavedMethod(const QString &id);
void setHasPassword(bool has);
void setShippingOption(const QString &id);
2021-03-30 14:49:41 +00:00
void setTips(int64 value);
void acceptTerms();
2021-04-02 12:59:54 +00:00
void trustBot();
void submit();
void submit(const Core::CloudPasswordResult &result);
2021-03-23 12:34:34 +00:00
private:
2021-03-26 13:05:31 +00:00
void fillInvoiceFromMessage();
void showProgress();
void hideProgress();
2021-03-26 13:05:31 +00:00
2022-05-05 08:45:29 +00:00
[[nodiscard]] Data::FileOrigin thumbnailFileOrigin() const;
2021-03-26 13:05:31 +00:00
void loadThumbnail(not_null<PhotoData*> photo);
[[nodiscard]] QImage prepareGoodThumbnail(
const std::shared_ptr<Data::PhotoMedia> &view) const;
[[nodiscard]] QImage prepareBlurredThumbnail(
const std::shared_ptr<Data::PhotoMedia> &view) const;
[[nodiscard]] QImage prepareThumbnail(
not_null<const Image*> image,
bool blurred = false) const;
[[nodiscard]] QImage prepareEmptyThumbnail() const;
2021-03-23 12:34:34 +00:00
void requestForm();
2021-03-30 06:01:31 +00:00
void requestReceipt();
2021-03-23 12:34:34 +00:00
void processForm(const MTPDpayments_paymentForm &data);
2021-03-30 06:01:31 +00:00
void processReceipt(const MTPDpayments_paymentReceipt &data);
2021-03-23 12:34:34 +00:00
void processInvoice(const MTPDinvoice &data);
void processDetails(const MTPDpayments_paymentForm &data);
2021-03-30 06:01:31 +00:00
void processDetails(const MTPDpayments_paymentReceipt &data);
2021-03-23 12:34:34 +00:00
void processSavedInformation(const MTPDpaymentRequestedInfo &data);
2022-07-19 11:28:23 +00:00
void processAdditionalPaymentMethods(
const QVector<MTPPaymentFormMethod> &list);
void processShippingOptions(const QVector<MTPShippingOption> &data);
void fillPaymentMethodInformation();
void fillStripeNativeMethod(QJsonObject object);
void fillSmartGlocalNativeMethod(QJsonObject object);
void refreshPaymentMethodDetails();
void refreshSavedPaymentMethodDetails();
[[nodiscard]] QString defaultPhone() const;
2021-03-26 17:09:09 +00:00
[[nodiscard]] QString defaultCountry() const;
2022-05-05 08:45:29 +00:00
[[nodiscard]] MTPInputInvoice inputInvoice() const;
void validateCard(
const StripePaymentMethod &method,
const Ui::UncheckedCardDetails &details,
bool saveInformation);
void validateCard(
const SmartGlocalPaymentMethod &method,
const Ui::UncheckedCardDetails &details,
bool saveInformation);
2021-03-23 12:34:34 +00:00
bool validateInformationLocal(
2021-03-26 17:09:09 +00:00
const Ui::RequestedInformation &information) const;
[[nodiscard]] Error informationErrorLocal(
const Ui::RequestedInformation &information) const;
bool validateCardLocal(
const Ui::UncheckedCardDetails &details) const;
[[nodiscard]] Error cardErrorLocal(
const Ui::UncheckedCardDetails &details) const;
2022-05-05 08:45:29 +00:00
const InvoiceId _id;
2021-03-23 12:34:34 +00:00
const not_null<Main::Session*> _session;
2022-05-05 08:45:29 +00:00
MTP::Sender _api;
2021-03-30 08:16:05 +00:00
bool _receiptMode = false;
2021-03-23 12:34:34 +00:00
Ui::Invoice _invoice;
2021-03-26 13:05:31 +00:00
std::unique_ptr<ThumbnailLoadProcess> _thumbnailLoadProcess;
2021-03-23 12:34:34 +00:00
FormDetails _details;
Ui::RequestedInformation _savedInformation;
2021-04-02 12:10:23 +00:00
Ui::RequestedInformation _information;
PaymentMethod _paymentMethod;
2021-03-23 12:34:34 +00:00
Ui::RequestedInformation _validatedInformation;
mtpRequestId _validateRequestId = 0;
mtpRequestId _passwordRequestId = 0;
std::unique_ptr<Stripe::APIClient> _stripe;
std::unique_ptr<SmartGlocal::APIClient> _smartglocal;
Ui::ShippingOptions _shippingOptions;
QString _requestedInformationId;
2021-03-23 12:34:34 +00:00
rpl::event_stream<FormUpdate> _updates;
};
} // namespace Payments