I am trying to link my iOS app with stripe using Heroku and its showing me this error -"Response status code was unacceptablle: 502."
My ViewController Code: -
import UIKit
import Stripe
import Alamofire
class ViewController: UIViewController {
var productStackView = UIStackView()
var paymentStackView = UIStackView()
var productImageView = UIImageView()
var productLabel = UILabel()
var payButton = UIButton()
var loadingSpinner = UIActivityIndicatorView()
var outputTextView = UITextView()
var paymentTextField = STPPaymentCardTextField()
var backendURL = "https://greyalien.herokuapp.com/"
override func viewDidLoad() {
super.viewDidLoad()
self.setupUI()
}
func setupUI() {
setupProductImage()
setupProductLabel()
setupLoadingSpinner()
setupPaymentTextFiled()
setupPayButton()
setupOutputTextView()
self.productStackView.frame = CGRect(x: 0, y: 70, width: 330, height: 150)
self.productStackView.center.x = self.view.center.x
self.productStackView.alignment = .center
self.productStackView.axis = .vertical
self.productStackView.distribution = .equalSpacing
self.productStackView.addArrangedSubview(self.productImageView)
self.productStackView.setCustomSpacing(10, after: self.productImageView)
self.productStackView.addArrangedSubview(self.productLabel)
self.view.addSubview(self.productStackView)
self.paymentStackView.frame = CGRect(x:0, y: 250, width: 300, height: 100)
self.paymentStackView.center.x = self.view.center.x
self.paymentStackView.alignment = .fill
self.paymentStackView.axis = .vertical
self.paymentStackView.distribution = .equalSpacing
self.paymentStackView.addArrangedSubview(self.paymentTextField)
self.paymentStackView.addArrangedSubview(self.payButton)
self.view.addSubview(self.paymentStackView)
}
func setupProductImage(){
self.productImageView = UIImageView(frame: CGRect(x: 50, y: 50, width: 275, height: 200))
self.productImageView.image = UIImage(named: "stripe press")
self.productImageView.contentMode = .scaleAspectFit
}
func setupProductLabel() {
self.productLabel.frame = CGRect(x: 0, y: 420, width: self.view.frame.width, height: 50)
self.productLabel.text = "Buy a stripe press book = $10.99"
self.productLabel.textAlignment = .center
}
func setupOutputTextView() {
self.outputTextView.frame = CGRect(x: 0, y: 420, width: self.view.frame.width - 50, height: 100)
self.outputTextView.center.x = self.view.center.x
self.outputTextView.textAlignment = .left
self.outputTextView.font = UIFont.systemFont(ofSize: 18)
self.outputTextView.text = ""
self.outputTextView.layer.borderColor = UIColor.purple.cgColor
self.outputTextView.isEditable = false
self.view.addSubview(self.outputTextView)
}
func setupPaymentTextFiled() {
self.paymentTextField.frame = CGRect(x: 0, y: 0, width: 330, height: 60)
}
func setupPayButton() {
self.payButton.frame = CGRect(x: 60, y: 480, width: 150, height: 40)
self.payButton.setTitle("Submit Payment", for: .normal)
self.payButton.setTitleColor(UIColor.white, for: .normal)
self.payButton.layer.cornerRadius = 5.0
self.payButton.backgroundColor = UIColor.init(red:50/255,green: 50/255,blue: 93/255, alpha:1.0)
self.payButton.layer.borderWidth = 1.0
self.payButton.addTarget(self, action: #selector(pay), for: .touchUpInside)
// payButton.addTarget(self, action: #selector(pay), for: UIControl.Event.touchUpInside)
}
func setupLoadingSpinner() {
self.loadingSpinner.color = UIColor.darkGray
self.loadingSpinner.frame = CGRect(x: 0, y: 300, width: 25, height: 25)
self.loadingSpinner.center.x = self.view.center.x
self.view.addSubview(self.loadingSpinner)
}
func startLoading() {
DispatchQueue.main.async {
self.loadingSpinner.startAnimating()
self.loadingSpinner.isHidden = false
}
}
func stopLoading() {
DispatchQueue.main.async {
self.loadingSpinner.stopAnimating()
self.loadingSpinner.isHidden = true
}
}
func diplayStatus (_ message:String) {
DispatchQueue.main.async {
self.outputTextView.text += message + " \n"
self.outputTextView.scrollRangeToVisible(NSMakeRange(self.outputTextView.text.count - 1, 1))
}
}
@objc func pay(){
//1)[server-side] Create a paymentintnent
// [client-side] confirm the paymentintent
// make a POST request to the /create paymentitney endpoint
self.stopLoading()
self.createPaymentIntent{(paymentIntentResponse,error) in
if let error = error {
self.stopLoading()
self.diplayStatus(error.localizedDescription )
print(error)
return
}
else {
guard let responseDictionary = paymentIntentResponse as? [String:AnyObject] else {
print("incorrect response")
return
}
print(responseDictionary)
let clientSecret = responseDictionary["secret"] as! String
}
}
}
func createPaymentIntent(completion:@escaping STPJSONResponseCompletionBlock)
{
var url = URL(string: backendURL)
url?.appendPathComponent("create_paymrnt_intent")
Alamofire.request(url as! URLConvertible,method:.post, parameters:[:])
.validate(statusCode: 200..<300)
.responseJSON {(response) in
switch(response.result) {
case .failure(let error):
completion(nil,error)
case .success(let json):
completion(json as? [String:Any], nil)
}
}
}
}
I am attaching the screenshot:-
I have entered the Stripe secret keys and everything correctly and the app is online as you can see:-
What am I doing wrong ?