当同时按下付款按钮时,两个用户能够购买1个数量的项目吗?

时间:2017-02-24 01:55:45

标签: ios swift firebase concurrency firebase-realtime-database

当两个不同的用户点击付费按钮时,这两个用户都被注册为成功购买该商品,尽管他们只剩下该商品的一个数量。

我正在检查项目是否在我的数据库中可用,一旦购买了一个项目,所购买的数量是从数据库中减去的。

我的付款按钮代码:

  let currentShoeRefSizes = DataService.ds.DATABASE_REF_SHOES.child("currentShoes").child(self.shoe.shoeId).child("sizes")

                        for (size,quantity) in self.shoe.databaseFormattedShoeSize {
                            //SIZE IN ACTUAL FORMAT
                            var sizeToChange = size
                            sizeToChange.insert(".", at: size.index(before: size.endIndex))

                            if "\(self.selectedSize)" == sizeToChange{
                                let currentSizeRef = currentShoeRefSizes.child(size)
                                currentSizeRef.observeSingleEvent(of: .value, with: { (snapshot) in
                                    if let quantity =  snapshot.value as? Int {
                                        if quantity > 0 {
                                            self.getCustomerInformation()
                                        }
                                    }
                                })
                            }

                        }
                    }

我的客户信息功能:

//FUNCTIONS
    func getCustomerInformation() {
        //Get Customer Id
        let currentUserRef = DataService.ds.DATABASE_REF_USERS.child(CURRENTUSER!).child("customerId")
        currentUserRef.observeSingleEvent(of: .value, with: { (snapshot) in
            let customerId = snapshot.value as! String
           self.chargeCustomer(customerId: customerId)


        })
    }

我的ChargeCustomer功能:

func chargeCustomer(customerId: String) {
        //Send the token to a server
        let serverUrl = "HIDDEN"
        let params = ["amount": "\(Int(shoe.shoePrice * 100))", "currency": "cad", "description" : "Single Purchase","customerId": customerId,"requestType": "singleCharge"]
        Alamofire.request(serverUrl, method: .post, parameters: params).responseJSON { (response) in
            print(response.response!) // HTTP URL response

        }
        //TODO: CHECK IF PAYMENT WENT THROUGH THEN DELETE IT FROM THE CART AND ADD TO ORDERS
        //Add Items to the completed orders
        let currentUserOrderRef = DataService.ds.DATABASE_REF_USERS.child(CURRENTUSER!).child("orders")

        let shoeDataToAdd = ["shoeName": shoe.shoeName,
                                 "shoePrice": shoe.shoePrice,
                                 "shoeProductImageUrl": shoe.shoeProductImageUrl,
                                 "size": self.selectedSize,
                                 "status": "Not Shipped Yet"
                ] as [String : Any]
            currentUserOrderRef.child(shoe.shoeId).updateChildValues(shoeDataToAdd)


        //Remove Quanitity Amount from database
            let currentShoeRef = DataService.ds.DATABASE_REF_SHOES.child("currentShoes").child(shoe.shoeId)
            for (size,quantity) in shoe.databaseFormattedShoeSize {
                //SIZE IN ACTUAL FORMAT
                var sizeToChange = size
                sizeToChange.insert(".", at: size.index(before: size.endIndex))

                if "\(selectedSize)" == sizeToChange{
                    let currentSizeRef = currentShoeRef.child("sizes").child(size)
                    let newQuantity = quantity - 1
                    currentSizeRef.setValue(newQuantity)
                }

            }
        self.performSegue(withIdentifier: "successPage", sender: nil)

    }

0 个答案:

没有答案