NSUrlRequest from curl for stripe [英] NSUrlRequest from curl for stripe

查看:274
本文介绍了NSUrlRequest from curl for stripe的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要使用以下说明发出http post请求:

I need to make a http post request using the following instructions:

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(user?)部分。

I have no idea how to go from curl to NSUrlrequest, especially the -u (user?) part. The stripe SDK has left this out from their SDK and has no example on their site.

感谢

推荐答案

编辑:我创建了另一个答案,专门用于获取bank_account的令牌。这个答案一般是如何使用parse的后端使用创建收件人的示例来打电话。

I created another answer specifically for getting a token for a bank_account. This answer was for generally how to make a call using parse's back end using an example of creating a recipient.

stipe文档在这里有点儿, bank_account令牌实际上是使用可发布的键直接从应用程序。确保不要在iOS应用中使用您的密钥。只有您的公钥应通过使用:

The stipe documentation is a little off here, the call for creating a bank_account token is actually made using the publishable key directly from the app. Make sure not to use your secret key in the iOS app itself. Only your public key should be used via:

[Stripe setDefaultPublishableKey:@"pk_test_your_test_key_here"];

您需要使用网络后端才能实现条带支付系统的完整功能。它们包括的ios sdk只让你从信用卡获得令牌。 web后端在那里你将实现秘密密钥。我使用parse.com作为我的后端的条纹,但很多实现自己的。

You need to use a web back end to implement the complete functionality of the stripe payment system. The ios sdk they include only gets you as far as getting a token from a credit card. The web back end is there you would implement the secret key. I use parse.com as my backend for stripe but many implement their own.

Stripe ios教程

下面是一个简单的httpRequest云代码,可以执行大多数条带任务。给它一个方法,前缀,后缀,后缀,然后请求的参数。我不是说这是实现条带的httpRequests的最好方法,但它涵盖了你开始工作的基础。

Below is a simple httpRequest cloud code that can perform most stripe tasks. Feed it a method, prefix, suffix, postfix, and then the parameters of the request. I'm not saying this is the best way to implement stripe's httpRequests, but it covers the bases for you to start working on it. The code below is tested, and it works, I created a john doe recipient in my stripe test sandbox.

解析云代码:

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,前缀recipients,并留下后缀,后缀为空。这将生成以下网址:

So when it comes to creating a recipient, you would feed it a method of "POST", and a prefix of "recipients", and leave the suffix, and postfix empty. This would generate a url of such:

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

除了方法& pre / suf / postfix你需要喂它的参数。你可以通过发送键控对象的字典来做到这一点。使用Stripe的文档,创建一个名为john doe的收件人:

In addition to the method & pre/suf/postfix you would need to feed it parameters. You can do this by sending a dictionary of keyed objects. Using Stripe's documentation, lets create a recipient named 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调用的云代码使用John Doe示例。我已经实现了一个通用的方法,你传递的方法,pre / suf / postfix的参数。

Here is the iOS call of the cloud code using the John Doe example. I have implemented a general method the you pass the method, pre/suf/postfix's and parameters. I then create many additional methods to handle the specific stripe calls, like creating a recipient for example.

ViewController.m

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

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);
    }];
}

这篇关于NSUrlRequest from curl for stripe的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆