BTAPIClient.h 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  1. #import <Foundation/Foundation.h>
  2. #import "BTClientMetadata.h"
  3. #import "BTConfiguration.h"
  4. #import "BTJSON.h"
  5. @class BTPaymentMethodNonce;
  6. NS_ASSUME_NONNULL_BEGIN
  7. /**
  8. Domain for API client errors.
  9. */
  10. extern NSString *const BTAPIClientErrorDomain;
  11. /**
  12. Error codes associated with API client.
  13. */
  14. typedef NS_ENUM(NSInteger, BTAPIClientErrorType) {
  15. /// Unknown error
  16. BTAPIClientErrorTypeUnknown = 0,
  17. /// Configuration fetch failed
  18. BTAPIClientErrorTypeConfigurationUnavailable,
  19. /// The authorization provided to the API client is insufficient
  20. BTAPIClientErrorTypeNotAuthorized,
  21. };
  22. /**
  23. This class acts as the entry point for accessing the Braintree APIs via common HTTP methods performed on API endpoints.
  24. @note It also manages authentication via tokenization key and provides access to a merchant's gateway configuration.
  25. */
  26. @interface BTAPIClient : NSObject
  27. /**
  28. Initialize a new API client.
  29. @param authorization Your tokenization key, client token, or PayPal ID Token. Passing an invalid value may return `nil`.
  30. @return A Braintree API client, or `nil` if initialization failed.
  31. */
  32. - (nullable instancetype)initWithAuthorization:(NSString *)authorization;
  33. /**
  34. Create a copy of an existing API client, but specify a new source and integration type.
  35. This provides a way to override an API client's source and integration metadata, which
  36. is captured and sent to Braintree as part of the analytics we track.
  37. */
  38. - (instancetype)copyWithSource:(BTClientMetadataSourceType)source
  39. integration:(BTClientMetadataIntegrationType)integration;
  40. /**
  41. Provides configuration data as a `BTJSON` object.
  42. The configuration data can be used by supported payment options to configure themselves
  43. dynamically through the Control Panel. It also contains configuration options for the
  44. Braintree SDK Core components.
  45. @note This method is asynchronous because it requires a network call to fetch the
  46. configuration for a merchant account from Braintree servers. This configuration is
  47. cached on subsequent calls for better performance.
  48. */
  49. - (void)fetchOrReturnRemoteConfiguration:(void (^)(BTConfiguration * _Nullable configuration, NSError * _Nullable error))completionBlock;
  50. /**
  51. Fetches a customer's vaulted payment method nonces.
  52. Must be using client token with a customer ID specified.
  53. @param completion Callback that returns an array of payment method nonces.
  54. On success, `paymentMethodNonces` contains the nonces and `error` is `nil`. The default payment method nonce, if one exists, will be first.
  55. On failure, `error` contains the error that occured and `paymentMethodNonces` is `nil`.
  56. */
  57. - (void)fetchPaymentMethodNonces:(void(^)(NSArray <BTPaymentMethodNonce *> * _Nullable paymentMethodNonces, NSError * _Nullable error))completion;
  58. /**
  59. Fetches a customer's vaulted payment method nonces.
  60. Must be using client token with a customer ID specified.
  61. @param defaultFirst Specifies whether to sorts the fetched payment method nonces with the default payment method or the most recently used payment method first
  62. @param completion Callback that returns an array of payment method nonces
  63. */
  64. - (void)fetchPaymentMethodNonces:(BOOL)defaultFirst
  65. completion:(void(^)(NSArray <BTPaymentMethodNonce *> * _Nullable paymentMethodNonces, NSError * _Nullable error))completion;
  66. /**
  67. Perfom an HTTP GET on a URL composed of the configured from environment and the given path.
  68. @param path The endpoint URI path.
  69. @param parameters Optional set of query parameters to be encoded with the request.
  70. @param completionBlock A block object to be executed when the request finishes.
  71. On success, `body` and `response` will contain the JSON body response and the
  72. HTTP response and `error` will be `nil`; on failure, `body` and `response` will be
  73. `nil` and `error` will contain the error that occurred.
  74. */
  75. - (void)GET:(NSString *)path
  76. parameters:(nullable NSDictionary <NSString *, NSString *> *)parameters
  77. completion:(nullable void(^)(BTJSON * _Nullable body, NSHTTPURLResponse * _Nullable response, NSError * _Nullable error))completionBlock;
  78. /**
  79. Perfom an HTTP POST on a URL composed of the configured from environment and the given path.
  80. @param path The endpoint URI path.
  81. @param parameters Optional set of parameters to be JSON encoded and sent in the body of the request.
  82. @param completionBlock A block object to be executed when the request finishes.
  83. On success, `body` and `response` will contain the JSON body response and the
  84. HTTP response and `error` will be `nil`; on failure, `body` and `response` will be
  85. `nil` and `error` will contain the error that occurred.
  86. */
  87. - (void)POST:(NSString *)path
  88. parameters:(nullable NSDictionary *)parameters
  89. completion:(nullable void(^)(BTJSON * _Nullable body, NSHTTPURLResponse * _Nullable response, NSError * _Nullable error))completionBlock;
  90. /**
  91. Base initializer - do not use.
  92. */
  93. - (instancetype)init __attribute__((unavailable("Use initWithAuthorization: instead.")));
  94. @end
  95. NS_ASSUME_NONNULL_END