[英]Store credit card using Stripe + Parse with cloud code
我正在嘗試將用戶的信用卡存儲到條帶中。 制作完令牌后,我嘗試將用戶的令牌保存到Stripe作為客戶。 但是我沒有找到問題的答案,我只需要將卡存儲到已經存在的用戶即可。
我嘗試使用方法Stripe.Customers.update,但是它存儲新卡,如果用戶有,則刪除“默認”卡。 並使用Stripe.Customers.create方法使用新卡創建新客戶,但我需要存儲在特定用戶中。
雲代碼:
Parse.Cloud.define("stripeCreateCard", function(request,response)
{
Stripe.initialize(STRIPE_SECRET_KEY);
Stripe.Customers.create
(
request.params,
{
success:function(results)
{
response.success(results);
},
error:function(error)
{
response.error("Error:" +error);
}
}
);
});
Parse.Cloud.define("stripeUpdateCustomer", function(request, response)
{
Stripe.initialize(STRIPE_SECRET_KEY);
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);
}
}
);
});
iOS代碼:
class func getParamsForAddingCardToCustomer(custormerId: String, cardToken: String) -> NSDictionary {
let params = NSMutableDictionary()
params.setObject(["card" : cardToken], forKey: "data")
params.setObject(custormerId, forKey: "customerId")
return params
}
var params = ParamsHelper.getParamsForAddingCardToCustomer(stripeId, cardToken: token)
PFCloud.callFunctionInBackground("stripeCreateCard", withParameters: params as [NSObject : AnyObject]) {
(response: AnyObject?, error: NSError?) -> Void in
let responseString = response as? String
if (error === nil) {
println("Response: \(responseString) ")
}
else if (error != nil) {
println("Error: \(error) \(error!.userInfo)")
}
}
我嘗試了幾種參數來按需存儲卡,但是總是收到錯誤消息“收到未知參數”
任何人都知道如何在不刪除或創建新客戶的情況下存儲卡?
解析的帶區實現不盡如人意。 在許多情況下,您必須使用HTTPRequest來執行Parse不提供的條帶功能。
對於這些情況,我使用以下iOS方法和CloudCode執行我所有的帶區HTTPRequest。 遵循條紋CURL API文檔時,編碼非常容易。 然后,我構建此方法來執行大多數條帶化任務,例如IE創建/更新/刪除客戶,卡,費用等。
首先,根據要獲取,創建/更新或刪除條帶對象,為它提供一個方法,即GET,POST,DELETE。
然后,我將可選的pre / suf / postfix組合提供給它,以創建一個url。
創建的示例URL:
最后,在創建卡並將其添加到客戶的情況下,我給它提供參數,這只需要是一個包含tokenID的字典。
+(void)executeStripeCloudCodeWithMethod:(NSString *)method
prefix:(NSString *)prefix
suffix:(NSString *)suffix
postfix:(NSString *)postfix
secondPostfix:(NSString *)secondPostfix
parameters:(NSDictionary *)params
completionHandler:(ELStripeCompletionBlock)handler
{
NSDictionary *parameters = @{@"method":method,
@"prefix":prefix?prefix:@"",
@"suffix":suffix?suffix:@"",
@"postfix":postfix?postfix:@"",
@"secondPostfix":secondPostfix?secondPostfix:@"",
@"params":params?params:[NSNull null]
};
[PFCloud callFunctionInBackground:@"stripeHTTPRequest"
withParameters:parameters
block:^(id object, NSError *error) {
id jsonObject;
if (!error) {
NSError *jsonError = nil;
//Turn the json string into an NSDictionary
jsonObject = [NSJSONSerialization JSONObjectWithData:[object dataUsingEncoding:NSUTF8StringEncoding]
options:kNilOptions error:&jsonError];
}
handler(jsonObject,error);
}];
}
執行的雲代碼:
var STRIPE_SECRET_KEY = 'sk_test_your_test_code_here';
var STRIPE_API_BASE_URL = 'api.stripe.com/v1/'
Parse.Cloud.define("stripeHTTPRequest", function(request, response)
{
//Check for valid pre/suf/postfixes, if they are not there do not include them.
var prefix = request.params["prefix"];
var suffix = "";
var postfix = "";
var secondPostfix = "";
if (!isEmpty(request.params["suffix"])) suffix = '/'+request.params['suffix'];
if (!isEmpty(request.params["postfix"])) postfix = '/'+request.params['postfix'];
if (!isEmpty(request.params["secondPostfix"])) secondPostfix = '/'+request.params['secondPostfix'];
Parse.Cloud.httpRequest(
{
method: request.params["method"],
//Create URL from base url and pre/suf/postfixes
url: 'https://'+STRIPE_API_BASE_URL + prefix + suffix + postfix + secondPostfix,
headers: {
'Authorization': "Bearer " + STRIPE_SECRET_KEY
},
params:request.params["params"],
success: function(httpResponse)
{
//response text is a json dictionary
response.success(httpResponse.text);
},
error: function(httpResponse)
{
response.error(httpResponse.text);
}
});
});
使用上面的方法,我可以創建單獨的方法來執行我需要的大多數條紋任務。
這是一個示例,它將創建一個新卡並將其附加到客戶的Stripe Card創建API
+ (void)createCardFromToken:(NSString *)tokenId customerId:(NSString *)customerId completionHandler:(ELCardCompletionBlock)handler
{
[ELStripe executeStripeCloudCodeWithMethod:@"POST" //I use post here because we are creating a card. POST would also be used for updating a customer/card or refunding a charge for example
prefix:@"customers" //If you look at the documentation and the example URL I use "customers" here as the prefix
suffix:customerId //The customerID is the suffix, this will be the customer you are going to add the card to
postfix:@"cards" //I believe this is "sources" now
secondPostfix:nil //Not needed for this URL
parameters:@{
@"card":tokenId //Only parameter is a tokenId, and I wrap this inside an NSDictionary
}
completionHandler:^(id jsonObject, NSError *error) {
if (error)
{
//Handle the error code here
handler(nil,rejectError);
return;
}
//If no error stripe returns a dictionary containing the card information. You can use this information to create a card object if so desired.
handler([ELCard cardFromDictionary:jsonObject],error);
}];
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.