app/models/workarea/payment/authorize/afterpay.rb in workarea-afterpay-2.0.2 vs app/models/workarea/payment/authorize/afterpay.rb in workarea-afterpay-2.1.0
- old
+ new
@@ -1,7 +1,60 @@
module Workarea
class Payment
module Authorize
- Afterpay = Capture::Afterpay
+ class Afterpay
+ include OperationImplementation
+ include CreditCardOperation
+ include AfterpayPaymentGateway
+
+ def complete!
+ response = authorize
+ if response.success?
+ transaction.response = ActiveMerchant::Billing::Response.new(
+ true,
+ I18n.t(
+ 'workarea.afterpay.authorize',
+ amount: transaction.amount
+ ),
+ response.body
+ )
+ else
+ transaction.response = ActiveMerchant::Billing::Response.new(
+ false,
+ I18n.t('workarea.afterpay.capture_failure'),
+ response.body
+ )
+ end
+ end
+
+ def cancel!
+ return unless transaction.success?
+
+ payment_id = transaction.response.params["id"]
+ response = gateway.void(payment_id)
+
+ transaction.cancellation = ActiveMerchant::Billing::Response.new(
+ true,
+ I18n.t('workarea.afterpay.void'),
+ response.body
+ )
+ end
+
+ private
+
+ def authorize
+ request_id = SecureRandom.uuid
+ auth_response = response(request_id)
+ if Workarea::Afterpay::RETRY_ERROR_STATUSES.include? auth_response.status
+ return response(request_id)
+ end
+
+ auth_response
+ end
+
+ def response(request_id)
+ gateway.authorize(tender.token, tender.payment.id, request_id)
+ end
+ end
end
end
end