Save transaction id

This commit is contained in:
benji 2015-09-14 11:20:52 +02:00
parent 891e596fda
commit 02b27233cd
6 changed files with 55 additions and 40 deletions

View file

@ -2,12 +2,13 @@
#
# Table name: orders
#
# id :integer not null, primary key
# user_id :integer
# price_cents :integer
# created_at :datetime not null
# updated_at :datetime not null
# cancelled :boolean default("f")
# id :integer not null, primary key
# user_id :integer
# price_cents :integer
# created_at :datetime not null
# updated_at :datetime not null
# cancelled :boolean default("f")
# transaction_id :integer
#
require 'httparty'
@ -18,6 +19,7 @@ class Order < ActiveRecord::Base
has_many :order_items, dependent: :destroy
has_many :products, through: :order_items
before_validation :calculate_price
after_create :tab_api_created
default_scope -> { where(cancelled: false) }
@ -67,6 +69,10 @@ class Order < ActiveRecord::Base
private
def calculate_price
self.price_cents = price_cents
end
def tab_api_created
body = { transaction: { debtor: user.uid, cents: price_cents, message: to_sentence } }
tab_api body
@ -81,6 +87,7 @@ class Order < ActiveRecord::Base
def tab_api body
headers = { "Authorization" => "Token token=#{Rails.application.secrets.tab_api_key}" }
HTTParty.post("https://zeus.ugent.be/tab/transactions", body: body, headers: headers )
result = HTTParty.post("https://zeus.ugent.be/tab/transactions", body: body, headers: headers )
update_attribute(:transaction_id, JSON.parse(result.body)["id"])
end
end

View file

@ -15,7 +15,7 @@ development:
omniauth_client_id: tomtest
omniauth_client_secret: blargh
access_token: "token"
tab_api_key: ""
tab_api_key: "LNJxGqkM39O21gcJJq6BLQ=="
test:
secret_key_base: 961437e28e7d6055ffaad9cf1f8d614354f57f10cb2d7601c9d6ede72a03b9c9535ad9e63507e3eb31252c4895970a63117493408f2e9a46c7a0c4a5a7836b81

View file

@ -0,0 +1,5 @@
class AddTransactionToOrders < ActiveRecord::Migration
def change
add_column :orders, :transaction_id, :int
end
end

View file

@ -11,7 +11,7 @@
#
# It's strongly recommended that you check this file into your version control system.
ActiveRecord::Schema.define(version: 20150911195029) do
ActiveRecord::Schema.define(version: 20150914081940) do
create_table "delayed_jobs", force: :cascade do |t|
t.integer "priority", default: 0, null: false
@ -38,9 +38,10 @@ ActiveRecord::Schema.define(version: 20150911195029) do
create_table "orders", force: :cascade do |t|
t.integer "user_id"
t.integer "price_cents"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
t.boolean "cancelled", default: false
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
t.boolean "cancelled", default: false
t.integer "transaction_id"
end
add_index "orders", ["cancelled"], name: "index_orders_on_cancelled"
@ -49,41 +50,41 @@ ActiveRecord::Schema.define(version: 20150911195029) do
add_index "orders", ["user_id"], name: "index_orders_on_user_id"
create_table "products", force: :cascade do |t|
t.string "name", limit: 255, null: false
t.integer "price_cents", default: 0, null: false
t.string "name", null: false
t.integer "price_cents", default: 0, null: false
t.datetime "created_at"
t.datetime "updated_at"
t.string "avatar_file_name", limit: 255
t.string "avatar_content_type", limit: 255
t.string "avatar_file_name"
t.string "avatar_content_type"
t.integer "avatar_file_size"
t.datetime "avatar_updated_at"
t.integer "category", default: 0
t.integer "stock", default: 0, null: false
t.integer "category", default: 0
t.integer "stock", default: 0, null: false
t.integer "calories"
t.boolean "deleted", default: false
t.boolean "deleted", default: false
end
create_table "users", force: :cascade do |t|
t.datetime "created_at"
t.datetime "updated_at"
t.datetime "remember_created_at"
t.integer "sign_in_count", default: 0, null: false
t.integer "sign_in_count", default: 0, null: false
t.datetime "current_sign_in_at"
t.datetime "last_sign_in_at"
t.string "current_sign_in_ip", limit: 255
t.string "last_sign_in_ip", limit: 255
t.string "current_sign_in_ip"
t.string "last_sign_in_ip"
t.boolean "admin"
t.integer "dagschotel_id"
t.string "avatar_file_name", limit: 255
t.string "avatar_content_type", limit: 255
t.string "avatar_file_name"
t.string "avatar_content_type"
t.integer "avatar_file_size"
t.datetime "avatar_updated_at"
t.integer "orders_count", default: 0
t.boolean "koelkast", default: false
t.integer "orders_count", default: 0
t.boolean "koelkast", default: false
t.string "provider"
t.string "uid"
t.string "encrypted_password", default: "", null: false
t.boolean "private", default: false
t.string "encrypted_password", default: "", null: false
t.boolean "private", default: false
end
add_index "users", ["koelkast"], name: "index_users_on_koelkast"

View file

@ -2,12 +2,13 @@
#
# Table name: orders
#
# id :integer not null, primary key
# user_id :integer
# price_cents :integer
# created_at :datetime not null
# updated_at :datetime not null
# cancelled :boolean default("f")
# id :integer not null, primary key
# user_id :integer
# price_cents :integer
# created_at :datetime not null
# updated_at :datetime not null
# cancelled :boolean default("f")
# transaction_id :integer
#
require 'faker'

View file

@ -2,12 +2,13 @@
#
# Table name: orders
#
# id :integer not null, primary key
# user_id :integer
# price_cents :integer
# created_at :datetime not null
# updated_at :datetime not null
# cancelled :boolean default("f")
# id :integer not null, primary key
# user_id :integer
# price_cents :integer
# created_at :datetime not null
# updated_at :datetime not null
# cancelled :boolean default("f")
# transaction_id :integer
#
describe Order do