123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187 |
- //
- // 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<String, Error>) -> Void) {
- stripeserverSideConfirmHandler(paymentMethod.stripeId, shouldSavePaymentMethod, intentCreationCallback);
- }
- func stripeserverSideConfirmHandler(_ paymentMethodID: String,
- _ shouldSavePaymentMethod: Bool,
- _ intentCreationCallback: @escaping (Result<String, Error>) -> 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<String, Error>) -> 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?
- }
-
- }
|