-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: adds support for
x-www-form-urlencoded
(#58)
* feat: feat: adds support for x-www-form-urlencoded content-type * fix: set text in http client example * feat: adds default content-type if not present in config * refactor: simplify example
- Loading branch information
1 parent
6147678
commit 9d6dfc4
Showing
6 changed files
with
306 additions
and
10 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
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
112 changes: 112 additions & 0 deletions
112
IntegrationTester/IntegrationTester/HttpClientViewController.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,112 @@ | ||
// | ||
// HttpClientViewController.swift | ||
// IntegrationTester | ||
// | ||
// Created by kevin on 28/6/23. | ||
// | ||
|
||
import Foundation | ||
import UIKit | ||
import BasisTheoryElements | ||
import BasisTheory | ||
import Combine | ||
|
||
class HttpClientViewController: UIViewController { | ||
private let lightBackgroundColor : UIColor = UIColor( red: 240/255, green: 240/255, blue: 240/255, alpha: 1.0 ) | ||
private let darkBackgroundColor : UIColor = UIColor( red: 200/255, green: 200/255, blue: 200/255, alpha: 1.0 ) | ||
private var cancellables = Set<AnyCancellable>() | ||
|
||
@IBOutlet weak var cardNumberTextField: CardNumberUITextField! | ||
@IBOutlet weak var expirationDateTextField: CardExpirationDateUITextField! | ||
@IBOutlet weak var cvcTextField: CardVerificationCodeUITextField! | ||
@IBOutlet weak var cardBrand: UITextView! | ||
|
||
@IBAction func printToConsoleLog(_ sender: Any) { | ||
cardNumberTextField.text = "4242424242424242" | ||
expirationDateTextField.text = "10/26" | ||
cvcTextField.text = "909" | ||
} | ||
|
||
@IBAction func createPaymentMethod(_ sender: Any) { | ||
let body: [String: Any] = [ | ||
"type": "card", | ||
"billing_details": [ | ||
"name": "Peter Panda" | ||
], | ||
"card": [ | ||
"number": self.cardNumberTextField, | ||
"exp_month": self.expirationDateTextField.month(), | ||
"exp_year": self.expirationDateTextField.format(dateFormat: "YY"), | ||
"cvc": self.cvcTextField | ||
] | ||
] | ||
|
||
BasisTheoryElements.post( | ||
url: "https://api.stripe.com/v1/payment_methods", | ||
payload: body, | ||
config: Config.init(headers: ["Authorization" : "Bearer {{ Stripe's API Key }}", "Content-Type": "application/x-www-form-urlencoded"])) { data, error, completion in | ||
DispatchQueue.main.async { | ||
guard error == nil else { | ||
print(error) | ||
return | ||
} | ||
print(data) | ||
} | ||
} | ||
} | ||
|
||
|
||
override func viewDidLoad() { | ||
super.viewDidLoad() | ||
|
||
setStyles(textField: cardNumberTextField, placeholder: "Card Number") | ||
setStyles(textField: expirationDateTextField, placeholder: "MM/YY") | ||
setStyles(textField: cvcTextField, placeholder: "CVC") | ||
|
||
let cvcOptions = CardVerificationCodeOptions(cardNumberUITextField: cardNumberTextField) | ||
cvcTextField.setConfig(options: cvcOptions) | ||
|
||
cardNumberTextField.subject.sink { completion in | ||
print(completion) | ||
} receiveValue: { message in | ||
print("cardNumber:") | ||
print(message) | ||
|
||
if (!message.details.isEmpty) { | ||
let brandDetails = message.details[0] | ||
|
||
self.cardBrand.text = brandDetails.type + ": " + brandDetails.message | ||
} | ||
}.store(in: &cancellables) | ||
|
||
expirationDateTextField.subject.sink { completion in | ||
print(completion) | ||
} receiveValue: { message in | ||
print("expirationDate:") | ||
print(message) | ||
}.store(in: &cancellables) | ||
|
||
cvcTextField.subject.sink { completion in | ||
print(completion) | ||
} receiveValue: { message in | ||
print("CVC:") | ||
print(message) | ||
}.store(in: &cancellables) | ||
} | ||
|
||
private func setStyles(textField: UITextField, placeholder: String) { | ||
textField.layer.cornerRadius = 15.0 | ||
textField.placeholder = placeholder | ||
textField.backgroundColor = lightBackgroundColor | ||
textField.addTarget(self, action: #selector(didBeginEditing(_:)), for: .editingDidBegin) | ||
textField.addTarget(self, action: #selector(didEndEditing(_:)), for: .editingDidEnd) | ||
} | ||
|
||
@objc private func didBeginEditing(_ textField: UITextField) { | ||
textField.backgroundColor = darkBackgroundColor | ||
} | ||
|
||
@objc private func didEndEditing(_ textField: UITextField) { | ||
textField.backgroundColor = lightBackgroundColor | ||
} | ||
} |
Oops, something went wrong.