使用Rails 5和剥离gem。我正在使用http://ngrok.io来使用Stripe Webhook。
这是 RegistrationsController 。
#include <string.h>
int main(int, char**)
{
strcpy(NULL, "Hello");
return 0;
}
注册模式
我在哪里定义了class RegistrationsController < ApplicationController
before_action :set_registration, only: [:show, :edit, :update, :destroy]
def index
@registrations = Registration.all
end
def show
end
def new
@registration = Registration.new
@registration.subscription = Subscription.find_by id: params["subscription_id"]
end
def create
@registration = Registration.new registration_params.merge(email: stripe_params["stripeEmail"],
card_token: stripe_params["stripeToken"])
raise "Please, check registration errors" unless @registration.valid?
@registration.process_payment
@registration.save
redirect_to @registration, notice: 'Registration was successfully created. Thank you!'
rescue Stripe::CardError => e
flash[:error] = e.message
render :new
end
# POST /registrations/hook
protect_from_forgery except: :webhook
def webhook
event = Stripe::Event.retrieve(params["id"])
case event.type
when "invoice.payment_succeeded" #renew subscription
Registration.find_by_customer_id(event.data.object.customer).renew
end
render status: :ok, json: "success"
end
private
def stripe_params
params.permit :stripeEmail, :stripeToken
end
def set_registration
@registration = Registration.find(params[:id])
end
def registration_params
params.require(:registration).permit(:subscription_id, :full_name, :company, :telephone, :email, :card_token, :end_date, :customer_id)
end
end
moethod。
process_payment
这是注册表
class Registration < ApplicationRecord
belongs_to :subscription
def process_payment
customer_data = {email: email, source: card_token}
.merge((subscription.plan.blank?)? {}: {plan: subscription.plan})
customer = Stripe::Customer.create(customer_data)
Stripe::Charge.create customer: customer.id,
amount: (subscription.price * 100).to_i,
description: subscription.name,
currency: 'CAD'
customer_id = customer.id
end
def renew
update_attibute :end_date, Date.today + 1.month
end
end
付款完成后,Stripe会反映所有正确的元素,例如customer_id,end_dat,stripeToken,....我的本地服务器上有一个问题,它没有显示所有customer_id和end_date。
这是终端输出
create_table "registrations", force: :cascade do |t|
t.bigint "subscription_id", null: false
t.string "full_name"
t.string "company"
t.string "email"
t.string "telephone"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
t.string "card_token"
t.date "end_date"
t.string "customer_id"
t.index ["subscription_id"], name: "index_registrations_on_subscription_id"
end
令我困惑的是Started POST "/registrations" for 2607:fea8:571f:f at 2017-10-17
Cannot render console from 2607::ee6f! Allowed networks:
Processing by RegistrationsController#create as HTML
Parameters: {"utf8"=>"✓", "authenticity_token"=>"XXXXXX==", "registration"=>{"subscription_id"=>"1", "full_name"=>"NAME", "company"=>"COMPANY", "telephone"=>"9999999"}, "stripeToken"=>"tok_XXXCCCC7777777", "stripeTokenType"=>"card", "stripeEmail"=>"NAME@mail.com"}
Unpermitted parameters: :utf8, :authenticity_token, :registration, :stripeTokenType
Unpermitted parameters: :utf8, :authenticity_token, :registration, :stripeTokenType
Subscription Load (0.3ms) SELECT "subscriptions".* FROM "subscriptions" WHERE "subscriptions"."id" = $1 LIMIT $2 [["id", 1], ["LIMIT", 1]]
(0.2ms) BEGIN
SQL (0.5ms) INSERT INTO "registrations" ("subscription_id", "full_name", "company", "email", "telephone", "created_at", "updated_at", "card_token") VALUES ($1, $2, $3, $4, $5, $6, $7, $8) RETURNING "id" [["subscription_id", 1], ["full_name", "NAME"], ["company", "COMPANY"], ["email", "NAME@mail.com"], ["telephone", "9999999"], ["created_at", "2017-10-17"], ["updated_at", "2017-10-17"], ["card_token", "tok_XXXXXXXX"]]
(1.2ms) COMMIT
Redirected to http://1fc4a17c.ngrok.io/registrations/2
Completed 302 Found in 2775ms (ActiveRecord: 2.2ms)
和end_data
是nil,而条带处理事务成功并显示每个元素的值。
customer_id
我在这里错过任何东西吗?
先谢谢。