使用Parse的Cloud Code保存条带客户详细信息

时间:2014-12-20 00:47:39

标签: ios objective-c parse-platform stripe-payments cloud-code

我正在使用Parse作为我的后端,并尝试集成Stripe以允许客户保存他们的付款信息以供将来付款。

客户未被保存到Stripe。

我认为这与我的Cloud Code上的网址有关(" https://" +" REMOVED SECRET KEY" +':@' ; +" api.stripe.com/v1" +" / customers /" + request.params.customerId +  " /卡/" + request.params.cardId,)

这是我的iOS代码

- (IBAction)save:(id)sender {
    if (![self.paymentView isValid]){
        return;
    }

    if (![Stripe defaultPublishableKey]) {
        UIAlertView *message = [[UIAlertView alloc] initWithTitle:@"No Publishable Key"
                                                          message:@"Please specify a Stripe Publishable Key in Constants.m"
                                                         delegate:nil
                                                cancelButtonTitle:NSLocalizedString(@"OK", @"OK")
                                                otherButtonTitles:nil];
        [message show];
        return;
    }

    PTKCard* card = self.paymentView.card;

    NSLog(@"Card last4: %@", card.last4);
    NSLog(@"Card expiry: %lu/%lu", (unsigned long)card.expMonth, (unsigned long)card.expYear);
    NSLog(@"Card cvc: %@", card.cvc);

    [[NSUserDefaults standardUserDefaults] setValue:card.last4 forKey:@"card.last4"];
    [self.navigationController popViewControllerAnimated:YES];
}

- (void)hasError:(NSError *)error {
    UIAlertView *message = [[UIAlertView alloc] initWithTitle:NSLocalizedString(@"Error", @"Error")
                                                      message:[error localizedDescription]
                                                     delegate:nil
                                            cancelButtonTitle:NSLocalizedString(@"OK", @"OK")
                                            otherButtonTitles:nil];
    [message show];
}

typedef void (^STPCardCompletionBlock)(STPCard *card,NSError *error);

+ (void)addTokenId:(NSString *)tokenId toCustomerId:(NSString *)customerId completion:(STPCardCompletionBlock)handler {
    [PFCloud callFunctionInBackground:@"stripeUpdateCustomer" withParameters:@{@"customerId":customerId,@"data":@{@"card":tokenId}} block:^(id object, NSError *error) {
        handler([[STPCard alloc]initWithAttributeDictionary:object],error);
    }];
}

+ (void)createCustomerFromCard:(NSString *)tokenId completion:(PFIdResultBlock)handler {
    [PFCloud callFunctionInBackground:@"createCustomer"
                       withParameters:@{@"tokenId":tokenId, }
                                block:^(id object, NSError *error) {
                                    //Object is an NSDictionary that contains the stripe customer information, you can use this as is, or create an instance of your own customer class
                                    handler(object,error);
                                }];
}

这是我的Cloud Code

Parse.Cloud.define("stripeUpdateCustomer", function(request, response) {
    Stripe.Customers.update(
        request.params["customerId"],
        request.params["data"], {
            success:function(results) {
                console.log(results["id"]);
                response.success(results);
            },
            error:function(error) {
                response.error("Error:" +error); 
            }
        }
    );
});

Parse.Cloud.define("stripeDeleteCardFromCustomer", function(request, response)  {
    Stripe.initialize(REMOVED SECRET KEY);
    Parse.Cloud.httpRequest({
        method:"DELETE",
        //STRIPE_SECRET_KEY will be your stripe secrect key obviously, this is different from the public key that you will use in your iOS/Android side.
        // STRIPE_API_BASE_URL = 'api.stripe.com/v1'
        url: "https://" + "REMOVED SECRET KEY" + ':@' + "api.stripe.com/v1" + "/customers/" + request.params.customerId + "/cards/" + request.params.cardId,
        success: function(httpResponse) {
          response.success(httpResponse.text);
        },
        error: function(httpResponse) {
          response.error('Request failed with response code ' + httpResponse.status);
        }
    });
});

Parse.Cloud.define("chargeToken",function(request,response) {
    Stripe.initialize(REMOVED SECRET KEY);
    Stripe.Charges.create(
        request.params,
        {
            success:function(results)
            {
                response.success(results);
            },
            error:function(error)
            {
                response.error("Error:" +error); 
            }
        }
    );
});

0 个答案:

没有答案