Permalink
Browse files

update to a working version of retrieving stored card information fro…

…m the vault
  • Loading branch information...
1 parent f1a5b07 commit fbd38a9612244301a2e098efbe797551c867ac7c @atmos committed Dec 28, 2008
@@ -1,2 +1,2 @@
class Application < Merb::Controller
-end
+end
@@ -1,20 +1,7 @@
class Payments < Application
before :ensure_authenticated
def index
- uri = Addressable::URI.parse(BRAINTREE[:query_api_url])
-# client = RFuzz::HttpClient.new(uri.host, 443, :use_ssl => true)
-# client.get(uri.path, :query => { 'username' => BRAINTREE[:username],
-# 'password' => BRAINTREE[:password],
-# 'customer_vault_id' => session.user.vault_tokens.first.token,
-# 'report_type' => 'customer_vault'})
-# req = Net::HTTP::Get.new(uri.path)
-# res = Net::HTTP.start(uri.host, 443) do |http|
-# http.request(req, {
-# 'username' => BRAINTREE[:username],
-# 'password' => BRAINTREE[:password],
-# 'customer_vault_id' => session.user.vault_tokens.first.token,
-# 'report_type' => 'customer_vault'})
-# end
+ @tokens = session.user.vault_tokens
render
end
@@ -25,6 +12,7 @@ def signup
def signup_response
@gateway_response = Braintree::GatewayResponse.new(params)
+ Merb.logger.info @gateway_response.inspect
case @gateway_response.response_status
when 'approved'
session.user.vault_tokens.create(:token => @gateway_response.customer_vault_id)
@@ -1,5 +1,24 @@
module Merb
module GlobalHelpers
- # helpers defined here available to all views.
+ def braintree_query(query_params)
+ uri = Addressable::URI.parse(BRAINTREE[:query_api_url])
+
+ server = Net::HTTP.new(uri.host, 443)
+ server.use_ssl = true
+
+ resp = server.start do |http|
+ req = Net::HTTP::Post.new(uri.path)
+ req.set_form_data(query_params.merge({
+ 'username' => BRAINTREE[:username],
+ 'password' => BRAINTREE[:password]}))
+ http.request(req)
+ end
+ case resp
+ when Net::HTTPSuccess, Net::HTTPRedirection
+ result = LibXML::XML::Document.string(resp.body)
+ else
+ resp.error!
+ end
+ end
end
end
@@ -1,5 +1,11 @@
module Merb
module PaymentsHelper
+ def card_holders_name(card_record)
+ "#{card_record.first_name} #{card_record.last_name}"
+ end
+ def card_billing_address(card_record)
+ "#{card_record.address_1} (#{card_record.postal_code})"
+ end
end
-end # Merb
+end # Merb
@@ -3,8 +3,7 @@ class CardRecord
attr_reader :first_name, :last_name, :address_1, :postal_code, :cc_number
def initialize(doc)
- @doc = LibXML::XML::Document.string(doc)
- customer = @doc.find('/nm_response/customer_vault/customer').first
+ customer = doc.find('/nm_response/customer_vault/customer').first
%w(first_name last_name address_1 postal_code cc_number).each do |k|
value = customer.find(k).first.content
instance_variable_set("@#{k}", value)
@@ -1,9 +1,14 @@
class VaultToken
include DataMapper::Resource
-
+ attr_accessor :card_info
+
property :id, Serial
property :token, String, :nullable => false
belongs_to :user
# validates_is_unique :token, :scope => [:user_id]
+ def card_info
+ params = { 'customer_vault_id' => token, 'report_type' => 'customer_vault'}
+ @card_info ||= CardRecord.new(braintree_query(params))
+ end
end
@@ -1,10 +1,19 @@
%h2 Hey, here's what we know about your account
%hr
%h3 Cards Setup
-- unless session.user.vault_tokens.empty?
- %ul
- - session.user.vault_tokens.each do |vt|
- %li= vt.inspect
+- unless @tokens.empty?
+ %table
+ %thead
+ %tr
+ %th Name
+ %th Address
+ %th Number
+ %tbody
+ - @tokens.each do |vt|
+ %tr
+ %td= card_holders_name(vt.card_info)
+ %td= card_billing_address(vt.card_info)
+ %td= vt.card_info.cc_number
%a{:href => url(:signup)} Add another?
- else
%p
View
@@ -27,5 +27,7 @@
dependency 'rcov', '>0.0', :require_as => nil
-dependency 'nokogiri', '=1.0.7'
-dependency 'webrat', '=0.3.2'
+dependency 'nokogiri', '=1.0.7', :require_as => nil
+dependency 'webrat', '=0.3.2', :require_as => nil
+dependency 'mongrel', '>1.0', :require_as => nil
+dependency 'libxml-ruby', '=0.9.7', :require_as => 'libxml'
View
@@ -1,6 +1,7 @@
# Go to http://wiki.merbivore.com/pages/init-rb
require 'config/dependencies.rb'
require 'digest/md5'
+require 'net/https'
require 'pp'
use_orm :datamapper
@@ -130,3 +130,22 @@ input {
display: block;
margin: 2px;
}
+table {
+ margin: 3px;
+ border: 1px solid #000;
+}
+table tr {
+ border: none;
+ padding: 0;
+ margin: 0;
+}
+table th {
+ border: none;
+ padding: 0px 7px;
+ margin: 0;
+}
+
+table td {
+ padding: 1px 7px;
+ border-top: 1px solid #000;
+}

0 comments on commit fbd38a9

Please sign in to comment.