I am making an e-commerce app and trying to deal with the backend stuff so the shop owner can see what a user ordered. I am going to use Stripe + Parse for this.
In my Payment View Controller I use fast enumeration to go through my array and get the items in the shopping cart. The items in my array can only be one of two custom objects (Bike or Accessory).
Then I put the objects I get back into a NSDictionary, which is required to use my Stripe token and Parse together. Then the items in my NSDictionary should come out listed in my Parse log.
My issue is that this only works if I have one Bike and/or one Accessory. If I add a second Bike or Accessory to my cart, it just replaces the old info with my new Bike or Accessory.
I can kinda see in my code why this happens but I don't really know how to find a solution (still a newbie to programming).
Would appreciate your help! Code below:
Cart *cartObject = [Cart sharedManager];
for (id object in cartObject.cartArray)
{
if ([object isKindOfClass:[ChosenBike class]])
{
ChosenBike *bikeObject = (ChosenBike *)object;
self.bikeName = bikeObject.chosenName;
self.bikeSize = bikeObject.chosenSize;
self.bicycleHasRearBrake = bikeObject.bicycleHasRearBrake;
self.bikeWheelSetColor = bikeObject.chosenWheelSetColor;
self.bikeExtraWheelset = bikeObject.extraSeriesWheelset;
self.bikeQty = bikeObject.chosenQuantity;
}
else if ([object isKindOfClass:[ChosenAccessory class]])
{
ChosenAccessory *accessoryObject = (ChosenAccessory *)object;
self.accessoryName = accessoryObject.chosenName;
self.accessoryQty = accessoryObject.chosenQuantity;
self.accessoryColor = accessoryObject.color;
self.accessorySize = accessoryObject.chosenSize;
}
}
NSDictionary *chargeParams = @{
@"token": token.tokenId,
@"currency": @"usd",
@"amount": result, // this is in cents (i.e. 1000 = $10)
@"bikeName": self.bikeName,
@"bikeSize": self.bikeSize,
@"bikeHasRearBrake": [NSNumber numberWithBool:self.bicycleHasRearBrake],
@"bikeColor": self.bikeWheelSetColor,
@"bikeExtraWheelset": self.bikeExtraWheelset,
@"bikeQty": [self.bikeQty stringValue],
@"accessoryName": self.accessoryName,
@"accessoryQty": [self.accessoryQty stringValue],
@"accessoryColor": self.accessoryColor,
@"accessorySize": self.accessorySize,
};
// This passes the token off to our payment backend, which will then actually complete charging the card using your account's
[PFCloud callFunctionInBackground:@"charge"
withParameters:chargeParams
block:^(id object, NSError *error) {
[MBProgressHUD hideHUDForView:self.view animated:YES];
if (error) {
[self hasError:error];
return;
}
[self.presentingViewController dismissViewControllerAnimated:YES
completion:^{
[[[UIAlertView alloc] initWithTitle:@"Payment Succeeded"
message:nil
delegate:nil
cancelButtonTitle:nil
otherButtonTitles:@"OK", nil] show];
}];
}];
}