2014-11-09 78 views
0

我是iOS編程的新手。Stripe與iOS和PHP的集成

我正在嘗試將Stripe整合到我的iOS app

它給我以下錯誤。

第一擲調用堆棧:(0的CoreFoundation
0x037c1946 exceptionPreprocess + 182 1 libobjc.A.dylib
0x03075a97 objc_exception_throw + 44 2的CoreFoundation
0x037c186d + [NSException提高:格式:] + 141 3 ValenX
0x00343c63 + [條紋validateKey:] + 163 4 ValenX
0x0034539a + [條紋 createTokenWithCard:publishableKey:operationQueue:完成:] + 314 5 ValenX 0x0034612e + [條紋 CRE ateTokenWithCard:publishableKey:完成:] + 222 6 ValenX
0x00345fff + [條紋createTokenWithCard:完成:] + 175 7 ValenX 0x000cf59f - [V_BuyStripeVC saveButtonAction:] + 879 8
libobjc.A.dylib 0x0308b7cd - [NSObject的 performSelector :withObject:withObject:] + 84 9的UIKit
0x01aea23d - [UIApplication的sendAction:爲:從:forEvent:] + 99 10 的UIKit 0x01aea1cf - [UIApplication的 sendAction:toTarget:fromSender:forEvent:] + 64 11的UIKit
0x01c1de86 - [UIControl sendAction:to:forEvent:] + 69 12 UIKit
0x01c1e2a3 - [UIControl _sendActionsForEvents:withEvent:] + 598 1 3 UIKit的0x01c1d50d - [UIControl touchesEnded:withEvent:方法] + 660 14的UIKit
0x01b3a60a - [一個UIWindow _sendTouchesForEvent:] + 874 15的UIKit
0x01b3b0e5 - [一個UIWindow的SendEvent:] + 791 16的UIKit
0x01b00549 - [UIApplication的的SendEvent :] + 242 17的UIKit
0x01b1037e _UIApplicationHandleEventFromQueueEvent + 20690 18的UIKit 0x01ae4b19 _UIApplicationHandleEventQueue + 2206 19的CoreFoundation
0x036e51df __CFRUNLOOP_IS_CALLING_OUT_TO_A_SOURCE0_PERFORM_FUNCTION
+ 15 20的CoreFoundation 0x036daced __CFRunLoopDoSources0 + 253 21的CoreFoundation 0x036d A248 __CFRunLoopRun + 952 22的CoreFoundation
0x036d9bcb CFRunLoopRunSpecific + 443 23的CoreFoundation
0x036d99fb CFRunLoopRunInMode + 123個24 GraphicsServices
0x0510624f GSEventRunModal + 192個25 GraphicsServices
0x0510608c GSEventRun + 104 26的UIKit
0x01ae88b6 UIApplicationMain + 1526 27 ValenX
0x000cf0ce top_level_code + 78 28 ValenX
0x000cf10b main + 43 29 libdyld.dylib
0x04105ac9 start + 1)libC++ abi.dylib:終止未捕獲 異常類型NSException

以下是代碼。

#import <AFNetworking/AFNetworking.h> 
#import "Stripe.h" 
#import "V_BuyStripeVC.h" 
#import "PTKView.h" 
#import "STPToken.h" 

#define STRIPE_TEST_PUBLIC_KEY @"pk_test_vh4FFxERgMGtcs344RkWEfpC" 
#define STRIPE_TEST_POST_URL @"http://s547905537.onlinehome.us/ValenX/serverauth.json" 

@interface V_BuyStripeVC()<PTKViewDelegate> 
@property(weak, nonatomic) PTKView *paymentView; 
@property (strong, nonatomic) IBOutlet UIButton *saveButton; 
@end 

@implementation V_BuyStripeVC 

- (IBAction)saveButtonAction:(UIButton *)sender { 

    STPCard *card = [[STPCard alloc] init]; 
    //STPToken *token = [[STPToken alloc]init]; 
    card.number = self.paymentView.card.number; 
    card.expMonth = self.paymentView.card.expMonth; 
    card.expYear = self.paymentView.card.expYear; 
    card.cvc = self.paymentView.card.cvc; 

    [Stripe createTokenWithCard:card completion:^(STPToken *token, NSError *error) { 
     if (error) { 
      [self handleError:error]; 
     } else { 
      [self postStripeToken:token]; 
     } 
    }]; 

} 


- (void)postStripeToken:(STPToken*)token { 

    //1 
    /* 
    NSURL *postURL = [NSURL URLWithString:STRIPE_TEST_POST_URL]; 
    AFHTTPClient* httpClient = [AFHTTPClient clientWithBaseURL:postURL]; 
    httpClient.parameterEncoding = AFJSONParameterEncoding; 
    [httpClient registerHTTPOperationClass:[AFJSONRequestOperation class]]; 
    [httpClient setDefaultHeader:@"Accept" value:@"text/json"]; 
    */ 

    //2 
    // RWCheckoutCart* checkoutCart = [RWCheckoutCart sharedInstance]; 
    NSInteger totalCents = 20000; 

    //3 
    NSMutableDictionary* postRequestDictionary = [[NSMutableDictionary alloc] init]; 
    postRequestDictionary[@"stripeAmount"] = [NSString stringWithFormat:@"%d", totalCents]; 
    postRequestDictionary[@"stripeCurrency"] = @"usd"; 
    postRequestDictionary[@"stripeToken"] = token; 
    postRequestDictionary[@"stripeDescription"] = @"Purchase from FoodApp iOS app!"; 

    //4 
    AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager]; 
    [manager POST:@"http://s547905537.onlinehome.us/ValenX/serverauth.php" parameters:postRequestDictionary success:^(AFHTTPRequestOperation *operation, id responseObject) { 
     [self chargeDidSucceed]; 
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) { 
     [self chargeDidNotSuceed]; 
    }]; 

    self.saveButton.enabled = YES; 
} 

- (void)viewDidLoad 
{ 
    [super viewDidLoad]; 
    PTKView *view = [[PTKView alloc] initWithFrame:CGRectMake(15,20,290,55)]; 
    self.paymentView = view; 
    self.paymentView.delegate = self; 
    [self.view addSubview:self.paymentView]; 
} 

- (void)paymentView:(PTKView *)view withCard:(PTKCard *)card isValid:(BOOL)valid 
{ 
    // Toggle navigation, for example 
    self.saveButton.enabled = valid; 
} 

- (void)handleError:(NSError *) error { 

    //1 
    if ([error.domain isEqualToString:@"StripeDomain"]) { 
     UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Error" 
                 message:[error localizedDescription] 
                 delegate:nil 
               cancelButtonTitle:@"OK" 
               otherButtonTitles:nil]; 
     [alert show]; 
    } 

    //2 
    else { 
     UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Error" 
                 message:@"Please try again" 
                 delegate:nil 
               cancelButtonTitle:@"OK" 
               otherButtonTitles:nil]; 
     [alert show]; 
    } 

    self.saveButton.enabled = YES; 
} 

- (void)chargeDidSucceed { 

    UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Success" 
                message:@"Please enjoy your meal." 
                delegate:nil 
              cancelButtonTitle:@"OK" 
              otherButtonTitles:nil]; 
    [alert show]; 

    //Send confirmation email 
    //RWEmailManager* emailManager = [[RWEmailManager alloc] initWithRecipient:self.nameTextField.text 
                   //recipientEmail:self.emailTextField.text]; 

    //[emailManager sendConfirmationEmail]; 

    //RWCheckoutCart* checkoutCart = [RWCheckoutCart sharedInstance]; 
    //[checkoutCart clearCart]; 

    [self.navigationController popToRootViewControllerAnimated:YES]; 
} 

- (void)chargeDidNotSuceed { 
    //2 
    UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Payment not successful" 
                message:@"Please try again later." 
                delegate:nil 
              cancelButtonTitle:@"OK" 
              otherButtonTitles:nil]; 
    [alert show]; 
} 

@end 

我堅持這個去年1周。任何幫助表示讚賞。

問候, 阿南德

+0

你可以請你的項目添加到github併發布鏈接。 – rshankar 2014-11-09 04:17:16

+0

我建議你重新格式化你可怕的格式錯誤信息。將它放在代碼塊中應該使其可讀。 – 2015-10-19 23:59:44

回答

0

能否請您儘量給Stripe.m下發布的關鍵,你可以從條紋發佈的關鍵 - >帳戶設置 - > API

靜態的NSString * defaultKey =

由於我使用了以4242開頭的測試卡,因此我收到一條警告消息,提示「付款不成功」

希望這有助於您。