How to resolve STPPaymentHandlerAction Status error Optional("No such setupintent: (null)") - ios

i did all the code regarding to the Stripe and i followed the documentation of the stripe in which i i am calling one api to get the secret key from the server n which is coming..and after that i am passing it ot the "STPPaymentHandler.shared()"..but i am getting the error STPPaymentHandlerActionStatus error Optional("No such setupintent: (null)") a..please help me if you know thank you in advance
below the code which i did
i followed this documentation
https://stripe.com/docs/payments/accept-a-payment
//method to get secrete key
func checkout() {
let apikey = UserDefaults.standard.value(forKey: keyParameter.apikey.rawValue) as! String
let dic = ["apikey":apikey,"amount":"5000"]
Webhelper.sharedInstance.apiPostRequest(apiUrl: GlobalConstant.stripe_token, parameter: dic, success: { (success) in
print("success from the server is \(success)")
do{
let responseResult = try Global.sharedInstance.decode(jwtToken: success)
let message = Global.sharedInstance.getStringValue(responseResult["Message"] as AnyObject)
if let status = responseResult["result"] as? NSNumber {
if status == 1{
let responseDic = responseResult["Secrate"] as! NSDictionary
self.setupIntentClientSecret = responseDic.value(forKey: "clientSecret") as! String
let apikey = responseDic.value(forKey: "publishableKey") as! String
Stripe.setDefaultPublishableKey(apikey)
print(" self.setupIntentClientSecret self.setupIntentClientSecret \(self.setupIntentClientSecret)")
}else if status == 0{
Global.sharedInstance.ShowMessagePopup(Viewobj: self, title: "", Message: message)
}
}
}catch{
print("cought error in the catch \(error.localizedDescription)")
}
}) { (error) in
print("error for the server is \(error.localizedDescription)")
}
}
///this is the method to complete the payment
#objc func pay() {
// Collect card details
let cardParams = self.paymentTextField.cardParams
// Collect the customer's email to know which customer the PaymentMethod belongs to.
let billingDetails = STPPaymentMethodBillingDetails()
billingDetails.email = "sunil#gmial.com"//emailTextField.text
// Create SetupIntent confirm parameters with the above
let paymentMethodParams = STPPaymentMethodParams(card: cardParams, billingDetails: billingDetails, metadata: nil)
print("setupIntentClientSecret inside method\(setupIntentClientSecret)")
let setupIntentParams = STPSetupIntentConfirmParams(clientSecret: setupIntentClientSecret)
setupIntentParams.paymentMethodParams = paymentMethodParams
print("secrete \(setupIntentClientSecret)")
// Complete the setup
let paymentHandler = STPPaymentHandler.shared()
paymentHandler.confirmSetupIntent(withParams: setupIntentParams, authenticationContext: self) { status, setupIntent, error in
print("success from the server is setupIntent\(setupIntent) status \(status) error \(error?.localizedDescription)")
switch (status) {
case .failed:
self.displayAlert(title: "Setup failed", message: error?.localizedDescription ?? "")
// print("\(error?.localizedDescription)")
break
case .canceled:
self.displayAlert(title: "Setup canceled", message: error?.localizedDescription ?? "")
break
case .succeeded:
self.displayAlert(title: "Setup succeeded", message: setupIntent?.description ?? "", restartDemo: true)
break
#unknown default:
fatalError()
break
}
}
}

Related

Swift, couldn't get earning details from AdMob and AdSense

I'm working on my app (iOS) to get AdMob and AdSense earnings information. But I've been having trouble getting specifics from them. I've already created the credentials and client ID from my Google account, but I'm not sure where to put them.
I tried carefully following many methods from this link but was never successful.
My first step: During startup, check to see if you are logged in or out.
import FirebaseAuth
import GoogleSignIn
var currentPID = ""
func checkGoogleAccountStatus() {
GIDSignIn.sharedInstance.restorePreviousSignIn { user, error in
if error != nil || user == nil {
print("Signed out")
self.loginButton()
} else {
print("Signed in")
let userAccess: String = user!.authentication.accessToken
self.googledSignedInSuccess(googleToken: userAccess, tokenID: user!.authentication.idToken!)
let dateformatter = DateFormatter()
dateformatter.dateFormat = "MMMM d, yyyy h:mm:ss a"
let expiredToken = user?.authentication.accessTokenExpirationDate
print("Token Expired: \(dateformatter.string(from: expiredToken!))")
}
}
}
Successful
My second step: When I tapped the button to log in, an alert controller appeared to see if the log in was successful. It will display the alert controller's profile picture, name, and email address.
#objc func loginTapped() {
let adMobScope = "https://www.googleapis.com/auth/admob.report"
let adSenseScope = "https://www.googleapis.com/auth/adsensehost"
let additionalScopes = [adMobScope,adSenseScope]
let signInConfig = GIDConfiguration.init(clientID: "<My URL Schemes>")
GIDSignIn.sharedInstance.signIn(with: signInConfig, presenting: self, hint: nil, additionalScopes: additionalScopes) { user, error in
guard error == nil else { return }
guard let user = user else { return }
if let profiledata = user.profile {
let grantedScopes = user.grantedScopes
if grantedScopes == nil || !grantedScopes!.contains(adMobScope) {
print("AdMob not Granted...")
} else {
print("AdMob Granted!")
}
if grantedScopes == nil || !grantedScopes!.contains(adSenseScope) {
print("AdSense not Granted...")
} else {
print("AdSense Granted!")
}
//let userId: String = user.userID ?? ""
let givenName: String = profiledata.givenName ?? ""
let familyName: String = profiledata.familyName ?? ""
let email: String = profiledata.email
let userToken: String = user.authentication.idToken!
let userAccess: String = user.authentication.accessToken
let credential = GoogleAuthProvider.credential(withIDToken: userToken, accessToken: userAccess)
Auth.auth().signIn(with: credential) { result, error in
if let error = error {
print(error.localizedDescription)
let alert = UIAlertController(title: "Error", message: "Something went wrong, please try again.", preferredStyle: .alert)
let action = UIAlertAction(title: "OK", style: .cancel, handler: {_ in return })
alert.addAction(action)
self.present(alert, animated: true, completion: nil)
}
if let imgurl = user.profile?.imageURL(withDimension: 300) {
let absoluteurl: String = imgurl.absoluteString
let alert = UIAlertController(title: "\(givenName) \(familyName)", message: "\n\n\n\n\n\n\n\(email)\nLogin Successful", preferredStyle: .alert)
let action = UIAlertAction(title: "OK", style: .cancel, handler: {_ in
// MARK: Do something to update
self.checkGoogleAccountStatus()
})
let imgViewTitle = UIImageView()
imgViewTitle.translatesAutoresizingMaskIntoConstraints = false
imgViewTitle.layer.borderColor = UIColor(named: "Font Color")?.cgColor
imgViewTitle.layer.borderWidth = 3
imgViewTitle.layer.cornerRadius = 50
imgViewTitle.clipsToBounds = true
alert.view.addSubview(imgViewTitle)
imgViewTitle.centerYAnchor.constraint(equalTo: alert.view.centerYAnchor, constant: -28).isActive = true
imgViewTitle.centerXAnchor.constraint(equalTo: alert.view.centerXAnchor).isActive = true
imgViewTitle.widthAnchor.constraint(equalToConstant: 100).isActive = true
imgViewTitle.heightAnchor.constraint(equalToConstant: 100).isActive = true
DispatchQueue.global().async {
if let data = try? Data(contentsOf: URL(string: absoluteurl)! ) { if let image = UIImage(data: data) { DispatchQueue.main.async { imgViewTitle.image = image } } }
}
alert.addAction(action)
self.present(alert, animated: true, completion: nil)
} else {
let alert = UIAlertController(title: "\(givenName) \(familyName)", message: "\(email)\nLogin Successful", preferredStyle: .alert)
let action = UIAlertAction(title: "OK", style: .cancel, handler: {_ in
// MARK: Do something to update
self.checkGoogleAccountStatus()
})
alert.addAction(action)
self.present(alert, animated: true, completion: nil)
}
}
}
}
}
When I logged in, it asked for permission to access AdMob and AdSense, followed by a pop-up alert that said I had successfully logged in.
My third step: Getting the PID from Google AdMob / AdSense
import CurlDSL
import Gzip
func googledSignedInSuccess(googleToken: String, tokenID: String) {
guard let url = URL(string: "https://admob.googleapis.com/v1/accounts/") else { return }
do {
try CURL(#"curl -H "Authorization: Bearer \#(googleToken)" "\#(url)""#).run { data, response, error in
if let error = error { print("Error took place \(error)"); return }
if let response = response as? HTTPURLResponse {
if response.statusCode != 200 {
print("Error: \(response)")
} else {
if let data = data {
do {
if let rawJSON = try? JSONDecoder().decode(GetAdMobInfo.self, from: data) {
currentPID = rawJSON.account[0].publisherID
print("Successful: \(currentPID)")
self.adMob_gettingReport(pid: currentPID, token: googleToken)
}
}
}
}
}
}
} catch { print("Failed.") }
}
struct GetAdMobInfo: Codable {
let account: [Account]
}
struct Account: Codable {
let name, publisherID, reportingTimeZone, currencyCode: String
enum CodingKeys: String, CodingKey {
case name
case publisherID = "publisherId"
case reportingTimeZone, currencyCode
}
}
It was success, I was able to get my PID and writed to currentPID as string.
My final step, which failed:
func adMob_gettingReport(pid: String, token: String) {
guard let url = URL(string: "https://admob.googleapis.com/v1/accounts/\(pid)/mediationReport:generate") else { return }
let reportData = "--data #- << EOF {\"report_spec\": {\"date_range\": {\"start_date\": {\"year\": 2020, \"month\": 4, \"day\": 1}, \"end_date\": {\"year\": 2020, \"month\": 4, \"day\": 1} },\"dimensions\": [\"AD_SOURCE\", \"AD_UNIT\", \"PLATFORM\"], \"metrics\": [\"ESTIMATED_EARNINGS\"]}} EOF"
do {
try CURL(#"curl -X POST "\#(url)" -H "Authorization: Bearer \#(token)" -H "Content-Type: application/json" \#(reportData)"#).run { data, response, error in
if let error = error { print("Error took place \(error)"); return }
if let response = response as? HTTPURLResponse {
if response.statusCode != 200 {
print("Error: \(response)")
} else {
if let data = data {
print("Getting AdMob Successful")
let decompressedData: Data
if data.isGzipped { decompressedData = try! data.gunzipped() }
else { decompressedData = data }
var getLineFromString: [String] = []
getLineFromString += String(data: decompressedData, encoding: .utf8)!.components(separatedBy: "\n")
for checkLine in getLineFromString {
print("Line: \(checkLine)")
}
}
}
}
}
} catch { print("Failed.") }
}
Attempting to obtain earnings information from AdMob and AdSense, but it kept saying failing in print. This is where I've been for nearly two months. What did I overlook?

How i can pass the completion handler to another function swift ios

I'm working on the firebase chat app and here is the first function insertConversation2 and this function creating a node of conversation array that contains all the data about chat.
Now I want when user sending the message and code goes to success block, and at this point, I want to create another node of messages through function and pass the first function
completion handler to the second function (finishCreatingConversation) completion handler but the issue is that it's not working fine and not creating the second node. See the firebase screenshot conversation node is creating but message-id not creating please check the code thanks.
First function
func insertConversation2(with otherUserEmail: String,name:String,message:Message,completion:#escaping(Bool) -> Void){
let dformatter = DateFormatter()
dformatter.dateFormat = "dd/MM/yyy HH:mm"
let dateToString = dformatter.string(from: Date())
guard let email = UserDefaults.standard.value(forKey: "useremail") as? String else {
return}
var getSafeEmail = getUserEmail(currentEmail: email)
database.child("\(getSafeEmail)").observeSingleEvent(of: .value) { (snapshot) in
//if user is not preens to go to false block else go to furhter
guard var userNode = snapshot.value as? [String:Any] else {
completion(false)
print("user not found in insert time in networking manager")
return
}
var messageData = ""
switch message.kind{
case .text(let messageText):
messageData = messageText
default:break
}
let conversationId = "conversation_\(message.messageId)"
let newConversation:[String:Any] = [
"id":conversationId,
"other_user_email": otherUserEmail,
"name":name,
"latest_message":[
"date":dateToString,
"message":messageData,
"is_read":false
]
]
if var userConversation = userNode["conversation"] as? [[String:Any]]{
//conversation array is exiten append the conversation data
userConversation.append(newConversation)
userNode["conversation"] = userConversation
database.child("\(getSafeEmail)").setValue(userNode) { (error, ref) in
guard error != nil else {return}
self.finishCreatingConversation(conversationId: conversationId, message: message, completion: completion)
//completion(true)
}
}
else{
userNode["conversation"] = [
newConversation
]
database.child("\(getSafeEmail)").setValue(userNode) { (error, ref) in
guard error != nil else {return}
self.finishCreatingConversation(conversationId: conversationId, message: message, completion: completion) //second not working fine
//completion(true) //passing a refrence of completion in above function of insert method
}
}
}
}
Second function
func finishCreatingConversation(conversationId:String,message:Message,completion:#escaping(Bool) -> Void){
var messageData = ""
switch message.kind{
case .text(let messageText):
messageData = messageText
}
let dformatter = DateFormatter()
dformatter.dateFormat = "dd/MM/yyy HH:mm"
let dateToString = dformatter.string(from: Date())
guard let email = UserDefaults.standard.value(forKey: "useremail") as? String else {
return}
//var getUserEmail = getUserEmail(currentEmail: email)
let getUserEmailData = getUserEmail(currentEmail: email)
let collectionMessge: [String:Any] = [
"id":message.messageId,
"type":message.kind.messageKindString,
"content":messageData,
"date": dateToString,
"sender_email":getUserEmailData,
"is_read":false
]
database.child("\(conversationId)").setValue(collectionMessge) { (error, ref) in
guard error != nil else {return}
completion(true)
}
}
Firebase Image:
Replace
self.finishCreatingConversation(conversationId: conversationId, message: message, completion: completion)
with
self.finishCreatingConversation(conversationId: conversationId, message: message) { res in
completion(res)
}

Boolean condition is not working in Swift

I'm using the message Kit. In a chatDashbaord extension I've boolean condition like if the isnewConversation contain true then call the function insertConversation2 and if isnewConversation contain false then call the function sendMessag.But I've checked using the break points the code is not going to isNewconversation block and I don't know why it's happening.When I'm not using the boolean statement the code working file and append the data into an array please check the code thanks.
MessageViewController:
class ChatDashboard: MessagesViewController {
var userActive: String? = nil
var receiverName:String? = nil
var isnewConversation = false
var recevierName:String? = nil
var otheruserEmail: String? = nil
var conversationId:String? = nil
var messages : [Message] = []{
didSet{
DispatchQueue.main.async {
self.messagesCollectionView.reloadDataAndKeepOffset()
}
}
extension ChatDashboard:
extension ChatDashboard: InputBarAccessoryViewDelegate{
func inputBar(_ inputBar: InputBarAccessoryView, didPressSendButtonWith text: String) {
guard !text.replacingOccurrences(of: "", with: "").isEmpty,let selfSender =
self.selfSender,let messageId = createMessageId() else{
return
}
let message = Message(sender: selfSender, messageId: messageId, sentDate: Date(), kind: .text(text))
guard let otheruserEmail = otheruserEmail,let recevierName = recevierName else {return}
if isnewConversation{
NetworkingService.shared.insertConversation2(with:otheruserEmail,name:recevierName, message: message) { (success) in
if success{
print("message Send")
self.isnewConversation = false
}
else{
print("not send")
}
}
}
else{
guard let converId = conversationId else {return}
NetworkingService.shared.sendMessage(converId,recevierName,message) { (success) in
if success{
print("message send in")
}
else{
print("message not send")
}
}
}
}
}
}
SendMessage func code when condtion false:
func sendMessage(_ convesationId:String,_ name:String,_ message:Message,completion:#escaping(Bool) -> Void){
self.sendMessageReciverToSender(convesationId) { (user) in
guard var receiverToSenderNdoe = user else {completion(false); return}
let dformatter = DateFormatter()
dformatter.dateFormat = "dd/MM/yyy HH:mm"
let dateToString = dformatter.string(from: Date())
guard let email = UserDefaults.standard.value(forKey: "useremail") as? String else {
print("Email not found in user default")
completion(false)
return
}
let currentUserEmail = getUserEmail(currentEmail: email)
var messageData = ""
switch message.kind{
case .text(let messageText):
messageData = messageText
default:
break
}
let newMessge: [String:Any] = [
"id":message.messageId,
"type":message.kind.messageKindString,
"content":messageData,
"date": dateToString,
"sender_email":currentUserEmail,
"is_read":false,
"name":name
]
//issues is here
if var conversationData = receiverToSenderNdoe["messages"] as? [[String:Any]]{
conversationData.append(newMessge)
receiverToSenderNdoe["messages"] = conversationData
self.reciverToSenderSave(convesationId, newMessge) { (result) in
if result{
completion(true)
}
else{
completion(false)
}
}
}
}
}
Inside the sendMessage func this two function called
func sendMessageReciverToSender(_ conversationId:String,completion:#escaping([[String:Any]]?) -> Void){
database.child("\(conversationId)/messages").observeSingleEvent(of: .value) { (snapshot) in
if let currentUser = snapshot.value as? [[String:Any]]{
completion(currentUser)
}
else{
print("errro in reciver to sender sendMessageReciverToSender func ")
completion(nil)
}
}
}
func reciverToSenderSave(_ conversationId:String,_ conversation:[[String:Any]],completion:#escaping(Bool)-> Void){
database.child("\(conversationId)/messages").setValue(conversation){(error,ref) in
if error == nil{
completion(true)
}
else{
print("eroor in receiptin \(error?.localizedDescription)")
completion(false)
}
}
}
Your sendMessage has return without calling completion here
guard var receiverToSenderNdoe = user else {return}
Should be
guard var receiverToSenderNdoe = user else { completion(false) ; return }
And here
guard let email = UserDefaults.standard.value(forKey: "useremail") as? String else {
print("Email not found in user default")
return
}
Should be
guard let email = UserDefaults.standard.value(forKey: "useremail") as? String else {
print("Email not found in user default")
completion(false)
return
}
You have to make sure all paths are handled so to have your callback called in success/failure , BTW something like this
if result{
completion(true)
}
else{
completion(false)
}
Could be shortly
completion(result)

How to Log In a user threw Facebook and save that users data in Cloud Firestore

I'm trying to log users into into my app using Facebook and then save the users into my Cloud Firestore. I'm just not too sure how to go about it. This is the code I have now
#IBAction func buttTapped(_ sender: Any) {
let fbLoginManager = LoginManager()
fbLoginManager.logIn(permissions: ["public_profile", "email"], from: self) { (result, error) in
if let error = error {
print("Failed to login: \(error.localizedDescription)")
return
}
guard let accessToken = AccessToken.current else {
print("Failed to get access token")
return
}
let credential = FacebookAuthProvider.credential(withAccessToken: accessToken.tokenString)
// Perform login by calling Firebase APIs
Auth.auth().signIn(with: credential, completion: { (user, error) in
if let error = error {
print("Login error: \(error.localizedDescription)")
let alertController = UIAlertController(title: "Login Error", message: error.localizedDescription, preferredStyle: .alert)
let okayAction = UIAlertAction(title: "OK", style: .cancel, handler: nil)
alertController.addAction(okayAction)
self.present(alertController, animated: true, completion: nil)
return
}
// Present the main view
self.dismiss(animated: true, completion: nil)
})
}
}//
What code do I have to add to grab certain values like email and name and save it to Cloud Firestore the image below is an example for what I have using email signup
You just done the Facebook authentication and get the access token. To get user public info you need a graph request. The full Facebook Authentication and Graph request code will be like
func facebookLogin(){
let loginManager = LoginManager()
loginManager.logIn(permissions: [.publicProfile, .email], viewController: self) { loginResult in
switch loginResult {
case .failed(let error):
print(error)
case .cancelled:
print("User cancelled login.")
case .success(let grantedPermissions, let declinedPermissions, let accessToken):
print("Logged in!")
self.fetchUserProfile()
}
}
}
func fetchUserProfile() {
let graphRequest : GraphRequest = GraphRequest(graphPath: "me", parameters: ["fields":"id, email, name, picture.width(480).height(480)"])
graphRequest.start(completionHandler: { (connection, result, error) -> Void in
if ((error) != nil)
{
print("Error took place: \(error ?? "" as! Error)")
}
else
{
let result = result as! Dictionary<String, Any>
let picture = result["picture"] as! Dictionary<String, Any>
let dataObj = picture["data"] as! Dictionary<String,Any>
var emailValue = result["email"] as! String?
if emailValue == nil {
emailValue = ""
}
let name = result["name"] as? String
let id = result["id"] as? String
let imageUrl = result["url"] as? String
// Update data to firestore
}
})
}
Hope you understand.

How to know when asynchronous request has completed using a closure?

I am using Alamofire to download data and parse it with JSON. I know that in order to announce that data is available we should use closures and no the NotificationCenter. I do not understand closures though. How would I use a closure to reload a table view once the request has completed?
Here is the code.
func downloadEvents() {
let coreDataObject = CoreDataMethods()
Alamofire.request(URL(string:URL)!)
.responseJSON { response in
switch response.result {
case .success:
// we create the model object
let downloadedEvent = EventModel()
/* we have new data remove old data from core data*/
coreDataObject.deleteData(entityArgument: "Event")
events.removeAll() // remove the data from array this is no longer needed FIX
for JSONData in response.result.value as! [Dictionary<String, Any>] {
/* get the data from JSON and store in the event model*/
downloadedEvent.eTitle = JSONData[downloadedEvent.titleString] as? String
downloadedEvent.eDate = JSONData[downloadedEvent.dateString] as? String
downloadedEvent.eDescription = JSONData[downloadedEvent.descriptionString] as? String
downloadedEvent.eLocation = JSONData[downloadedEvent.locationline1String] as? String
downloadedEvent.eLocation2 = JSONData[downloadedEvent.locationline2String] as? String
/* if the event has an image save the url*/
if let image = JSONData[downloadedEvent.imageString] as? String {
downloadedEvent.eImageURL = image
} else {
/* else we save the default image url */
downloadedEvent.eImageURL = downloadedEvent.defaultImageURL
}
coreDataObject.save(eventParam: downloadedEvent)
}
/* post notification to reload table view FIX */
NotificationCenter.default.post(name: RELOAD_NOTIFICATION, object: nil)
case .failure(let error):
print("ALAMO REQUEST FIALED: \(error)")
}
}
}
Here is the downloadEvents function with the ability to notify the caller it was successful:
func downloadEvents(completion: #escaping (Bool, String?)-> Void) {
let coreDataObject = CoreDataMethods()
Alamofire.request(URL(string:URL)!)
.responseJSON { response in
switch response.result {
case .success:
// we create the model object
let downloadedEvent = EventModel()
/* we have new data remove old data from core data*/
coreDataObject.deleteData(entityArgument: "Event")
events.removeAll() // remove the data from array this is no longer needed FIX
for JSONData in response.result.value as! [Dictionary<String, Any>] {
/* get the data from JSON and store in the event model*/
downloadedEvent.eTitle = JSONData[downloadedEvent.titleString] as? String
downloadedEvent.eDate = JSONData[downloadedEvent.dateString] as? String
downloadedEvent.eDescription = JSONData[downloadedEvent.descriptionString] as? String
downloadedEvent.eLocation = JSONData[downloadedEvent.locationline1String] as? String
downloadedEvent.eLocation2 = JSONData[downloadedEvent.locationline2String] as? String
/* if the event has an image save the url*/
if let image = JSONData[downloadedEvent.imageString] as? String {
downloadedEvent.eImageURL = image
} else {
/* else we save the default image url */
downloadedEvent.eImageURL = downloadedEvent.defaultImageURL
}
coreDataObject.save(eventParam: downloadedEvent)
}
completion(true, nil)
/* post notification to reload table view FIX */
//NotificationCenter.default.post(name: RELOAD_NOTIFICATION, object: nil)
case .failure(let error):
print("ALAMO REQUEST FIALED: \(error)")
completion(false, "ALAMO REQUEST FIALED: \(error)")
}
}
}
You would then call the function like this:
func reloadTable(){
downloadEvents { (success, errMsg) in
if success{
DispatchQueue.main.async {
self.tableView.reloadData()
}
}
else{
let alertMessage: String
if let err = errMsg{
alertMessage = err
}
else{
alertMessage = "An unknown error occurred."
}
let alert = UIAlertController.init(title: "Request Failed", message: alertMessage, preferredStyle: .alert)
alert.addAction(UIAlertAction(title: "Ok", style: UIAlertActionStyle.cancel, handler: nil))
DispatchQueue.main.async {
self.present(alert, animated: true, completion: nil)
}
}
}
}

Resources