簡體   English   中英

將Stripe與iOS和PHP集成

[英]Integration of Stripe with iOS and PHP

我是iOS編程的新手。

我正在嘗試將Stripe集成到我的iOS app

它給了我以下錯誤。

首先拋出調用堆棧:(0 CoreFoundation
0x037c1946 異常預處理+ 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:completion:] + 314 5 ValenX 0x0034612e + [條帶createTokenWithCard:publishableKey:completion:] + 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:to:from:forEvent:] + 99 10 UIKit 0x01aea1cf-[UIApplication sendAction:toTarget:fromTarget:fromSender:forEvent:] + 64 11 UIKit
0x01c1de86-[UIControl sendAction:to:forEvent:] + 69 12 UIKit
0x01c1e2a3-[UIControl _sendActionsForEvents:withEvent:] + 598 13 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 0x036da248 __CFRunLoopRun + 952 22 CoreFoundation
0x036d9bcb CFRunLoopRunSpecific + 443 23 CoreFoundation
0x036d99fb CFRunLoopRunInMode + 123 24圖形服務
0x0510624f GSEventRunModal + 192 25圖形服務
0x0510608c GSEventRun + 104 26 UIKit
0x01ae88b6 UIApplicationMain + 1526 27 ValenX
0x000cf0ce top_level_code + 78 28 ValenX
0x000cf10b主+ 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周內一直堅持使用。 任何幫助表示贊賞。

問候阿南德

能否請您在Stripe.m下提供可發布的密鑰,您可以從Stripe->帳戶設置-> API中獲取可發布的密鑰

靜態NSString * defaultKey =

由於我使用了以4242開頭的測試卡,因此收到“付款不成功”的警告消息

希望這可以幫助。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM