forked from stripe/stripe-ios
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMockAPIClient.swift
78 lines (69 loc) · 3.08 KB
/
MockAPIClient.swift
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
//
// MockAPIClient.swift
// UI Examples
//
// Created by Ben Guo on 7/18/17.
// Copyright © 2017 Stripe. All rights reserved.
//
import Foundation
import Stripe
private let swizzle: (AnyClass, Selector, Selector) -> () = { forClass, originalSelector, swizzledSelector in
let originalMethod = class_getInstanceMethod(forClass, originalSelector)
let swizzledMethod = class_getInstanceMethod(forClass, swizzledSelector)
method_exchangeImplementations(originalMethod!, swizzledMethod!)
}
extension STPAddCardViewController {
// We can't swizzle in initialize because it's been deprecated in Swift 3.1.
// Instead, we have to call this method before STPAddCardViewController appears.
static func startMockingAPIClient() {
let originalSelector = #selector(apiClient)
let swizzledSelector = #selector(swizzled_apiClient)
swizzle(self, originalSelector, swizzledSelector)
}
// Expose the private `apiClient` property as a method
@objc func apiClient() -> STPAPIClient? {
return nil
}
@objc func swizzled_apiClient() -> STPAPIClient? {
return MockAPIClient()
}
}
class MockAPIClient: STPAPIClient {
override func createPaymentMethod(with paymentMethodParams: STPPaymentMethodParams, completion: @escaping STPPaymentMethodCompletionBlock) {
guard let card = paymentMethodParams.card, let billingDetails = paymentMethodParams.billingDetails else { return }
// Generate a mock card model using the given card params
var cardJSON: [String: Any] = [:]
var billingDetailsJSON: [String: Any] = [:]
cardJSON["id"] = "\(card.hashValue)"
cardJSON["exp_month"] = "\(card.expMonth ?? 0)"
cardJSON["exp_year"] = "\(card.expYear ?? 0)"
cardJSON["last4"] = card.number?.suffix(4)
billingDetailsJSON["name"] = billingDetails.name
billingDetailsJSON["line1"] = billingDetails.address?.line1
billingDetailsJSON["line2"] = billingDetails.address?.line2
billingDetailsJSON["state"] = billingDetails.address?.state
billingDetailsJSON["postal_code"] = billingDetails.address?.postalCode
billingDetailsJSON["country"] = billingDetails.address?.country
cardJSON["country"] = billingDetails.address?.country
if let number = card.number {
let brand = STPCardValidator.brand(forNumber: number)
cardJSON["brand"] = STPCard.string(from: brand)
}
cardJSON["fingerprint"] = "\(card.hashValue)"
cardJSON["country"] = "US"
let paymentMethodJSON: [String: Any] = [
"id": "\(card.hashValue)",
"object": "payment_method",
"type": "card",
"livemode": false,
"created": NSDate().timeIntervalSince1970,
"used": false,
"card": cardJSON,
"billing_details": billingDetailsJSON,
]
let paymentMethod = STPPaymentMethod.decodedObject(fromAPIResponse: paymentMethodJSON)
DispatchQueue.main.asyncAfter(deadline: .now() + 0.6) {
completion(paymentMethod, nil)
}
}
}