-
Notifications
You must be signed in to change notification settings - Fork 294
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Support
Encodable
POST types (#1151)
- Loading branch information
Showing
10 changed files
with
223 additions
and
44 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
25 changes: 25 additions & 0 deletions
25
Sources/BraintreeApplePay/BTApplePaymentTokensRequest.swift
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
import Foundation | ||
import PassKit | ||
|
||
/// The POST body for `v1/payment_methods/apple_payment_tokens` | ||
struct BTApplePaymentTokensRequest: Encodable { | ||
|
||
private let applePaymentToken: ApplePaymentToken | ||
|
||
init(token: PKPaymentToken) { | ||
self.applePaymentToken = ApplePaymentToken( | ||
paymentData: token.paymentData.base64EncodedString(), | ||
transactionIdentifier: token.transactionIdentifier, | ||
paymentInstrumentName: token.paymentMethod.displayName, | ||
paymentNetwork: token.paymentMethod.network?.rawValue | ||
) | ||
} | ||
|
||
private struct ApplePaymentToken: Encodable { | ||
|
||
let paymentData: String | ||
let transactionIdentifier: String | ||
let paymentInstrumentName: String? | ||
let paymentNetwork: String? | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
import Foundation | ||
|
||
/// An `Encodable` type containing POST body details & metadata params formatted for the BT Gateway & BT GraphQL API | ||
struct BTAPIRequest: Encodable { | ||
|
||
private let requestBody: Encodable | ||
private let metadata: BTClientMetadata | ||
private let httpType: BTAPIClientHTTPService | ||
|
||
private enum MetadataKeys: String, CodingKey { | ||
case gatewayMetadataKey = "_meta" | ||
case graphQLMetadataKey = "clientSdkMetadata" | ||
} | ||
|
||
/// Initialize a `BTAPIRequest` to format a POST body with metadata params for BT APIs. | ||
/// - Parameters: | ||
/// - requestBody: The actual POST body details. | ||
/// - metadata: The metadata details to append into the POST body. | ||
/// - httpType: The Braintree API type for this request. | ||
init(requestBody: Encodable, metadata: BTClientMetadata, httpType: BTAPIClientHTTPService) { | ||
self.requestBody = requestBody | ||
self.metadata = metadata | ||
self.httpType = httpType | ||
} | ||
|
||
func encode(to encoder: Encoder) throws { | ||
try requestBody.encode(to: encoder) | ||
|
||
var metadataContainer = encoder.container(keyedBy: MetadataKeys.self) | ||
switch httpType { | ||
case .gateway: | ||
let metadataEncoder = metadataContainer.superEncoder(forKey: .gatewayMetadataKey) | ||
try self.metadata.encode(to: metadataEncoder) | ||
case .graphQLAPI: | ||
let metadataEncoder = metadataContainer.superEncoder(forKey: .graphQLMetadataKey) | ||
try self.metadata.encode(to: metadataEncoder) | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.