From 9412846bbdee5b9c46ea9d8065883e2e223130ee Mon Sep 17 00:00:00 2001 From: Ivan Shamatov Date: Sun, 7 Nov 2021 14:38:28 +0300 Subject: [PATCH] Revise confirmations (#16) --- lib/yookassa/entity/confirmation.rb | 65 ++++++++++++++++++++++++++--- lib/yookassa/entity/payment.rb | 2 +- spec/yookassa/payments_spec.rb | 2 +- 3 files changed, 62 insertions(+), 7 deletions(-) diff --git a/lib/yookassa/entity/confirmation.rb b/lib/yookassa/entity/confirmation.rb index 3454dd7..4d498c6 100644 --- a/lib/yookassa/entity/confirmation.rb +++ b/lib/yookassa/entity/confirmation.rb @@ -4,11 +4,66 @@ require_relative "./types" module Yookassa module Entity - class Confirmation < Dry::Struct - attribute :type, Types::String - attribute :confirmation_url, Types::String - attribute? :enforce, Types::Bool - attribute? :return_url, Types::String + class Confirmation + class Base < Dry::Struct + # Language of the interface, emails, and text messages that will be displayed or sent to the user. + # Formatted in accordance with ISO/IEC 15897. Possible values: ru_RU, en_US, de_DE. Case sensitive. + # https://en.wikipedia.org/wiki/Locale_(computer_software) + attribute? :locale, Types::String + end + + class Embedded < Base + attribute :type, Types.Value("embedded") + + # Token for the YooMoney Checkout Widget initialization. + attribute? :confirmation_token, Types::String + end + + class External < Base + attribute :type, Types.Value("external") + end + + class MobileApplication < Base + attribute :type, Types.Value("mobile_application") + + # URL or deep link, to which user will return after confirming or canceling the payment in the app. + # Send the URL if the payment was made via the mobile version of the website or send the deep link + # if the payment was made via mobile app. Maximum 2048 characters. + attribute :return_url, Types::String + + # Deep link to the mobile app where the user confirms the payment. + attribute? :confirmation_url, Types::String + end + + class QrCode < Base + attribute :type, Types.Value("qr") + + # Data for generating the QR code. + attribute? :confirmation_data, Types::String + end + + class Redirect < Base + attribute :type, Types.Value("redirect") + + # The URL that the user will be redirected to for payment confirmation. + attribute? :confirmation_url, Types::String + + # A request for making a payment with authentication by 3-D Secure. It works if you accept + # bank card payments without user confirmation by default. In other cases, the 3-D Secure + # authentication will be handled by YooMoney. If you would like to accept payments without + # additional confirmation by the user, contact your YooMoney manager. + attribute? :enforce, Types::Bool + + # The URL that the user will return to after confirming or canceling the payment on the webpage. + # Maximum 2048 characters. + attribute :return_url, Types::String + end end + + Confirmations = Confirmation::Embedded | + Confirmation::External | + Confirmation::MobileApplication | + Confirmation::QrCode | + Confirmation::Redirect end end diff --git a/lib/yookassa/entity/payment.rb b/lib/yookassa/entity/payment.rb index f64d642..486f025 100644 --- a/lib/yookassa/entity/payment.rb +++ b/lib/yookassa/entity/payment.rb @@ -62,7 +62,7 @@ module Yookassa # confirmation [object, optional] # Selected payment confirmation scenario. For payments requiring confirmation from the user. # More about confirmation scenarios https://yookassa.ru/en/developers/api#:~:text=confirmation,from%20the%20user.%20More%20about%20confirmation%20scenarios%C2%A0 - attribute? :confirmation, Entity::Confirmation + attribute? :confirmation, Entity::Confirmations # test [boolean, required] # The attribute of a test transaction. diff --git a/spec/yookassa/payments_spec.rb b/spec/yookassa/payments_spec.rb index 2286edb..673f132 100644 --- a/spec/yookassa/payments_spec.rb +++ b/spec/yookassa/payments_spec.rb @@ -27,7 +27,7 @@ RSpec.describe Yookassa::Payments do expect(subject.amount.currency).to eq "RUB" expect(subject.amount.value).to eq 10.0 - expect(subject.confirmation).to be_a Yookassa::Entity::Confirmation + expect(subject.confirmation).to be_a Yookassa::Entity::Confirmation::Redirect expect(subject.confirmation.confirmation_url).to eq "https://money.yookassa.ru/payments/external/confirmation?orderId=2490ded1-000f-5000-8000-1f64111bc63e" expect(subject.confirmation.type).to eq "redirect" expect(subject.confirmation.return_url).to eq "https://url.test"