简体   繁体   English

Stripe API(PHP)和解析

[英]Stripe API (PHP) & Parse

I am building an app on iOS in swift and I am accepting payments using stripe. 我正在iOS上快速构建应用程序,并且接受使用Stripe的付款。 I have created a form that creates a customer and upon a successful save I call back the customerID and save it into my Parse Database. 我已经创建了一个创建客户的表单,成功保存后,我会回调customerID并将其保存到我的Parse数据库中。

Below is the code for the above: 下面是上面的代码:

XCODE: XCODE:

    let myCustomerID = user!.valueForKey("secretID") as! String
    self.sendTokenToServer(myCustomerID, myAmount: cost)

    func sendTokenToServer(aCustomer: String, myAmount: String) {
    var theamount = myAmount
    //  SEND REQUEST TO PHP FILE IN WEBSERVER
    let url = NSURL(string: ".../cutomeragain.php")
    var request = NSMutableURLRequest(URL: url!)
    request.HTTPMethod = "POST"
    let body = "custID=\(aCustomer)&amount=\(theamount)"
    request.HTTPBody = body.dataUsingEncoding(NSUTF8StringEncoding, allowLossyConversion: true)

    //  SEND ASYNCHORNOUS REQUEST
    NSURLConnection.sendAsynchronousRequest(request, queue: NSOperationQueue.mainQueue()) {
        (response, data, error) -> Void in
        if error == nil {
        }
     }

When I go to charge the customer by ID, it does not accept the charge. 当我通过ID向客户收费时,它不接受收费。 Below is the PHP code: 以下是PHP代码:

    <?php

    require_once('stripe-php/init.php');

    \Stripe\Stripe::setApiKey('xxx');

    // Get the credit card details submitted by the form
    $amount = $_POST['amount'];
    $customerId = $_POST['custID'];
    $cutomer = \Stripe\Customer::retrieve('custID');

    // Charge the Customer instead of the card
    $charge = \Stripe\Charge::create(array(
    "amount" => $amount, 
    "currency" => "usd",
    "customer" => $customerId)
    );

    // create a json output with completion variable, (this will be read from    the ios app as response)
    $json = array(
    'completion' => 'done',
    'completion1' => $cutomerId,
    'ok' => $cutomer-id
    );
    echo json_encode($json);

    ?>

Am I missing something in my PHP file? 我的PHP文件中缺少什么吗? I've tried a number of different methods. 我尝试了许多不同的方法。 Ie retrieving the customer from Stripe using 即使用以下方法从Stripe检索客户

Ok so I figured it out. 好的,所以我知道了。 I had to play around a bit, however, I think that someone else will find this useful. 我不得不尝试一下,但是,我认为其他人会发现这很有用。

In my xcode file I changed 在我的xcode文件中,我更改了

    let myCustomerID = user!.valueForKey("secretID") as! String 

to

    let myCustomerID = user?["secretID"] as? String

In my PHP file I changed the code to the following: 在我的PHP文件中,将代码更改为以下代码:

    // Get the credit card details submitted by the form
    $amount = $_POST['amount'];
    $customerId = $_POST['cus'];
    $cu = \Stripe\Customer::retrieve($customerId);

    // Charge the Customer instead of the card
    $charge = \Stripe\Charge::create(array(
    "amount" => $amount, 
    "currency" => "usd",
    "customer" => $customerId,)
    );

    // create a json output with completion variable, (this will be read from      the ios app as response)
   $json = array(
   'completion' => 'done',
   'completion1' => $cutomerId,
   'ok' => $cutomer-id
   );
   echo json_encode($json);

   ?>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM