【问题标题】:Swift/Stripe error communicating with the Parse Cloud Code与 Parse Cloud 代码通信的 Swift/Stripe 错误
【发布时间】:2015-08-21 03:22:24
【问题描述】:

我已经工作了好几天,试图弄清楚如何使用 Swift 以 Parse.com Cloud Code 作为后端来对卡进行收费并将卡保存给客户。我成功地将 Stripe pod 与我的项目集成,并且我创建了一个令牌,并在控制台中打印出来以验证它的存在。但我不能用它做任何事情!我到处寻找答案,但无法弄清楚为什么我不断出错。我认为这与我试图提供给 Cloud Code 的参数有关,但我不确定。我已经阅读了 Cloud Code 和 Stripe 的文档,但无济于事。这是我的 PaymentViewController.swift:

import UIKit
import Stripe
import PaymentKit
import Parse
import Bolts




class PaymentViewController: UIViewController, PTKViewDelegate {


@IBOutlet weak var saveBtn: UIButton!

var paymentView: PTKView = PTKView()

override func viewDidLoad() {
    super.viewDidLoad()



    var view : PTKView = PTKView(frame: CGRectMake(15,20,290,55))

    paymentView = view
    paymentView.delegate = self;
    self.view.addSubview(self.paymentView)

    saveBtn.enabled = false



}

func paymentView(view: PTKView!, withCard card: PTKCard!, isValid valid: Bool) {
    if (valid) {
        saveBtn.enabled = true
    } else {
        saveBtn.enabled = false
    }
}



override func didReceiveMemoryWarning() {
    super.didReceiveMemoryWarning()
    // Dispose of any resources that can be recreated.
}





@IBAction func save(sender: AnyObject) {

    var card: STPCard = STPCard()
    card.number = self.paymentView.card.number
    card.expMonth = self.paymentView.card.expMonth
    card.expYear = self.paymentView.card.expYear
    card.cvc = self.paymentView.card.cvc

    STPAPIClient.sharedClient().createTokenWithCard(card, completion: { (tokenId: STPToken?, error: NSError?) -> Void in
        if (error != nil) {
            println(error)
            println("what the..")
        } else {
            println(tokenId)


            PFCloud.callFunctionInBackground("hello", withParameters: nil) {
                (response: AnyObject?, error: NSError?) -> Void in
                let responseString = response as? String
                println(responseString)
            }




            PFCloud.callFunctionInBackground("createCharge", withParameters: nil, block: { (success: AnyObject?, error: NSError?) -> Void in
                if error != nil {
                    println("error")
                }
            })








        }
    })



}







@IBAction func cancel(sender: AnyObject) {
    self.dismissViewControllerAnimated(true, completion: nil)
}






}

我添加了“Hello World”示例以查看云代码是否设置正确,以及 callFunction 是否有效。我的云代码是:

var Stripe = require('stripe');
Stripe.initialize('My_Secret_Key');

Parse.Cloud.define("hello", function(request, response) {
               response.success("Hello world!");
               });

Parse.Cloud.define("createCharge", function(request, response) {
               Stripe.Charges.create({
                                     amount: 100 * 10, // $10 expressed in cents
                                     currency: "usd",
                                     card: "tok_3TnIVhEv9P24T0"
                                     },{
                                     success: function(httpResponse) {
                                     response.success("Purchase made!");
                                     },
                                     error: function(httpResponse) {
                                     response.error("Uh oh, something went wrong");
                                     }
                                     });


               });

任何帮助将不胜感激!我一直在不知疲倦地解决这个问题!控制台打印出来

Uh oh, something went wrong (Code: 141, Version: 1.7.2)

【问题讨论】:

    标签: swift parse-platform stripe-payments parse-cloud-code


    【解决方案1】:
    class PaymentViewController: UIViewController, PTKViewDelegate {
    
    
    @IBOutlet weak var saveBtn: UIButton!
    
    var paymentView: PTKView = PTKView()
    
    override func viewDidLoad() {
        super.viewDidLoad()
    
    
    
        var view : PTKView = PTKView(frame: CGRectMake(15,20,290,55))
    
        paymentView = view
        paymentView.delegate = self;
        self.view.addSubview(self.paymentView)
    
        saveBtn.enabled = false
    
    
    
    }
    
    func paymentView(view: PTKView!, withCard card: PTKCard!, isValid valid: Bool) {
        if (valid) {
            saveBtn.enabled = true
        } else {
            saveBtn.enabled = false
        }
    }
    
    
    
    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }
    
    
    
    
    
    @IBAction func save(sender: AnyObject) {
    
        var card: STPCard = STPCard()
        card.number = self.paymentView.card.number
        card.expMonth = self.paymentView.card.expMonth
        card.expYear = self.paymentView.card.expYear
        card.cvc = self.paymentView.card.cvc
    
        STPAPIClient.sharedClient().createTokenWithCard(card, completion: { (token: STPToken?, error: NSError?) -> Void in
            if (error != nil) {
                println(error)
                println("not working")
            } else {
                //println(tokenId)
                var coin = token!.tokenId
    
    
                PFCloud.callFunctionInBackground("hello", withParameters: nil) {
                    (response: AnyObject?, error: NSError?) -> Void in
                    let responseString = response as? String
                    println(responseString)
                }
    
                var name = PFUser.currentUser()?.username as String!
                var customer = PFUser.currentUser()?.objectId as String!
    
    
                PFCloud.callFunctionInBackground("createCustomer", withParameters: ["coin" : coin, "name": name, "customer": customer], block: { (success: AnyObject?, error: NSError?) -> Void in
                    if error != nil {
                        println("create customer not working")
                    }
    
    
    
    
                })
    
                var customerId = customer!
    
                PFCloud.callFunctionInBackground("createCharge", withParameters: ["customerId" : customerId], block: { (success: AnyObject?, error: NSError?) -> Void in
                    if error != nil {
                        println("not working")
                    }
                })
    
    
    
    
    
    
    
    
            }
        })
    
    
    
    }
    
    
    
    @IBAction func cancel(sender: AnyObject) {
        self.dismissViewControllerAnimated(true, completion: nil)
    }
    

    }

    我更新的云代码在这里:

    var Stripe = require('stripe');
    Stripe.initialize('sk_test_xxxxxxxxxxxxxx');
    
    Parse.Cloud.define("hello", function(request, response) {
                   response.success("Hello world!");
                   });
    
    
    
    Parse.Cloud.define("createCustomer", function(request, response) {
                                      Stripe.Customers.create({
                                                             card:     request.params.coin,
                                                              account_balance: -10*100,
                                                              metadata: {
                                                              name: request.params.name,
                                                              customer: request.params.customer, // e.g PFUser object ID
                                                              }
    
                                                             }, {
                                                             success: function(customer) {
                                                             response.success(customer.id);
                                                             },
                                                             error: function(error) {
                                                             response.error("Error:" +error);
                                                             }
                                                             })
                                      });
    

     

    Parse.Cloud.define("createCharge", function(request, response) {
                   Stripe.Charges.create({
                                         amount: 100 * 10, // $10 expressed in cents
                                         currency: "usd",
                                         //card: request.params.coin
                                         customer: request.params.customerId
                                         },{
                                         success: function(httpResponse) {
                                         response.success("Purchase made!");
                                         },
                                         error: function(httpResponse) {
                                         response.error(httpResponse)
                                         response.error("Uh oh, something went wrong");
                                         }
                                         });
    
    
                   });
    

    最终我希望在不同的 viewController.swift 文件中创建客户并在应用程序的另一部分收费,但现在我正在同一个 PaymentViewController.swift 文件中与他们一起测试它

    【讨论】:

    • 现在你正在做一大堆时髦的东西。首先,无需为客户设置账户余额。这仅对订阅有用。客户创建成功了吗?
    • 哈哈,是的,抱歉,我添加了账户余额和其他东西,以测试我是否正确地做事,以及它是否会显示在我的 Stripe 仪表板上,确实如此。客户已成功创建,但我似乎无法向该客户收费。
    • 在 Stripe 仪表板中,您应该可以查看日志,您是否看到 /v1/charges 的任何 4XX(其中 XX 是 00 或 02)?
    • 不,他们只是 200 个 POST/v1/客户。没有 4XX 或 v1/收费
    【解决方案2】:

    您看到的错误Uh oh, something went wrong (Code: 141, Version: 1.7.2) 表示您的 createCharge Parse 函数返回了错误。您可能需要记录 httpResponse 的值以从 Stripe 中找到确切的错误。

    查看您的代码最有可能出现错误:Invalid Request Error: Cannot use token tok_3TnIVhEv9P24T0 more than once。您可以在 Stripe 仪表板的日志部分确认这一点。

    我看到您打印了令牌println(tokenId),您还希望将其发送到您的 Parse 函数并将卡设置为等于您刚刚创建的令牌的值。

    【讨论】:

    • 您好,感谢您的回复。我试过你说的,我得到了这个响应[错误]:错误:无法在 Object.Stripe.Charges.create.error 的 Object.error (:106:9) 处多次调用成功/错误( main.js:19:51) 在 Object.Parse.Cloud.httpRequest.error (stripe.js:107:19) 在 Object. (:578:19) (代码:141,版本:1.7 .4)我不确定为什么仍然会发生这种情况。有什么想法吗?
    • 嗯,这是一个奇怪的错误,这表明您正在尝试多次调用 response.success / error。你的云功能改变了多少?
    • 所以,我设法弄清楚如何让令牌条带化。我添加了 var token = token!.tokenId 并将 ["token" : token] 作为参数传入。但是,我现在正在尝试创建客户,并使用 parse objectId 保存它,并且在尝试将客户传递到 createCharge 参数时遇到与上述相同的错误。你想让我发布我的代码给你看吗?
    • 好的,所以您要创建费用并创建客户?
    • 是的,我想创建客户,然后根据客户创建费用,这样用户就不必每次都输入他们的抄送信息。我会把它贴在下面
    猜你喜欢
    • 2015-12-24
    • 1970-01-01
    • 2016-11-12
    • 1970-01-01
    • 2015-08-01
    • 1970-01-01
    • 2016-01-23
    • 1970-01-01
    • 2019-03-06
    相关资源
    最近更新 更多