【问题标题】:iOS integration with Heroku backendiOS 与 Heroku 后端的集成
【发布时间】:2016-10-31 05:24:42
【问题描述】:

我在这方面遇到了很多困难,我只是看不出哪里出错了。虽然我是新手,但我正在尝试做一些应用程序开发人员必须成功完成的事情 - 通过 Stripe 的示例 Sinatra / Ruby 后端处理 Apple Pay 费用:

https://github.com/stripe/example-ios-backend

我相信我做得很好:

我的 Stripe 帐户已设置,我处于测试模式,我已验证我的银行,一切正常。

我正在使用测试 API 密钥、机密和可发布:以下是我稍作修改的 web.ry 文件中的代码:如果您转到:@987654322,您可以看到该应用至少在“/”端点处工作@ - 请注意,我完全不知道我在 Rails 中所做的事情 - 我制作的唯一模组是:

我已插入 Stripe 测试密钥 我稍微修改了初始返回消息以检查 Heroku 上的更改是否正在更新 我把货币换成了英镑(条带账户是英镑)

代码是:

require 'sinatra'
require 'stripe'
require 'dotenv'
require 'json'

Dotenv.load

Stripe.api_key = ENV['sk_test_mystripetestkeymystripetestkey']

get '/' do
    status 200
    return "Great, terrific, your backend is set up. Now you can configure the Stripe example iOS apps to point here."
end

post '/charge' do

    # Get the credit card details submitted by the form
    source = params[:source] || params[:stripe_token] || params[:stripeToken]
    customer = params[:customer]

    # Create the charge on Stripe's servers - this will charge the user's card
    begin
        charge = Stripe::Charge.create(
                                       :amount => params[:amount], # this number should be in cents
                                       :currency => "gbp",
                                       :customer => customer,
                                       :source => source,
                                       :description => "Example Charge"
                                       )
                                       rescue Stripe::StripeError => e
                                       status 402
                                       return "Error creating charge: #{e.message}"
    end

    status 200
    return "Charge successfully created"

end

get '/customers/:customer/cards' do

    customer = params[:customer]

    begin
        # Retrieves the customer's cards
        customer = Stripe::Customer.retrieve(customer)
        rescue Stripe::StripeError => e
        status 402
        return "Error retrieving cards: #{e.message}"
    end

    status 200
    content_type :json
    cards = customer.sources.all(:object => "card")
    selected_card = cards.find {|c| c.id == customer.default_source}
    return { :cards => cards.data, selected_card: selected_card }.to_json

end

post '/customers/:customer/sources' do

    source = params[:source]
    customer = params[:customer]

    # Adds the token to the customer's sources
    begin
        customer = Stripe::Customer.retrieve(customer)
        customer.sources.create({:source => source})
        rescue Stripe::StripeError => e
        status 402
        return "Error adding token to customer: #{e.message}"
    end

    status 200
    return "Successfully added source."

end

post '/customers/:customer/select_source' do

    source = params[:source]
    customer = params[:customer]

    # Sets the customer's default source
    begin
        customer = Stripe::Customer.retrieve(customer)
        customer.default_source = source
        customer.save
        rescue Stripe::StripeError => e
        status 402
        return "Error selecting default source: #{e.message}"
    end

    status 200
    return "Successfully selected default source."

end

delete '/customers/:customer/cards/:card' do

    card = params[:card]
    customer = params[:customer]

    # Deletes the source from the customer
    begin
        customer = Stripe::Customer.retrieve(customer)
        customer.sources.retrieve(card).delete()
        rescue Stripe::StripeError => e
        status 402
        return "Error deleting card"
    end

    status 200
    return "Successfully deleted card."

end

如您所见,Ruby 网络应用运行良好……

切换到 Xcode,然后我去下载 Stripe 的示例应用程序:https://github.com/stripe/stripe-ios/tree/master/Example

我在“stripe-ios-master”文件夹中打开“Stripe.xcworkspace”。

在我的 Apple Developer 帐户中,我设置了一个新应用“com.AH.thenameigaveit”Merchant

我勾选了 ApplePay,然后设置了商家标识符:merchant.com.AH.thenameigaveit

我去 Stripe 并创建一个 CSR 证书,然后我将它与我在 Apple Dev 中的商家 ID 关联起来。

我获得了新的 Apple CER 文件并将其上传到 Stripe - 我能够验证它是否已成功安装。

回到 Xcode 中,我为配置文件问题点击了修复问题,这一切都被接受了。

我的权利文件显示“com.apple.developer.in-app-payments”

现在在 Xcode 中,我转到 Stripe 的“简单”示例中的 ViewController.swift 并更改各种用户变量的值,如下所示:

stripePublishableKey = “'pk_test_mystripetestkeymystripetestkey'” 我的测试可发布密钥

backendChargeURLString = “https://ibidurubypay.herokuapp.com

appleMerchantId = “merchant.com.AH.thenameigaveit”

请注意 Stripe 'Simple' 示例 Swift 代码将货币称为 USD,而我的 web.ry 脚本引用“gbp”,但是从过去我每次尝试几次时可以看出,看起来好像没关系,因为货币值没有传递给 Ruby 脚本(而且我的 Stripe 帐户是以英镑为单位的。)

因此,是时候构建并运行 iPhone 6 plus 并使用实时 Visa 信用卡在其上激活 Apple Pay……应用程序首次成功构建..

第一次显示:“付款请求无效:检查您的权利”,所以我转到 Capabilities 并确保根据 ApplePay 检查了正确的商家 ID - 再次构建,这次我们更进一步 - Apple Pay 屏幕完美地出现在屏幕上,指定与 Apple Pay 关联的信用卡支付 10 美元购买这件酷衬衫。-我触摸了一下,它出现了“正在处理付款”,然后在“付款未完成!”和 Apple Pay 之后不久对话框退回到屏幕下方,返回“购买衬衫”选项

所以,我对与此服务器后端的通信一无所知,除了尝试收集从调用中返回的一些信息,所以我插入:

print ("data: \(data), \r\r response: \(response), \r\r error: \(error)")

这给了我:

数据:可选(),

响应:可选({ URL: *(ASBEFORE)*ibidurubypay.herokuapp.com/charge } { 状态码:402,标题 { 连接=“保持活动”; “内容长度”= 248; “内容类型”=“文本/html;字符集=utf-8”; 日期 =“2016 年 6 月 28 日星期二 00:16:43 GMT”; 服务器 = "WEBrick/1.3.1 (Ruby/2.1.2/2014-05-08)"; Via = "1.1 维格"; “X-Content-Type-Options”= nosniff; “X-Frame-Options”=SAMEORIGIN; “X-Xss-保护”=“1;模式=块”; } }),

错误:无

这似乎是卡拒绝消息,但卡没有问题。

在我在门户的 Stripe 帐户上,成功创建了一个令牌,时间:2016/06/28 01:16:42

……但是,我在这里的任何地方都没有看到 402 下降的证据,也许我没有找对地方……

如果有人能告诉我出了什么问题,我将不胜感激 - 我正在扯头发,这几天我断断续续地被这个问题难住了。

【问题讨论】:

  • 由于您是 Ruby 新手,我建议您阅读一些关于 byebug 的内容并使用它进行一些调试。您可以在控制器操作中放置断点,并确保从客户端发送正确的参数。在尝试将其连接到前端之前,请确保在 irb 中测试您的 Stripe::Charge.create 调用。它有助于单独测试代码片段以识别错误的真正罪魁祸首。

标签: ios ruby heroku applepay


【解决方案1】:

好的,所以我问了 Stripe 的人这个问题并让我大吃一惊,他们直接解决了 - 问题是:

Stripe.api_key = ENV['sk_test_mystripetestkeymystripetestkey'] 在我的 Ruby 脚本中...

要么:应该是:

Stripe.api_key = 'sk_test_mystripetestkeymystripetestkey'(注意没有 ENV[])

或者:更好:如果它最初是从 Stripe 下载的,这是一个环境变量,然后我在 Stripe 中设置了它,所以在脚本中:

Stripe.api_key = ENV['STRIPE_TEST_SECRET_KEY']

然后更新您的 Heroku 配置以创建一个名为 STRIPE_TEST_SECRET_KEY 的环境变量,并将您的测试 API 密钥作为值:https://devcenter.heroku.com/articles/config-vars

一旦你知道怎么做就简单了,感谢 Stripe 的大力支持!

【讨论】:

    猜你喜欢
    • 2014-11-26
    • 1970-01-01
    • 1970-01-01
    • 2018-12-06
    • 2019-07-24
    • 1970-01-01
    • 2019-07-01
    • 2017-09-24
    • 2012-05-25
    相关资源
    最近更新 更多