payments_checkout_process.h 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205
  1. /*
  2. This file is part of Telegram Desktop,
  3. the official desktop application for the Telegram messaging service.
  4. For license and copyright information please follow this link:
  5. https://github.com/telegramdesktop/tdesktop/blob/master/LEGAL
  6. */
  7. #pragma once
  8. #include "base/weak_ptr.h"
  9. #include "payments/ui/payments_panel_delegate.h"
  10. #include "webview/webview_common.h"
  11. class HistoryItem;
  12. class PasscodeBox;
  13. namespace Core {
  14. struct CloudPasswordState;
  15. } // namespace Core
  16. namespace Main {
  17. class Session;
  18. } // namespace Main
  19. namespace Ui {
  20. class GenericBox;
  21. } // namespace Ui
  22. namespace Payments::Ui {
  23. class Panel;
  24. enum class InformationField;
  25. enum class CardField;
  26. } // namespace Payments::Ui
  27. namespace Payments {
  28. class Form;
  29. struct FormUpdate;
  30. struct Error;
  31. struct InvoiceCredits;
  32. struct InvoiceStarGift;
  33. struct InvoiceId;
  34. struct InvoicePremiumGiftCode;
  35. struct CreditsFormData;
  36. struct CreditsReceiptData;
  37. enum class Mode {
  38. Payment,
  39. Receipt,
  40. };
  41. enum class CheckoutResult {
  42. Paid,
  43. Pending,
  44. Cancelled,
  45. Failed,
  46. };
  47. struct RealFormPresentedNotification {
  48. };
  49. struct NonPanelPaymentForm
  50. : std::variant<
  51. std::shared_ptr<CreditsFormData>,
  52. std::shared_ptr<CreditsReceiptData>,
  53. RealFormPresentedNotification> {
  54. using variant::variant;
  55. };
  56. struct PaidInvoice {
  57. QString title;
  58. };
  59. class CheckoutProcess final
  60. : public base::has_weak_ptr
  61. , private Ui::PanelDelegate {
  62. struct PrivateTag {};
  63. public:
  64. static void Start(
  65. not_null<const HistoryItem*> item,
  66. Mode mode,
  67. Fn<void(CheckoutResult)> reactivate,
  68. Fn<void(NonPanelPaymentForm)> nonPanelPaymentFormProcess);
  69. static void Start(
  70. not_null<Main::Session*> session,
  71. const QString &slug,
  72. Fn<void(CheckoutResult)> reactivate,
  73. Fn<void(NonPanelPaymentForm)> nonPanelPaymentFormProcess);
  74. static void Start(
  75. InvoicePremiumGiftCode giftCodeInvoice,
  76. Fn<void(CheckoutResult)> reactivate,
  77. Fn<void(NonPanelPaymentForm)> nonPanelPaymentFormProcess = nullptr);
  78. static void Start(
  79. InvoiceCredits creditsInvoice,
  80. Fn<void(CheckoutResult)> reactivate);
  81. static void Start(
  82. InvoiceStarGift giftInvoice,
  83. Fn<void(CheckoutResult)> reactivate,
  84. Fn<void(NonPanelPaymentForm)> nonPanelPaymentFormProcess);
  85. [[nodiscard]] static std::optional<PaidInvoice> InvoicePaid(
  86. not_null<const HistoryItem*> item);
  87. [[nodiscard]] static std::optional<PaidInvoice> InvoicePaid(
  88. not_null<Main::Session*> session,
  89. const QString &slug);
  90. static void ClearAll();
  91. CheckoutProcess(
  92. InvoiceId id,
  93. Mode mode,
  94. Fn<void(CheckoutResult)> reactivate,
  95. Fn<void(NonPanelPaymentForm)> nonPanelPaymentFormProcess,
  96. PrivateTag);
  97. ~CheckoutProcess();
  98. private:
  99. enum class SubmitState {
  100. None,
  101. Validating,
  102. Validated,
  103. Finishing,
  104. };
  105. [[nodiscard]] not_null<PanelDelegate*> panelDelegate();
  106. static void RegisterPaymentStart(
  107. not_null<CheckoutProcess*> process,
  108. PaidInvoice info);
  109. static void UnregisterPaymentStart(not_null<CheckoutProcess*> process);
  110. void setReactivateCallback(Fn<void(CheckoutResult)> reactivate);
  111. void setNonPanelPaymentFormProcess(Fn<void(NonPanelPaymentForm)>);
  112. void requestActivate();
  113. void closeAndReactivate(CheckoutResult result);
  114. void close();
  115. void handleFormUpdate(const FormUpdate &update);
  116. void handleError(const Error &error);
  117. void showForm();
  118. void showEditInformation(Ui::InformationField field);
  119. void showInformationError(Ui::InformationField field);
  120. void showCardError(Ui::CardField field);
  121. void chooseShippingOption();
  122. void chooseTips();
  123. void editPaymentMethod();
  124. void requestSetPassword();
  125. void requestPassword();
  126. void getPasswordState(
  127. Fn<void(const Core::CloudPasswordState&)> callback);
  128. void performInitialSilentValidation();
  129. void panelRequestClose() override;
  130. void panelCloseSure() override;
  131. void panelSubmit() override;
  132. void panelTrustAndSubmit() override;
  133. void panelAcceptTermsAndSubmit() override;
  134. void panelWebviewMessage(
  135. const QJsonDocument &message,
  136. bool saveInformation) override;
  137. bool panelWebviewNavigationAttempt(const QString &uri) override;
  138. void panelSetPassword() override;
  139. void panelOpenUrl(const QString &url) override;
  140. void panelCancelEdit() override;
  141. void panelEditPaymentMethod() override;
  142. void panelEditShippingInformation() override;
  143. void panelEditName() override;
  144. void panelEditEmail() override;
  145. void panelEditPhone() override;
  146. void panelChooseShippingOption() override;
  147. void panelChangeShippingOption(const QString &id) override;
  148. void panelChooseTips() override;
  149. void panelChangeTips(int64 value) override;
  150. void panelValidateInformation(Ui::RequestedInformation data) override;
  151. void panelValidateCard(
  152. Ui::UncheckedCardDetails data,
  153. bool saveInformation) override;
  154. void panelShowBox(object_ptr<Ui::BoxContent> box) override;
  155. QVariant panelClickHandlerContext() override;
  156. Webview::StorageId panelWebviewStorageId() override;
  157. Webview::ThemeParams panelWebviewThemeParams() override;
  158. std::optional<QDate> panelOverrideExpireDateThreshold() override;
  159. const not_null<Main::Session*> _session;
  160. const std::unique_ptr<Form> _form;
  161. const std::unique_ptr<Ui::Panel> _panel;
  162. QPointer<PasscodeBox> _enterPasswordBox;
  163. Fn<void(CheckoutResult)> _reactivate;
  164. Fn<void(NonPanelPaymentForm)> _nonPanelPaymentFormProcess;
  165. SubmitState _submitState = SubmitState::None;
  166. bool _initialSilentValidation = false;
  167. bool _realFormNotified = false;
  168. bool _sendFormPending = false;
  169. bool _sendFormFailed = false;
  170. rpl::lifetime _gettingPasswordState;
  171. rpl::lifetime _lifetime;
  172. };
  173. } // namespace Payments