1import JGProgressHUD
2import UIKit
3import monetate_ios_sdk
4
5class PurchaseProductViewController: UIViewController {
6 var productforCart: [ProductObj] = []
7 @IBOutlet weak var topLabel: UILabel!
8 @IBOutlet weak var horizotalCentreView: UIView!
9 @IBOutlet weak var purchaseBtn: UIButton!
10 @IBOutlet weak var tableView: UITableView!
11 var spinner: SpinnerView?
12 var isPurchaseSuccessful = false
13 final var objPersonalization = Personalization(
14 account: Account(
15 instance: "p", domain: "localhost.org", name: "a-701b337c", shortname: "localhost"),
16 user: User(deviceId: "62bd2e2d-213d-463f-83bb-12c0b2530a14"))
17
18 override func viewDidLoad() {
19 super.viewDidLoad()
20 tableView.delegate = self
21 tableView.dataSource = self
22 tableView.separatorStyle = .none
23 horizotalCentreView.isHidden = true
24 self.title = "Checkout"
25 }
26
27 override func viewWillAppear(_ animated: Bool) {
28 var totlaprice = 0.0
29
30 for prod in productforCart {
31 totlaprice += prod.price * Double(prod.quantity)
32 }
33 topLabel.text = "Total Price of Products : $\(totlaprice)"
34 }
35
36 @IBAction func goDashboard(_ sender: Any) {
37 self.navigationController?.popToViewController(
38 (self.navigationController?.viewControllers.first)!, animated: false)
39 }
40
41 @IBAction func buy(_ sender: Any) {
42 let hud = JGProgressHUD()
43 hud.textLabel.text = "Purchasing.."
44 hud.show(in: self.view)
45
46 hud.dismiss(afterDelay: 3, animated: true) { [self] in
47 let alert = UIAlertController(
48 title: "Payment", message: "Would you like to proceed with payment?", preferredStyle: .alert
49 )
50 alert.addAction(
51 UIAlertAction(
52 title: "Yes", style: .default,
53 handler: { action in
54 let hud2 = JGProgressHUD()
55 hud2.textLabel.text = "Payment.."
56 hud2.show(in: self.view)
57 hud2.dismiss(afterDelay: 3, animated: true) { [self] in
58 placeOrderDidSelect()
59 print("Purchased")
60 }
61 }))
62 alert.addAction(
63 UIAlertAction(
64 title: "No", style: .destructive,
65 handler: { action in
66 }))
67 self.present(alert, animated: true, completion: nil)
68 }
69 }
70
71 func placeOrderDidSelect() {
72 var cartline: [PurchaseLine] = []
73 for item in productforCart {
74 cartline.append(
75 PurchaseLine(
76 sku: item.sku, pid: item.pid, quantity: item.quantity, currency: "USD",
77 value: String(Double(item.quantity) * item.price)))
78 }
79 objPersonalization.addEvent(
80 context: .PageView,
81 event: PageView(
82 pageType: "Purchase", path: "n/a", url: "n/a", categories: [], breadcrumbs: []))
83 objPersonalization.addEvent(
84 context: .Purchase,
85 event: Purchase(
86 account: "Amazon", domain: "amazon.com", instance: "temp", purchaseId: "pur-23232",
87 purchaseLines: cartline))
88 objPersonalization.getActionsData(
89 requestId: "123456", includeReporting: false,
90 arrActionTypes: ["monetate:action:OmnichannelJson"]
91 )
92 .on { (res) in
93 self.isPurchaseSuccessful = true
94 self.handleAction(res: res)
95 }
96 }
97
98 fileprivate func handleAction(res: APIResponse) {
99 let data = JSON(res.data)
100 for item in data["data"]["responses"].arrayValue {
101
102 if item["requestId"].string == res.requestId {
103 for oneaction in item["actions"].arrayValue {
104 let component = oneaction["component"].string ?? ""
105 if component.lowercased() == "footer" {
106 if let json = oneaction["json"].dictionary {
107 print("final dict \(json)")
108 if let text = json["text"]?.string {
109 if isPurchaseSuccessful {
110 for item in productforCart {
111 CategoryGeneratorforProducts.chnangeQuantityProduct(pid: item.pid, value: 0)
112 }
113 horizotalCentreView.isHidden = false
114 tableView.isHidden = true
115 productforCart.removeAll()
116 tableView.reloadData()
117 purchaseBtn.isHidden = true
118 topLabel.isHidden = true
119 return
120 }
121 }
122 }
123 }
124 }
125 }
126 }
127 }
128}
129
130extension PurchaseProductViewController: UITableViewDelegate, UITableViewDataSource {
131 func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
132 return productforCart.count
133 }
134
135 func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
136 let cell =
137 tableView.dequeueReusableCell(withIdentifier: "PurchaseProductTableViewCell", for: indexPath)
138 as! PurchaseProductTableViewCell
139 cell.img.image = productforCart[indexPath.row].image
140 cell.name.text = productforCart[indexPath.row].title
141 cell.price.text = "Price $\(String(productforCart[indexPath.row].price))"
142 cell.quantiy.text = "Quantity: \(String(productforCart[indexPath.row].quantity))"
143 cell.totalPrice.text =
144 "Total price $\(String(Double(productforCart[indexPath.row].quantity) * productforCart[indexPath.row].price))"
145 cell.totalPrice.textColor = .systemGreen
146 cell.name.textColor = .systemGreen
147 return cell
148 }
149 func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
150 return 85
151 }
152}