Skip to content

Commit

Permalink
1st commit
Browse files Browse the repository at this point in the history
  • Loading branch information
hamzaouazzani committed Jun 27, 2014
0 parents commit a72f19e
Show file tree
Hide file tree
Showing 150 changed files with 11,459 additions and 0 deletions.
4 changes: 4 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
MVC.xcodeproj/project.xcworkspace/xcuserdata/hamzaouazzanichahdi.xcuserdatad/UserInterfaceState.xcuserstate
*.xcworkspace
xcuserdata
Pods/*
699 changes: 699 additions & 0 deletions MVC.xcodeproj/project.pbxproj

Large diffs are not rendered by default.

27 changes: 27 additions & 0 deletions MVC/AddPaymentViewController.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
//
// PaymentViewController.h
// MVC
//
// Created by Hamza Ouazzani Chahdi on 17/03/2014.
// Copyright (c) 2014 Hamza Ouazzani Chahdi. All rights reserved.
//

#import <UIKit/UIKit.h>
#import "STPView.h"
#import "SWRevealViewController.h"
#import "PaymentViewController.h"
#import "User.h"
#import "AppDelegate.h"

@interface AddPaymentViewController : UIViewController <STPViewDelegate>

@property STPView* checkoutView;

@property (strong, nonatomic) IBOutlet UIButton *saveButton;
@property (strong, nonatomic) IBOutlet UILabel *paymentLabel;
@property (strong, nonatomic) IBOutlet UIButton *cancelButton;
@property BOOL changeCard;

- (IBAction)save:(id)sender;

@end
175 changes: 175 additions & 0 deletions MVC/AddPaymentViewController.m
Original file line number Diff line number Diff line change
@@ -0,0 +1,175 @@
//
// PaymentViewController.m
// MVC
//
// Created by Hamza Ouazzani Chahdi on 17/03/2014.
// Copyright (c) 2014 Hamza Ouazzani Chahdi. All rights reserved.
//
#import "AddPaymentViewController.h"
#import "MBProgressHUD.h"
#import "Mixpanel.h"
#import <Appsee/Appsee.h>
#import "Heap.h"

@interface AddPaymentViewController ()

- (void)hasError:(NSError *)error;
- (void)hasToken:(STPToken *)token;

@end

@implementation AddPaymentViewController

- (void)viewDidLoad
{
[super viewDidLoad];

// Setup save button
self.saveButton.enabled = NO;
self.paymentLabel.text = [[NSUserDefaults standardUserDefaults] objectForKey:@"credit_card"];


// Setup checkout
self.checkoutView = [[STPView alloc] initWithFrame:CGRectMake(15,100,290,55) andKey:STRIPE_PUBLISHABLE_KEY];
self.checkoutView.delegate = self;
[self.view addSubview:self.checkoutView];


}
- (IBAction)cancel:(id)sender {
//If it's coming from AddPayment then go to the ParkVC
if ([self.presentingViewController isKindOfClass:[AddPhoneViewController class]]) {

// Track event
[Heap track:@"Cancel Card"];

AppDelegate *appDelegate = [UIApplication sharedApplication].delegate;
[appDelegate userLoggedIn];
}
//Else dismiss
else{
[self dismissViewControllerAnimated:YES completion:nil];
}
}

- (void)stripeView:(STPView *)view withCard:(PKCard *)card isValid:(BOOL)valid
{
// Enable save button if the Checkout is valid
self.saveButton.enabled = YES;
}

- (IBAction)save:(id)sender
{
[MBProgressHUD showHUDAddedTo:self.view animated:YES];
self.saveButton.enabled=NO;

[self.checkoutView createToken:^(STPToken *token, NSError *error) {
[MBProgressHUD hideHUDForView:self.view animated:YES];

if (error) {
[self hasError:error];
Mixpanel *mixpanel = [Mixpanel sharedInstance];
[mixpanel track:@"Card error"];
} else {
[self hasToken:token];
}
}];

}

- (void)hasError:(NSError *)error
{
UIAlertView *message = [[UIAlertView alloc] initWithTitle:NSLocalizedString(@"Error", @"Error")
message:@"Your card is not valid. Call us if you need help"
delegate:self
cancelButtonTitle:@"Retry"
otherButtonTitles:@"Call support",nil];
[message show];
}

- (void)alertView:(UIAlertView *)alert clickedButtonAtIndex:(NSInteger)buttonIndex
{
if (buttonIndex == 1) {
NSUserDefaults* userDefaults=[NSUserDefaults standardUserDefaults];
NSString* supportNumber =[userDefaults objectForKey:@"support_number"];
NSString *phoneNumber = [supportNumber stringByReplacingOccurrencesOfString:@" " withString:@""];
NSString *phoneURL = [NSString stringWithFormat:@"tel:%@",phoneNumber];
[[UIApplication sharedApplication] openURL:[NSURL URLWithString:phoneURL]];
}
}

- (void)hasToken:(STPToken *)token
{
NSLog(@"Stripe card token %@", token.tokenId);
NSLog(@"Card type %@", token.card.type);

// Define variables
NSString *cardNumber = [[NSString alloc] initWithFormat:@"•••%@",token.card.last4];
NSString *cardType = token.card.type;

// Store last4 digits and card type in phone
[[NSUserDefaults standardUserDefaults] setObject:cardNumber forKey:@"cardNumber"];
[[NSUserDefaults standardUserDefaults] setObject:cardType forKey:@"cardType"];

// Set user properties
User *theUser = [User theUser];
theUser.cardNumber = cardNumber;
theUser.cardType = cardType;

// Send card token to back-end
NSDictionary *cardParams = [NSDictionary dictionaryWithObjectsAndKeys:token.tokenId, @"card_token", [User theUser].token,@"auth_token", nil];

NSString *baseURL = API_URL;
NSString *path = [NSString stringWithFormat:@"/users/%@",[User theUser].id];
NSString *url = [baseURL stringByAppendingString:path];

AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
manager.requestSerializer = [AFJSONRequestSerializer serializer];
manager.responseSerializer = [AFJSONResponseSerializer serializer];

[manager PUT:url parameters:cardParams success:^(AFHTTPRequestOperation *operation, id JSON) {

[MBProgressHUD hideHUDForView:self.view animated:YES];

if (!theUser.isCustomer) {

// Track Mixpanel
Mixpanel *mixpanel = [Mixpanel sharedInstance];
[mixpanel track:@"Save card"];
[mixpanel.people set:@{@"Card saved": @"1"}];

// Send to Appsee
[Appsee addEvent:@"Card saved"];

NSUserDefaults *userDefaults = [NSUserDefaults standardUserDefaults];

// The user is now a customer
[userDefaults setObject:JSON[@"user"][@"is_customer"] forKey:@"isCustomer"];
theUser.isCustomer = [JSON[@"user"][@"is_customer"] boolValue];

// Update his credits
[userDefaults setObject:JSON[@"user"][@"credits"] forKey:@"credits"];
theUser.credits = JSON[@"user"][@"credits"];

AppDelegate *appDelegate = [UIApplication sharedApplication].delegate;
[appDelegate userLoggedIn];
} else {
[self.navigationController popViewControllerAnimated:YES];
}


} failure:^(AFHTTPRequestOperation *operation, NSError *error) {

NSLog(@"NSError: %@",error.localizedDescription);
[MBProgressHUD hideHUDForView:self.view animated:YES];
[self hasError:error];

}];
}

-(void) popVC
{
[self.navigationController popViewControllerAnimated:YES];
}

@end
21 changes: 21 additions & 0 deletions MVC/AddPhoneViewController.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
//
// AddPhoneViewController.h
// MVC
//
// Created by Thomas on 28/05/2014.
// Copyright (c) 2014 Hamza Ouazzani Chahdi. All rights reserved.
//

#import <UIKit/UIKit.h>
#import "RMPhoneFormat.h"
#import "AppDelegate.h"
#import "User.h"
#import "Mixpanel.h"

@interface AddPhoneViewController : UIViewController

@property (strong, nonatomic) IBOutlet UITextField *phoneTextField;
@property (strong, nonatomic) IBOutlet UILabel *phoneLabel;
@property (weak, nonatomic) IBOutlet UIButton *nextButton;

@end
89 changes: 89 additions & 0 deletions MVC/AddPhoneViewController.m
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
//
// AddPhoneViewController.m
// MVC
//
// Created by Thomas on 28/05/2014.
// Copyright (c) 2014 Hamza Ouazzani Chahdi. All rights reserved.
//

#import "AddPhoneViewController.h"
#import <Appsee/Appsee.h>

@implementation AddPhoneViewController

#pragma mark - UIViewController

- (void)viewDidLoad
{
[super viewDidLoad];
self.nextButton.enabled=YES;

NSString *phoneMessage = [[NSUserDefaults standardUserDefaults] objectForKey:@"phone_message"];
self.phoneLabel.text = phoneMessage;
}

- (void)viewDidAppear:(BOOL)animated
{
[self.phoneTextField becomeFirstResponder];
}

#pragma mark - IBAction

- (IBAction)next:(id)sender {
// Change the UI
self.nextButton.enabled=NO;

// Validate number
RMPhoneFormat *fmt = [[RMPhoneFormat alloc] init];
NSString *phoneNumber = self.phoneTextField.text;
BOOL valid = [fmt isPhoneNumberValid:phoneNumber];
if (valid) {
// Save number in backend
User *theUser = [User theUser];
theUser.phoneNumber = phoneNumber;
[theUser patchUserWithParameter:@{@"phone":phoneNumber}];

// Send to Appsee
[Appsee addEvent:@"Phone saved"];

[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(phoneUpdatedInBackend) name:@"UserUpdatedInBackend" object:nil];
} else {
[self phoneNumberNotValid];
}
}

- (void)phoneNumberNotValid
{
UIAlertView *error = [[UIAlertView alloc] initWithTitle:@"Invalid Phone Number"
message:@"Your phone number is not valid. Call us if you need help"
delegate:self
cancelButtonTitle:@"Retry"
otherButtonTitles:@"Call support", nil];
[error show];
Mixpanel *mixpanel = [Mixpanel sharedInstance];
[mixpanel track:@"Phone number error"];

}

- (void)alertView:(UIAlertView *)alert clickedButtonAtIndex:(NSInteger)buttonIndex {
if (buttonIndex == 1) {
NSUserDefaults* userDefaults=[NSUserDefaults standardUserDefaults];
NSString* supportNumber =[userDefaults objectForKey:@"support_number"];
NSString *phoneNumber = [supportNumber stringByReplacingOccurrencesOfString:@" " withString:@""];
NSString *phoneURL = [NSString stringWithFormat:@"tel:%@",phoneNumber];
[[UIApplication sharedApplication] openURL:[NSURL URLWithString:phoneURL]];
}
}

- (void)phoneUpdatedInBackend
{
User *theUser = [User theUser];
if (theUser.isCustomer) {
AppDelegate *appDelegate = [UIApplication sharedApplication].delegate;
[appDelegate userLoggedIn];
} else {
[self performSegueWithIdentifier:@"ShowAddPayment" sender:nil];
}
}

@end
34 changes: 34 additions & 0 deletions MVC/AppDelegate.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
//
// AppDelegate.h
// MVC
//
// Created by Hamza Ouazzani Chahdi on 05/02/2014.
// Copyright (c) 2014 Hamza Ouazzani Chahdi. All rights reserved.
//

#import <UIKit/UIKit.h>
#import <FacebookSDK/FacebookSDK.h>
#import "LoginViewController.h"
#import "AddPhoneViewController.h"
#import "AddPaymentViewController.h"
#import "User.h"
#import "SWRevealViewController.h"
#import "StartViewController.h"



@interface AppDelegate : UIResponder <UIApplicationDelegate>

@property (strong, nonatomic) UIWindow *window;
@property (strong, nonatomic) UINavigationController* navController;
@property (strong, nonatomic) SWRevealViewController* revealVC;
@property (strong, nonatomic) LoginViewController *loginViewController;
@property (strong, nonatomic) NSDictionary *userParameters;
@property (strong, nonatomic) NSString *deviceToken;
@property (strong, nonatomic) User *user;
@property BOOL newSession;
@property BOOL readyToUpdate;

- (void)openFbSession;
- (void)userLoggedIn;
@end
Loading

0 comments on commit a72f19e

Please sign in to comment.