BTPaymentFlowDriver.h 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  1. #if __has_include("BraintreeCore.h")
  2. #import "BraintreeCore.h"
  3. #else
  4. #import <BraintreeCore/BraintreeCore.h>
  5. #endif
  6. #import "BTPaymentFlowRequest.h"
  7. #import "BTPaymentFlowResult.h"
  8. #import <Foundation/Foundation.h>
  9. NS_ASSUME_NONNULL_BEGIN
  10. /**
  11. Payment flow error domain
  12. */
  13. extern NSString * const BTPaymentFlowDriverErrorDomain;
  14. /**
  15. Errors associated with payment flows.
  16. */
  17. typedef NS_ENUM(NSInteger, BTPaymentFlowDriverErrorType) {
  18. /// PaymentFlow unknown error.
  19. BTPaymentFlowDriverErrorTypeUnknown = 0,
  20. /// PaymentFlow is disabled in configuration.
  21. BTPaymentFlowDriverErrorTypeDisabled,
  22. /// UIApplication failed to switch to browser.
  23. BTPaymentFlowDriverErrorTypeAppSwitchFailed,
  24. /// Return URL was invalid.
  25. BTPaymentFlowDriverErrorTypeInvalidReturnURL,
  26. /// Braintree SDK is integrated incorrectly.
  27. BTPaymentFlowDriverErrorTypeIntegration,
  28. /// Request URL was invalid, configuration may be missing required values.
  29. BTPaymentFlowDriverErrorTypeInvalidRequestURL,
  30. /// Payment flow was canceled, typically initiated by the user when exiting early from the flow.
  31. BTPaymentFlowDriverErrorTypeCanceled,
  32. };
  33. /**
  34. Protocol for payment flow processing via BTPaymentFlowRequestDelegate.
  35. */
  36. @protocol BTPaymentFlowDriverDelegate
  37. /**
  38. Use when payment URL is ready for processing.
  39. */
  40. - (void)onPaymentWithURL:(NSURL * _Nullable) url error:(NSError * _Nullable)error;
  41. /**
  42. Use when the payment flow was cancelled.
  43. */
  44. - (void)onPaymentCancel;
  45. /**
  46. Use when the payment flow has completed or encountered an error.
  47. @param result The BTPaymentFlowResult of the payment flow.
  48. @param error NSError containing details of the error.
  49. */
  50. - (void)onPaymentComplete:(BTPaymentFlowResult * _Nullable)result error:(NSError * _Nullable)error;
  51. /**
  52. Returns the base return URL scheme used by the driver.
  53. @return A NSString representing the base return URL scheme used by the driver.
  54. */
  55. - (NSString *)returnURLScheme;
  56. /**
  57. Returns the BTAPIClient used by the BTPaymentFlowDriverDelegate.
  58. @return The BTAPIClient used by the driver.
  59. */
  60. - (BTAPIClient *)apiClient;
  61. @end
  62. /**
  63. Protocol for payment flow processing.
  64. */
  65. @protocol BTPaymentFlowRequestDelegate
  66. /**
  67. Handle payment request for a variety of web/app switch flows.
  68. Use the delegate to handle success/error/cancel flows.
  69. @param request A BTPaymentFlowRequest request.
  70. @param delegate The BTPaymentFlowDriverDelegate to handle response.
  71. */
  72. - (void)handleRequest:(BTPaymentFlowRequest *)request client:(BTAPIClient *)apiClient paymentDriverDelegate:(id<BTPaymentFlowDriverDelegate>)delegate;
  73. /**
  74. Check if this BTPaymentFlowRequestDelegate can handle the URL from the source application.
  75. @param url The URL to check.
  76. @param sourceApplication The source application to sent the URL.
  77. @return True if the BTPaymentFlowRequestDelegate can handle the URL. Otherwise return false.
  78. */
  79. - (BOOL)canHandleAppSwitchReturnURL:(NSURL *)url sourceApplication:(NSString *)sourceApplication;
  80. /**
  81. Handles the return URL and completes and post processing.
  82. @param url The URL to check.
  83. */
  84. - (void)handleOpenURL:(NSURL *)url;
  85. /**
  86. A short and unique alphanumeric name for the payment flow.
  87. Used for analytics/events. No spaces and all lowercase.
  88. */
  89. - (NSString *)paymentFlowName;
  90. @end
  91. /**
  92. BTPaymentFlowDriver handles the shared aspects of web/app payment flows.
  93. Handles the app switching and shared logic for payment flows that use web or app switching.
  94. */
  95. @interface BTPaymentFlowDriver : NSObject <BTAppSwitchHandler, BTPaymentFlowDriverDelegate>
  96. /**
  97. Initialize a new BTPaymentFlowDriver instance.
  98. @param apiClient The API client.
  99. */
  100. - (instancetype)initWithAPIClient:(BTAPIClient *)apiClient NS_DESIGNATED_INITIALIZER;
  101. /**
  102. Base initializer - do not use.
  103. */
  104. - (instancetype)init __attribute__((unavailable("Please use initWithAPIClient:")));
  105. /**
  106. Starts a payment flow using a BTPaymentFlowRequest (usually subclassed for specific payment methods).
  107. @param request A BTPaymentFlowRequest request.
  108. @param completionBlock This completion will be invoked exactly once when the payment flow is complete or an error occurs.
  109. */
  110. - (void)startPaymentFlow:(BTPaymentFlowRequest<BTPaymentFlowRequestDelegate> *)request completion:(void (^)( BTPaymentFlowResult * _Nullable result, NSError * _Nullable error))completionBlock;
  111. /**
  112. A required delegate to control the presentation and dismissal of view controllers.
  113. */
  114. @property (nonatomic, weak, nullable) id<BTViewControllerPresentingDelegate> viewControllerPresentingDelegate;
  115. /**
  116. An optional delegate for receiving notifications about the lifecycle of a payment flow app/browser switch, as well as updating your UI
  117. @note BTPaymentFlowDriver will only send notifications for `appContextWillSwitch:` and `appContextDidReturn:`.
  118. */
  119. @property (nonatomic, weak, nullable) id<BTAppSwitchDelegate> appSwitchDelegate;
  120. @end
  121. NS_ASSUME_NONNULL_END