BTPayPalDriver.h 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263
  1. #if __has_include("BraintreeCore.h")
  2. #import "BraintreeCore.h"
  3. #else
  4. #import <BraintreeCore/BraintreeCore.h>
  5. #endif
  6. #import "BTPayPalAccountNonce.h"
  7. #import "BTPayPalRequest.h"
  8. #import <Foundation/Foundation.h>
  9. NS_ASSUME_NONNULL_BEGIN
  10. @class PPOTRequest;
  11. /**
  12. Domain for PayPal errors.
  13. */
  14. extern NSString *const BTPayPalDriverErrorDomain;
  15. /**
  16. Error codes associated with PayPal.
  17. */
  18. typedef NS_ENUM(NSInteger, BTPayPalDriverErrorType) {
  19. /// Unknown error
  20. BTPayPalDriverErrorTypeUnknown = 0,
  21. /// PayPal is disabled in configuration
  22. BTPayPalDriverErrorTypeDisabled,
  23. /// App switch is not configured appropriately. You must specify a
  24. /// valid returnURLScheme via BTAppSwitch before attempting an app switch
  25. BTPayPalDriverErrorTypeIntegrationReturnURLScheme,
  26. /// UIApplication failed to switch despite it being available.
  27. /// `[UIApplication openURL:]` returned `NO` when `YES` was expected
  28. BTPayPalDriverErrorTypeAppSwitchFailed,
  29. /// Invalid configuration, e.g. bad CFBundleDisplayName
  30. BTPayPalDriverErrorTypeInvalidConfiguration,
  31. /// Invalid request, e.g. missing PayPal request
  32. BTPayPalDriverErrorTypeInvalidRequest,
  33. /// Braintree SDK is integrated incorrectly
  34. BTPayPalDriverErrorTypeIntegration,
  35. };
  36. /**
  37. Protocol to handle custom PayPal Approval via BTPayPalApprovalHandler
  38. */
  39. @protocol BTPayPalApprovalDelegate
  40. /**
  41. Use when custom approval has completed with success or error
  42. */
  43. - (void)onApprovalComplete:(NSURL *) url;
  44. /**
  45. Use when custom approval was canceled
  46. */
  47. - (void)onApprovalCancel;
  48. @end
  49. /**
  50. Protocol for custom authentication and authorization of PayPal.
  51. */
  52. @protocol BTPayPalApprovalHandler
  53. /**
  54. Handle approval request for PayPal and carry out custom authentication and authorization.
  55. Use the delegate to handle success/error/cancel flows.
  56. On completion or error use BTPayPalApprovalDelegate:onApprovalComplete
  57. On cancel use BTPayPalApprovalDelegate:onApprovalCancel
  58. @param request PayPal request object.
  59. @param delegate The BTPayPalApprovalDelegate to handle response.
  60. */
  61. - (void)handleApproval:(PPOTRequest*)request paypalApprovalDelegate:(id<BTPayPalApprovalDelegate>)delegate;
  62. @end
  63. /**
  64. BTPayPalDriver enables you to obtain permission to charge your customers' PayPal accounts via app switch to the PayPal app and the browser.
  65. @note To make PayPal available, you must ensure that PayPal is enabled in your Braintree control panel.
  66. See our [online documentation](https://developers.braintreepayments.com/ios+ruby/guides/paypal) for
  67. details.
  68. This class supports two basic use-cases: Vault and Checkout. Each of these involves variations on the
  69. user experience as well as variations on the capabilities granted to you by this authorization.
  70. The *Vault* option uses PayPal's future payments authorization, which allows your merchant account to
  71. charge this customer arbitrary amounts for a long period of time into the future (unless the user
  72. manually revokes this permission in their PayPal control panel.) This authorization flow includes
  73. a screen with legal language that directs the user to agree to the terms of Future Payments.
  74. Unfortunately, it is not currently possible to collect shipping information in the Vault flow.
  75. The *Checkout* option creates a one-time use PayPal payment on your behalf. As a result, you must
  76. specify the checkout details up-front, so that they can be shown to the user during the PayPal flow.
  77. With this flow, you must specify the estimated transaction amount, and you can collect shipping
  78. details. While this flow omits the Future Payments agreement, the resulting payment method cannot be
  79. stored in the vault. It is only possible to create one Braintree transaction with this form of user
  80. approval.
  81. Both of these flows are available to all users on any iOS device. If the PayPal app is installed on the
  82. device, the PayPal login flow will take place there via an app switch. Otherwise, PayPal login takes
  83. place in the Safari browser.
  84. Regardless of the type or target, all of these user experiences take full advantage of One Touch. This
  85. means that users may bypass the username/password entry screen when they are already logged in.
  86. Upon successful completion, you will receive a `BTPayPalAccountNonce`, which includes user-facing
  87. details and a payment method nonce, which you must pass to your server in order to create a transaction
  88. or save the authorization in the Braintree vault (not possible with Checkout).
  89. ## User Experience Details
  90. To keep your UI in sync during app switch authentication, you may set a delegate, which will receive
  91. notifications as the PayPal driver progresses through the various steps necessary for user
  92. authentication.
  93. ## App Switching Details
  94. This class will handle switching out of your app to the PayPal app or the browser (including the call to
  95. `-[UIApplication openURL:]`).
  96. */
  97. @interface BTPayPalDriver : NSObject <BTAppSwitchHandler, BTPayPalApprovalDelegate>
  98. /**
  99. Initialize a new PayPal driver instance.
  100. @param apiClient The API client
  101. */
  102. - (instancetype)initWithAPIClient:(BTAPIClient *)apiClient;
  103. /**
  104. Base initializer - do not use.
  105. */
  106. - (instancetype)init __attribute__((unavailable("Please use initWithAPIClient:")));
  107. /**
  108. Authorize a PayPal user for saving their account in the Vault via app switch to the PayPal App or the browser.
  109. On success, you will receive an instance of `BTPayPalAccountNonce`; on failure, an error; on user cancellation,
  110. you will receive `nil` for both parameters.
  111. @note During the app switch authorization, the user may switch back to your app manually. In this case, the caller
  112. will not receive a cancellation via the completionBlock. Rather, it is the caller's responsibility to observe
  113. `UIApplicationDidBecomeActiveNotification` and `UIApplicationWillResignActiveNotification` using `NSNotificationCenter`
  114. if necessary.
  115. @param completionBlock This completion will be invoked exactly once when authorization is complete or an error occurs.
  116. */
  117. - (void)authorizeAccountWithCompletion:(void (^)(BTPayPalAccountNonce * _Nullable tokenizedPayPalAccount, NSError * _Nullable error))completionBlock;
  118. /**
  119. Authorize a PayPal user for saving their account in the Vault via app switch to the PayPal App or the browser with additional scopes (e.g. address).
  120. On success, you will receive an instance of `BTPayPalAccountNonce`; on failure, an error; on user cancellation,
  121. you will receive `nil` for both parameters.
  122. @note During the app switch authorization, the user may switch back to your app manually. In this case, the caller
  123. will not receive a cancellation via the completionBlock. Rather, it is the caller's responsibility to observe
  124. `UIApplicationDidBecomeActiveNotification` and `UIApplicationWillResignActiveNotification` using `NSNotificationCenter`
  125. if necessary.
  126. @param additionalScopes An `NSSet` of requested scope-values as `NSString`s. Available scope-values are listed at
  127. https://developer.paypal.com/webapps/developer/docs/integration/direct/identity/attributes/
  128. @param completionBlock This completion will be invoked exactly once when authorization is complete or an error occurs.
  129. */
  130. - (void)authorizeAccountWithAdditionalScopes:(NSSet<NSString *> *)additionalScopes
  131. completion:(void (^)(BTPayPalAccountNonce * _Nullable tokenizedPayPalAccount, NSError * _Nullable error))completionBlock;
  132. /**
  133. Check out with PayPal to create a single-use PayPal payment method nonce.
  134. @note You can use this as the final step in your order/checkout flow. If you want, you may create a transaction from your
  135. server when this method completes without any additional user interaction.
  136. On success, you will receive an instance of `BTPayPalAccountNonce`; on failure, an error; on user cancellation,
  137. you will receive `nil` for both parameters.
  138. @note This method is mutually exclusive with `authorizeAccountWithCompletion:`. In both cases, you need to create a
  139. Braintree transaction from your server in order to actually move money!
  140. @param request A PayPal request
  141. @param completionBlock This completion will be invoked exactly once when checkout is complete or an error occurs.
  142. */
  143. - (void)requestOneTimePayment:(BTPayPalRequest *)request
  144. completion:(void (^)(BTPayPalAccountNonce * _Nullable tokenizedPayPalAccount, NSError * _Nullable error))completionBlock;
  145. /**
  146. Check out with PayPal to create a single-use PayPal payment method nonce.
  147. @note You can use this as the final step in your order/checkout flow. If you want, you may create a transaction from your
  148. server when this method completes without any additional user interaction.
  149. On success, you will receive an instance of `BTPayPalAccountNonce`; on failure, an error; on user cancellation,
  150. you will receive `nil` for both parameters.
  151. @note This method is mutually exclusive with `authorizeAccountWithCompletion:`. In both cases, you need to create a
  152. Braintree transaction from your server in order to actually move money!
  153. @param request A PayPal request
  154. @param handler A BTPayPalApprovalHandler for custom authorizatin and approval
  155. @param completionBlock This completion will be invoked exactly once when checkout is complete or an error occurs.
  156. */
  157. - (void)requestOneTimePayment:(BTPayPalRequest *)request handler:(id<BTPayPalApprovalHandler>)handler
  158. completion:(void (^)(BTPayPalAccountNonce * _Nullable tokenizedPayPalAccount, NSError * _Nullable error))completionBlock;
  159. /**
  160. Create a PayPal Billing Agreement for repeat purchases.
  161. @note You can use this as the final step in your order/checkout flow. If you want, you may create a transaction from your
  162. server when this method completes without any additional user interaction.
  163. On success, you will receive an instance of `BTPayPalAccountNonce`; on failure, an error; on user cancellation,
  164. you will receive `nil` for both parameters.
  165. @note This method is mutually exclusive with `authorizeAccountWithCompletion:`. In both cases, you need to create a
  166. Braintree transaction from your server in order to actually move money!
  167. @param request A PayPal request
  168. @param completionBlock This completion will be invoked exactly once when checkout is complete or an error occurs.
  169. */
  170. - (void)requestBillingAgreement:(BTPayPalRequest *)request
  171. completion:(void (^)(BTPayPalAccountNonce * _Nullable tokenizedPayPalAccount, NSError * _Nullable error))completionBlock;
  172. /**
  173. Create a PayPal Billing Agreement for repeat purchases.
  174. @note You can use this as the final step in your order/checkout flow. If you want, you may create a transaction from your
  175. server when this method completes without any additional user interaction.
  176. On success, you will receive an instance of `BTPayPalAccountNonce`; on failure, an error; on user cancellation,
  177. you will receive `nil` for both parameters.
  178. @note This method is mutually exclusive with `authorizeAccountWithCompletion:`. In both cases, you need to create a
  179. Braintree transaction from your server in order to actually move money!
  180. @param request A PayPal request
  181. @param handler A BTPayPalApprovalHandler for custom authorization and approval
  182. @param completionBlock This completion will be invoked exactly once when checkout is complete or an error occurs.
  183. */
  184. - (void)requestBillingAgreement:(BTPayPalRequest *)request handler:(id<BTPayPalApprovalHandler>)handler
  185. completion:(void (^)(BTPayPalAccountNonce * _Nullable tokenizedPayPalAccount, NSError * _Nullable error))completionBlock;
  186. #pragma mark - Delegate
  187. /**
  188. An optional delegate for receiving notifications about the lifecycle of a PayPal app switch for updating your UI
  189. */
  190. @property (nonatomic, weak, nullable) id<BTAppSwitchDelegate> appSwitchDelegate;
  191. /**
  192. A required delegate to control the presentation and dismissal of view controllers
  193. */
  194. @property (nonatomic, weak, nullable) id<BTViewControllerPresentingDelegate> viewControllerPresentingDelegate;
  195. @end
  196. NS_ASSUME_NONNULL_END