// // ToolStripePayment.swift // westkissMob // // Created by 王猛 on 2023/9/4. // import Foundation import UIKit import StripePaymentSheet import Stripe public typealias tt_currenttapclosure = (NSInteger , Any)->() public class ToolStripePayment: NSObject { @objc public var payFinishBlock:((String, String)->())? @objc public var order_id:String = "" @objc public var paymentMethodID:String = "" @objc public var req_baseUrl:String = "" @objc public var currentVC:UIViewController = UIViewController() @Published var paymentSheet: PaymentSheet? // @Published var paymentResult: PaymentSheetResult? @objc public func stripedidTapCheckoutButton(currentvc vc:UIViewController?, publishableKey:String, reqbaseUrl:String, amount:Int, currency:String, addressModel:ASAddressModel?, completion:tt_currenttapclosure?){ currentVC = vc! req_baseUrl = reqbaseUrl STPAPIClient.shared.publishableKey = publishableKey; let intentConfig = PaymentSheet.IntentConfiguration( mode: .payment(amount: amount, currency: currency) ) { [self] paymentMethod, shouldSavePaymentMethod, intentCreationCallback in self.stripehandleConfirm(paymentMethod, shouldSavePaymentMethod, intentCreationCallback) } var configuration = PaymentSheet.Configuration() configuration.defaultBillingDetails.email = addressModel?.email // let string1:str = addressModel?.firstname // let string2 = "World!" // let combinedString = string1 + string2 configuration.defaultBillingDetails.name = String(addressModel!.firstname + addressModel!.firstname) configuration.defaultBillingDetails.phone = addressModel?.telephone configuration.defaultBillingDetails.address.city = addressModel?.city configuration.defaultBillingDetails.address.country = addressModel?.country configuration.defaultBillingDetails.address.postalCode = addressModel?.postcode configuration.defaultBillingDetails.address.state = addressModel?.region.region configuration.defaultBillingDetails.address.line1 = addressModel?.street.first // print("=====street" + (addressModel?.street.first)! as Any) // configuration.allowsPaymentMethodsRequiringShippingAddress = true // STPUserInformation // let userInformation = STPUserInformation() // userInformation.email = "用户邮箱" // userInformation.phone = "用户电话" // let customer = PaymentSheet.CustomerConfiguration(id: "845600348@qq.com", ephemeralKeySecret: configuration.customer!.ephemeralKeySecret) ////// // configuration.customer = customer configuration.applePay = .init(merchantId: "merchant.com.asteriahair.app", merchantCountryCode: "US") configuration.returnURL = "asteriaStripe://stripe-redirect" // Use the return url you set up in the previous step configuration.allowsDelayedPaymentMethods = true self.paymentSheet = PaymentSheet(intentConfiguration: intentConfig, configuration: configuration) self.paymentSheet?.present(from: vc!) { paymentResult in switch paymentResult { case .completed: print("sucess") // if completion != nil { // completion!(0,self.order_id) // } if self.payFinishBlock != nil { self.payFinishBlock!("1", self.paymentMethodID) } case .canceled: print("Canceled!") // completion!(1,"") if self.payFinishBlock != nil { self.payFinishBlock!("2", "Canceled") } case .failed(let error): print(error) // completion!(2,error) if self.payFinishBlock != nil { self.payFinishBlock!("3", "failed") } } } } func stripehandleConfirm(_ paymentMethod: STPPaymentMethod, _ shouldSavePaymentMethod: Bool, _ intentCreationCallback: @escaping (Result) -> Void) { stripeserverSideConfirmHandler(paymentMethod.stripeId, shouldSavePaymentMethod, intentCreationCallback); } func stripeserverSideConfirmHandler(_ paymentMethodID: String, _ shouldSavePaymentMethod: Bool, _ intentCreationCallback: @escaping (Result) -> Void) { // Create and confirm an intent on your server and invoke `intentCreationCallback` with the client secret stripeconfirmIntent(paymentMethodID: paymentMethodID, shouldSavePaymentMethod: shouldSavePaymentMethod) { result in switch result { case .success(let clientSecret): intentCreationCallback(.success(clientSecret)) case .failure(let error): intentCreationCallback(.failure(error)) } } } func stripeconfirmIntent(paymentMethodID: String, shouldSavePaymentMethod: Bool, completion: @escaping (Result) -> Void) { self.paymentMethodID = paymentMethodID; let payMethod: [String: Any?] = [ "payment_method" : paymentMethodID, "manual_authentication" : "card", "payment_element" : true ] let body: [String: Any?] = [ "method" : "stripe_payments", "additional_data" : payMethod, ] let param: [String: Any?] = [ "paymentMethod" : body ] let order_requestUrl = req_baseUrl ASNetTools.shared().put(withPath: order_requestUrl, param:param as [AnyHashable : Any]) { json in if (json is NSNumber) { let orderId = json as! Int self.order_id = String(orderId) if self.payFinishBlock != nil { self.payFinishBlock!("0", self.order_id) } self.currentVC.dismiss(animated: true) } else { let clientSecret = json completion(.success(clientSecret as! String)) } } faild: { code, msg in if msg.contains("Authentication Required:") { var client_secret = msg.replacingOccurrences(of: "Authentication Required:", with: "") client_secret = client_secret.replacingOccurrences(of: " ", with: "") print("client_secret" + client_secret) completion(.success(client_secret)) } else { completion(.failure(ExampleError(errorDescription: (msg)) )) } } } struct ExampleError: LocalizedError { var errorDescription: String? } }