Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update SEPA Requests to Encodable #1173

Merged
merged 6 commits into from
Jan 17, 2024
Merged
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions Braintree.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -290,6 +290,8 @@
BEED3B8C291C56B900BA114B /* BraintreeCore.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = 570B93AC285397520041BAFE /* BraintreeCore.framework */; };
BEF1089029019CAE003B2FDA /* BTAnalyticsService.swift in Sources */ = {isa = PBXBuildFile; fileRef = BEE2E4E329007FF100C03FDD /* BTAnalyticsService.swift */; };
BEF1089129019DB9003B2FDA /* BTAPIClient.swift in Sources */ = {isa = PBXBuildFile; fileRef = 570B93AF2853980C0041BAFE /* BTAPIClient.swift */; };
BEF177A62B51C7ED004F0F35 /* SEPADebitAccountsRequest.swift in Sources */ = {isa = PBXBuildFile; fileRef = BEF177A52B51C7ED004F0F35 /* SEPADebitAccountsRequest.swift */; };
BEF177A82B51D6A7004F0F35 /* SEPADebitRequest.swift in Sources */ = {isa = PBXBuildFile; fileRef = BEF177A72B51D6A7004F0F35 /* SEPADebitRequest.swift */; };
BEF3F17E27CE9EDF00072467 /* BTSEPADirectDebitClient.swift in Sources */ = {isa = PBXBuildFile; fileRef = BEF3F17127CE9E6C00072467 /* BTSEPADirectDebitClient.swift */; };
BEF5D2E6294A18B300FFD56D /* BTPayPalLineItem.swift in Sources */ = {isa = PBXBuildFile; fileRef = BEF5D2E5294A18B300FFD56D /* BTPayPalLineItem.swift */; };
BEFD1AF4284F92130070076E /* PPRiskMagnes.xcframework in Frameworks */ = {isa = PBXBuildFile; fileRef = 9C36BD4226B30AA600F0A559 /* PPRiskMagnes.xcframework */; };
Expand Down Expand Up @@ -902,6 +904,8 @@
BEE2E4E5290080BD00C03FDD /* BTAnalyticsServiceError.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = BTAnalyticsServiceError.swift; sourceTree = "<group>"; };
BEEB56582AE9B2A30029F264 /* BTAPIClient_IntegrationTests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = BTAPIClient_IntegrationTests.swift; sourceTree = "<group>"; };
BEEB565A2AE9B3030029F264 /* BTIntegrationTestsConstants.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = BTIntegrationTestsConstants.swift; sourceTree = "<group>"; };
BEF177A52B51C7ED004F0F35 /* SEPADebitAccountsRequest.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = SEPADebitAccountsRequest.swift; sourceTree = "<group>"; };
BEF177A72B51D6A7004F0F35 /* SEPADebitRequest.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = SEPADebitRequest.swift; sourceTree = "<group>"; };
BEF3F17127CE9E6C00072467 /* BTSEPADirectDebitClient.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = BTSEPADirectDebitClient.swift; sourceTree = "<group>"; };
BEF3F17C27CE9EC600072467 /* BraintreeSEPADirectDebit.framework */ = {isa = PBXFileReference; explicitFileType = wrapper.framework; includeInIndex = 0; path = BraintreeSEPADirectDebit.framework; sourceTree = BUILT_PRODUCTS_DIR; };
BEF5D2E5294A18B300FFD56D /* BTPayPalLineItem.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = BTPayPalLineItem.swift; sourceTree = "<group>"; };
Expand Down Expand Up @@ -1729,6 +1733,8 @@
BE642D8727D0094600694A5B /* BTSEPADirectDebitNonce.swift */,
BE642D8527D0047800694A5B /* BTSEPADirectDebitRequest.swift */,
BE0B163C27E0CC7400868C77 /* CreateMandateResult.swift */,
BEF177A52B51C7ED004F0F35 /* SEPADebitAccountsRequest.swift */,
BEF177A72B51D6A7004F0F35 /* SEPADebitRequest.swift */,
BE0B163E27E0CD7C00868C77 /* SEPADirectDebitAPI.swift */,
);
path = BraintreeSEPADirectDebit;
Expand Down Expand Up @@ -3097,10 +3103,12 @@
isa = PBXSourcesBuildPhase;
buildActionMask = 2147483647;
files = (
BEF177A82B51D6A7004F0F35 /* SEPADebitRequest.swift in Sources */,
BE642D8827D0094600694A5B /* BTSEPADirectDebitNonce.swift in Sources */,
BE642D8A27D00F1300694A5B /* BTSEPADirectDebitMandateType.swift in Sources */,
3BC0E09229E4673C009217D6 /* BTSEPADirectAnalytics.swift in Sources */,
BE642D8627D0047800694A5B /* BTSEPADirectDebitRequest.swift in Sources */,
BEF177A62B51C7ED004F0F35 /* SEPADebitAccountsRequest.swift in Sources */,
BE0B163D27E0CC7400868C77 /* CreateMandateResult.swift in Sources */,
BEF3F17E27CE9EDF00072467 /* BTSEPADirectDebitClient.swift in Sources */,
BE0B163F27E0CD7C00868C77 /* SEPADirectDebitAPI.swift in Sources */,
Expand Down
35 changes: 35 additions & 0 deletions Sources/BraintreeSEPADirectDebit/SEPADebitAccountsRequest.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
import Foundation

/// The POST body for `v1/payment_methods/sepa_debit_accounts`
struct SEPADebitAccountsRequest: Encodable {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

❓ - do we want to create a /Models directory within each Sources/<PaymentMethod> directory? Specifically to house these new classes specific to encoding (and maybe eventually to house ones specific to decoding if we go that route).

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, I think that would be helpful. I was thinking that as I was adding this. Should we do that as a follow up PR for all of the models we've created so far? I think it changes the source paths for the package managers (at least Cocoapods). Also down to do that for SEPA in this PR and just a follow up for the other 2. Thoughts?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I like the idea of having those be 1 PR! But your call

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Cool - agree we can 🏈 on it in this PR to establish a pattern in a single PR moving forward


private let createMandateResult: SEPADebitAccountRequest

enum CodingKeys: String, CodingKey {
case createMandateResult = "sepa_debit_account"
}

init(createMandateResult: CreateMandateResult) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nit - I would move the init to the bottom of this class.

Maybe these new Encodable structs is a good candidate for us to add to our style guide how we should organize these, MARKs, etc

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I had followed the pattern we established in BTApplePaymentTokensRequest where the nested struct is under the init. Though I do think having structs within structs could be making our preferred style more convoluted. Thoughts on removing the nesting?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeh, looking back I think having all the struct definitions be near the property that uses it & pushing the init down is more readable vs having them intertwined. What do you think?

In terms of nested structs, I think they sometimes help readability to know when something is defined within the context of something else. For example with the ApplePaymentToken one, that name is pretty generic, but namespacing it the <Request>.ApplePaymentToken helps reduce name collisions.

Open to folks thoughts though!

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeh, looking back I think having all the struct definitions be near the property that uses it & pushing the init down is more readable vs having them intertwined. What do you think?

Yeah I am down if we feel like it makes things more readable! I pushed a commit here with the change: d479ab9

Also agree on style guiding whatever we decide (and finding a home for the style guide).

In terms of nested structs, I think they sometimes help readability to know when something is defined within the context of something else. For example with the ApplePaymentToken one, that name is pretty generic, but namespacing it the .ApplePaymentToken helps reduce name collisions.

That makes sense with naming collisions, I forgot about that aspect. Especially with something like card that could be a situation where a merchant has something similar by the same name.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I added a backlog ticket for updating our style guide - DTBTSDK-3373

self.createMandateResult = SEPADebitAccountRequest(
last4: createMandateResult.ibanLastFour,
merchantOrPartnerCustomerID: createMandateResult.customerID,
bankReferenceToken: createMandateResult.bankReferenceToken,
mandateType: createMandateResult.mandateType
)
}

struct SEPADebitAccountRequest: Encodable {

let last4: String?
let merchantOrPartnerCustomerID: String?
let bankReferenceToken: String?
let mandateType: String?
Comment on lines +14 to +17
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit(entirely optional): maybe we can add short in-line docs to describe each property within these structs


enum CodingKeys: String, CodingKey {
case last4 = "last_4"
case merchantOrPartnerCustomerID = "merchant_or_partner_customer_id"
case bankReferenceToken = "bank_reference_token"
case mandateType = "mandate_type"
}
}
}
83 changes: 83 additions & 0 deletions Sources/BraintreeSEPADirectDebit/SEPADebitRequest.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
import Foundation

#if canImport(BraintreeCore)
import BraintreeCore
#endif

/// The POST body for `v1/sepa_debit`
struct SEPADebitRequest: Encodable {

let merchantAccountID: String?
let cancelURL: String?
let returnURL: String?
let locale: String?

private let sepaAccountRequest: SEPAAccountRequest

enum CodingKeys: String, CodingKey {
case sepaAccountRequest = "sepa_debit"
case merchantAccountID = "merchant_account_id"
case cancelURL = "cancel_url"
case returnURL = "return_url"
case locale = "locale"
}

init(sepaDirectDebitRequest: BTSEPADirectDebitRequest) {
self.sepaAccountRequest = SEPAAccountRequest(sepaDirectDebitRequest: sepaDirectDebitRequest)
self.merchantAccountID = sepaDirectDebitRequest.merchantAccountID
self.cancelURL = BTCoreConstants.callbackURLScheme + "://sepa/cancel"
self.returnURL = BTCoreConstants.callbackURLScheme + "://sepa/success"
self.locale = sepaDirectDebitRequest.locale
}

struct SEPAAccountRequest: Encodable {

let merchantOrPartnerCustomerID: String?
let mandateType: String?
let accountHolderName: String?
let iban: String?
let billingAddress: BillingAddress?

enum CodingKeys: String, CodingKey {
case merchantOrPartnerCustomerID = "merchant_or_partner_customer_id"
case mandateType = "mandate_type"
case accountHolderName = "account_holder_name"
case iban = "iban"
case billingAddress = "billing_address"
}

init(sepaDirectDebitRequest: BTSEPADirectDebitRequest) {
self.merchantOrPartnerCustomerID = sepaDirectDebitRequest.customerID
self.mandateType = sepaDirectDebitRequest.mandateType?.description
self.accountHolderName = sepaDirectDebitRequest.accountHolderName
self.iban = sepaDirectDebitRequest.iban
self.billingAddress = BillingAddress(
streetAddress: sepaDirectDebitRequest.billingAddress?.streetAddress,
extendedAddress: sepaDirectDebitRequest.billingAddress?.extendedAddress,
locality: sepaDirectDebitRequest.billingAddress?.locality,
region: sepaDirectDebitRequest.billingAddress?.region,
postalCode: sepaDirectDebitRequest.billingAddress?.postalCode,
countryCodeAlpha2: sepaDirectDebitRequest.billingAddress?.countryCodeAlpha2
)
}

struct BillingAddress: Encodable {

let streetAddress: String?
let extendedAddress: String?
let locality: String?
let region: String?
let postalCode: String?
let countryCodeAlpha2: String?

enum CodingKeys: String, CodingKey {
case streetAddress = "address_line_1"
case extendedAddress = "address_line_2"
case locality = "admin_area_1"
case region = "admin_area_2"
case postalCode = "postal_code"
case countryCodeAlpha2 = "country_code"
}
}
}
}
42 changes: 4 additions & 38 deletions Sources/BraintreeSEPADirectDebit/SEPADirectDebitAPI.swift
Original file line number Diff line number Diff line change
Expand Up @@ -17,34 +17,8 @@ class SEPADirectDebitAPI {
sepaDirectDebitRequest: BTSEPADirectDebitRequest,
completion: @escaping (CreateMandateResult?, Error?) -> Void
) {
let billingAddress = sepaDirectDebitRequest.billingAddress

let billingAddressDictionary: [String: String?] = [
"address_line_1": billingAddress?.streetAddress,
"address_line_2": billingAddress?.extendedAddress,
"admin_area_1": billingAddress?.locality,
"admin_area_2": billingAddress?.region,
"postal_code": billingAddress?.postalCode,
"country_code": billingAddress?.countryCodeAlpha2
]

let sepaDebitDictionary: [String: Any] = [
"merchant_or_partner_customer_id": sepaDirectDebitRequest.customerID ?? "",
"mandate_type": sepaDirectDebitRequest.mandateType?.description ?? "",
"account_holder_name": sepaDirectDebitRequest.accountHolderName ?? "",
"iban": sepaDirectDebitRequest.iban ?? "",
"billing_address": billingAddressDictionary
]

let json: [String: Any] = [
"sepa_debit": sepaDebitDictionary,
"merchant_account_id": sepaDirectDebitRequest.merchantAccountID ?? "",
"cancel_url": BTCoreConstants.callbackURLScheme + "://sepa/cancel",
"return_url": BTCoreConstants.callbackURLScheme + "://sepa/success",
"locale": sepaDirectDebitRequest.locale ?? ""
]

apiClient.post("v1/sepa_debit", parameters: json) { body, response, error in
let sepaDebitRequest = SEPADebitRequest(sepaDirectDebitRequest: sepaDirectDebitRequest)
apiClient.post("v1/sepa_debit", parameters: sepaDebitRequest) { body, response, error in
if let error = error {
completion(nil, error)
return
Expand All @@ -61,16 +35,8 @@ class SEPADirectDebitAPI {
}

func tokenize(createMandateResult: CreateMandateResult, completion: @escaping (BTSEPADirectDebitNonce?, Error?) -> Void) {
let sepaDebitAccountDictionary: [String: String?] = [
"last_4": createMandateResult.ibanLastFour,
"merchant_or_partner_customer_id": createMandateResult.customerID,
"bank_reference_token": createMandateResult.bankReferenceToken,
"mandate_type": createMandateResult.mandateType
]

let json: [String: Any] = ["sepa_debit_account": sepaDebitAccountDictionary]

apiClient.post("v1/payment_methods/sepa_debit_accounts", parameters: json) { body, response, error in
let sepaDebitAccountsRequest = SEPADebitAccountsRequest(createMandateResult: createMandateResult)
apiClient.post("v1/payment_methods/sepa_debit_accounts", parameters: sepaDebitAccountsRequest) { body, response, error in
if let error = error {
completion(nil, error)
return
Expand Down
Loading