3

Has anybody successfully integrated the Adyen payment drop-in in an iOS app?.

I get to the point where it shows the user the payment options, but selecting one of them doesn't do anything. I would expect it to move on to the form to capture the card details but it doesn't.

Any help would be greatly appreciated. The code for the view controller is as follows.

import UIKit
import Adyen

extension Dictionary {
        func percentEncoded() -> Data? {
            return map { key, value in
                let escapedKey = "\(key)".addingPercentEncoding(withAllowedCharacters: .urlQueryValueAllowed) ?? ""
                let escapedValue = "\(value)".addingPercentEncoding(withAllowedCharacters: .urlQueryValueAllowed) ?? ""
                return escapedKey + "=" + escapedValue
            }
            .joined(separator: "&")
            .data(using: .utf8)
        }
    }

extension CharacterSet {
    static let urlQueryValueAllowed: CharacterSet = {
        let generalDelimitersToEncode = ":#[]@" // does not include "?" or "/" due to RFC 3986 - Section 3.4
        let subDelimitersToEncode = "!$&'()*+,;="
        
        var allowed = CharacterSet.urlQueryAllowed
        allowed.remove(charactersIn: "\(generalDelimitersToEncode)\(subDelimitersToEncode)")
        return allowed
    }()
}

class ViewController: UIViewController , DropInComponentDelegate {
    
    func didSubmit(_ data: PaymentComponentData, from component: DropInComponent) {
        
    }
    
    func didProvide(_ data: ActionComponentData, from component: DropInComponent) {
        
    }
    
    func didFail(with error: Error, from component: DropInComponent) {
       
    }
    
    func didCancel(component: PresentableComponent, from dropInComponent: DropInComponent){
       
    }

    override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view.

    }
    
    @IBAction func buttonCLicked(_ sender: UIButton) {
        setUpAdyen()
    }
    
    func setUpAdyen() {
        
        let url = URL(string: "xxxxxxx")!
        
        var request = URLRequest(url: url)
        
        request.setValue("application/x-www-form-urlencoded", forHTTPHeaderField: "Content-Type")
        
        request.httpMethod = "POST"
        
        let parameters: [String: Any] = [
            "amount": 1000,
        ]
        
        request.httpBody = parameters.percentEncoded()
        
        print(request.httpBody ?? "")
        
        let task = URLSession.shared.dataTask(with: request) { data, response, error in
            
            guard let data = data,
                  let response = response as? HTTPURLResponse,
                  error == nil else {
                print("error", error ?? "Unknown error")
                return
            }
            
            guard (200 ... 299) ~= response.statusCode else {
                print("statusCode should be 2xx, but is \(response.statusCode)")
                print("response = \(response)")
                return
            }
            
            do {
              
                let paymentMethods = try JSONDecoder().decode(PaymentMethods.self, from: data)
                
                let configuration = DropInComponent.PaymentMethodsConfiguration()
                
                configuration.clientKey = "xxxxxxxx"
                
                let dropInComponent = DropInComponent(paymentMethods: paymentMethods,paymentMethodsConfiguration: configuration)
                
                dropInComponent.delegate = self
                
                dropInComponent.environment = .test
                
                dropInComponent.payment = Payment(amount: Payment.Amount(value: 1000, currencyCode: "GBP"))
                
                DispatchQueue.main.async {
                    self.present(dropInComponent.viewController, animated: true)
                }
 
            } catch {
                print(error)
            }
        
        }
        
        task.resume()

        
    }
  
}

The payment options show but clicking on any of them does nothing when it should show the screen to capture the card details.

Xmlwiz
  • 31
  • 2

1 Answers1

2

The dropInComponent should be persisted.


class ViewController: UIViewController , DropInComponentDelegate {

     private var dropInComponent: DropInComponent?

     ...
     
     func setUpAdyen() {
         ...

         let dropInComponent = DropInComponent(paymentMethods: paymentMethods,paymentMethodsConfiguration: configuration)
         self.dropInComponent = dropInComponent

         ...
                
     }
}

Vladimir
  • 345
  • 1
  • 13