gpt4 book ai didi

curl - 来自 strip curl 的 NSUrlRequest

转载 作者:行者123 更新时间:2023-12-01 05:39:10 26 4
gpt4 key购买 nike

我需要按照以下说明发出 http post 请求:

curl https://api.stripe.com/v1/tokens \
-u sk_test_BQokikJOvBiI2HlWgH4olfQ2: \
-d "bank_account[country]=US" \
-d "bank_account[routing_number]=110000000" \
-d "bank_account[account_number]=000123456789"

我不知道如何从 curl 转到 NSUrlrequest,尤其是 -u(用户?)部分。 stripe SDK 已将其从他们的 SDK 中删除,并且在他们的网站上没有示例。

谢谢

最佳答案

编辑:我专门为获取 bank_account 的 token 创建了另一个答案。这个答案通常是关于如何使用创建收件人的示例使用解析的后端进行调用。

stipe 文档在这里有点偏离,创建 bank_account token 的调用实际上是直接从应用程序使用可发布 key 进行的。确保不要在 iOS 应用程序本身中使用您的 key 。只能通过以下方式使用您的公钥:

[Stripe setDefaultPublishableKey:@"pk_test_your_test_key_here"];

您需要使用网络后端来实现 Stripe 支付系统的完整功能。他们包含的 ios sdk 只能让您从信用卡中获取 token 。 Web 后端在那里你可以实现 key 。我使用 parse.com 作为我的 stripe 后端,但许多实现了他们自己的。

Stripe ios Tutorial ios stripe backend

下面是一个简单的 httpRequest 云代码,可以执行大多数 stripe 任务。为它提供方法、前缀、后缀、后缀,然后是请求的参数。我并不是说这是实现 stripe 的 httpRequests 的最佳方式,但它涵盖了您开始使用它的基础。下面的代码已经过测试,并且可以正常工作,我在我的 strip 测试沙箱中创建了一个 john doe 收件人。

解析云代码:

var Stripe = require('stripe');
var STRIPE_SECRET_KEY = 'sk_test_yoursecretkeyhere';
var STRIPE_API_BASE_URL = 'api.stripe.com/v1/'
Stripe.initialize(STRIPE_SECRET_KEY);

Parse.Cloud.define("stripeHTTPRequest", function(request, response)
{
//check for suffix, and postfix
var suffix = "";
if (!isEmpty(request.params["suffix"])) {
suffix = '/'+request.params['suffix'];
}
var postfix = "";
if (!isEmpty(request.params["postfix"])) {
postfix = '/'+request.params['postfix'];
}

Parse.Cloud.httpRequest({
method: request.params["method"],
url: 'https://' + STRIPE_SECRET_KEY + ':@' + STRIPE_API_BASE_URL + request.params["prefix"] + suffix + postfix,
params:request.params["parameters"],
success: function(httpResponse) {
response.success(httpResponse.text);
},
error: function(httpResponse) {
response.error('Request failed with response code' + httpResponse.status);
}
});
});

function isEmpty(obj) {

// null and undefined are "empty"
if (obj == null) return true;

// Assume if it has a length property with a non-zero value
// that that property is correct.
if (obj.length > 0) return false;
if (obj.length === 0) return true;

// Otherwise, does it have any properties of its own?
// Note that this doesn't handle
// toString and valueOf enumeration bugs in IE < 9
for (var key in obj) {
if (hasOwnProperty.call(obj, key)) return false;
}

return true;
}

因此,当涉及到创建收件人时,您可以为其提供“POST”方法和“收件人”前缀,并将后缀和后缀留空。这将生成这样的 url:

https://sk_test_yoursecretkeyhere:@api.stripe.com/v1/recipients

除了方法和 pre/suf/postfix 之外,您还需要为其提供参数。您可以通过发送键控对象字典来做到这一点。使用 Stripe 的文档,让我们创建一个名为 john doe 的收件人:

   -d "name=John Doe" \
-d type=individual \
-d tax_id=000000000 \
-d "email=test@example.com" \
-d "description=Recipient for John Doe"

这里是使用李四示例的云代码的 iOS 调用。我已经实现了一个通用方法,你传递方法、pre/suf/postfix 和参数。然后,我创建了许多其他方法来处理特定的 strip 调用,例如创建收件人。

ViewController.m

-(void)createJohnDoe
{
NSDictionary *parameters = @{@"name":@"John Doe",
@"type":@"individual",
@"tax_id":@"000000000",
@"email":@"test@example.com",
@"description":@"Recipient for John Doe"
};
[ELStripe executeStripeCloudCodeWithMethod:@"POST" prefix:@"recipients" suffix:nil postfix:nil parameters:parameters completionHandler:^(id jsonObject, NSError *error) {
//jsonObject will be a dictionary that would need be parsed into your recipient object
NSLog(@"jsonObject:%@",jsonObject);
}];
}

ELStripe.m

   //Completion Handler Definition.
typedef void (^ELStripeCompletionBlock)(id jsonObject, NSError *error);

+(void)executeStripeCloudCodeWithMethod:(NSString *)method prefix:(NSString *)prefix suffix:(NSString *)suffix postfix:(NSString *)postfix parameters:(NSDictionary *)parameters completionHandler:(ELStripeCompletionBlock)handler
{

[PFCloud callFunctionInBackground:@"stripeHTTPRequest" withParameters:@{@"method":method, @"prefix":prefix?prefix:@"", @"suffix":suffix?suffix:@"", @"postfix":postfix?postfix:@"", @"parameters":parameters} block:^(id object, NSError *error) {
id jsonObject;
if (!error) {
NSError *jsonError = nil;
jsonObject = [NSJSONSerialization JSONObjectWithData:[object dataUsingEncoding:NSUTF8StringEncoding] options:kNilOptions error:&jsonError];
}
handler(jsonObject,error);
}];
}

关于curl - 来自 strip curl 的 NSUrlRequest,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24875772/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com