Hello,
I start in the swift language, I manage with many other languages but there I do not understand how variables and parameters pass in functions ...
I want to add a 3D secure control function from Stripe, here is the example given by Stripe:
// Assemble the PaymentIntent parameters
let paymentIntentParams = STPPaymentIntentParams(clientSecret: clientSecret)
paymentIntentParams.paymentMethodId = paymentResult.paymentMethod.stripeId
// Confirm the PaymentIntent
STPPaymentHandler.shared().confirmPayment(withParams: paymentIntentParams, authenticationContext: paymentContext) { status, paymentIntent, error in
switch status {
case .succeeded:
// Your backend asynchronously fulfills the customer's order, e.g. via webhook
completion(.success, nil)
case .failed:
completion(.error, error) // Report error
case .canceled:
completion(.userCancellation, nil) // Customer cancelled
@unknown default:
completion(.error, nil)
}
}
But I don't understand where to place this function!
This is an existing application, here is the part concerned with regard to payment:
PurchaseValidationController.swift:
import UIKit
import Lottie
import Firebase
class PurchaseValidationController: UIViewController {
let worker = PurchaseValidationWorker()
@IBOutlet private weak var animationView: AnimationView!
@IBOutlet private weak var titleLabel: UILabel!
@IBOutlet private weak var subtitleLabel: UILabel!
@IBOutlet private weak var purchaseButton: PrimaryButton!
override func viewDidLoad() {
// ...
}
private func buyProduct() {
if worker.product.creator?.id == worker.user.id {
displayUserOwnProductError()
return
}
purchaseButton.displayActivity(true)
worker.buyProduct {
[weak self] (error) in
if self == nil { return }
print("Purchase validation controller")
self?.purchaseButton.displayActivity(false)
if error != nil {
self?.displayPurchaseError()
} else {
if let id = self?.worker.product.id,
let title = self?.worker.product.title,
let order = self?.worker.order {
print(order)
Analytics.logEvent(AnalyticsEventBeginCheckout, parameters: [
AnalyticsParameterItemID: id,
AnalyticsParameterItemName: title,
])
}
self?.replaceByPurchaseConfirmationController()
}
}
}
}
We therefore call the buyProduct function of the worker which is in "PuschaseValidationWorker.swift"
import Foundation
import Stripe
class PurchaseValidationWorker {
// Input
var product: Product!
var user: Account!
// Ouput
var order: Order?
func buyProduct(completion: @escaping (_ error: AppError?) -> Void) {
Product.buyProduct(id: product.id!){
[weak self] (order, error) in
self?.order = order
print("Purchase Validation Worker")
completion(error)
}
}
}
Who calls him the buyProduct function of Product either from the file "Product Networking.swift"
import Alamofire
import CoreLocation
import Stripe
extension Product {
static func buyProduct(id: String,
completion: @escaping (_ order: Order?, _ error: AppError?) -> Void) {
ProductAPIRouter.buyProduct(productId: id).performRequest { (result, networkError) in
processOrderResponse(result: result,
networkError: networkError,
completion: completion)
// We send the request to a nodeJS API which returns the "result"
// I tried to put the function here but it gives me an error because of the "paymentContext"
}
}
}
I tried to put the Stripe function in each of the 3 files but nothing to do. I don't understand where I should put it.
Logically, since I get the API response in the "buyProduct" function of the "Product Networking.swift" file, should I put it there?
So I put it in this function but it does not find the "paymentContext" of the stripe function (authenticationContext: paymentContext)
Some use "self" as paymentContext, so I have to declare paymentContext somewhere but where?
Thank you in advance, because I have been struggling for days on!