Skip to content

Commit

Permalink
WIP: paypal_response
Browse files Browse the repository at this point in the history
  • Loading branch information
Jacqui Maher authored and veezus committed Apr 14, 2009
1 parent 4ab0376 commit 3bd00e0
Show file tree
Hide file tree
Showing 14 changed files with 145 additions and 7 deletions.
5 changes: 5 additions & 0 deletions app/controllers/myspot/purchases_controller.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
class Myspot::PurchasesController < ApplicationController
include HTTParty

before_filter :login_required
ssl_required :create, :new
Expand Down Expand Up @@ -33,6 +34,10 @@ def create
end
end

def paypal_response
HTTParty.post(PAYPAL_POST_URL, params.merge({"cmd" => "_notify_validate"}))
end

protected

def unpaid_donations_required
Expand Down
3 changes: 3 additions & 0 deletions app/models/paypal_transaction.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
class PaypalTransaction < ActiveRecord::Base
validates_presence_of :txn_id
end
2 changes: 0 additions & 2 deletions app/models/purchase.rb
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,6 @@
class Purchase < ActiveRecord::Base
class GatewayError < RuntimeError; end

PAYPAL_EMAIL = "info@spot.us"

attr_accessor :credit_card_number, :credit_card_year, :credit_card_month,
:credit_card_type, :verification_value
attr_reader :credit_card
Expand Down
6 changes: 4 additions & 2 deletions app/views/myspot/purchases/new.html.haml
Original file line number Diff line number Diff line change
Expand Up @@ -50,15 +50,17 @@
.span-595.box_yellow.create_a_tip_form
Use PayPal to fund a donation with money in your PayPal account, or to pay with a credit card.
.float-right
- form_tag "https://www.paypal.com/cgi-bin/webscr" do
- form_tag PAYPAL_POST_URL do
= hidden_field_tag "cmd", "_cart"
= hidden_field_tag "upload", "1"
= hidden_field_tag "business", Purchase::PAYPAL_EMAIL
= hidden_field_tag "business", PAYPAL_EMAIL
- i = 1
- @donations.each do |donation|
= hidden_field_tag "item_number_#{i}", "#{donation.id}"
= hidden_field_tag "item_name_#{i}", "PITCH: #{donation.pitch.headline}"
= hidden_field_tag "amount_#{i}", donation.amount
- i += 1
= hidden_field_tag "item_number_#{i}", "#{@purchase.spotus_donation.id}"
= hidden_field_tag "item_name_#{i}", "Support Spot.Us"
= hidden_field_tag "amount_#{i}", @purchase.spotus_donation[:amount]
= image_submit_tag "buy_now.gif"
Expand Down
1 change: 1 addition & 0 deletions config/environment.rb
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
config.gem "rspec-rails", :lib => false, :version => "= 1.2.2"
config.gem "cucumber", :lib => false, :version => "= 0.1.12"
config.gem "webrat", :lib => false, :version => "= 0.3.2"
config.gem "httparty", :version => "=0.4.2"

config.time_zone = 'UTC'

Expand Down
2 changes: 2 additions & 0 deletions config/environments/development.rb
Original file line number Diff line number Diff line change
Expand Up @@ -32,3 +32,5 @@
end
end

PAYPAL_POST_URL = "https://www.sandbox.paypal.com/cgi-bin/webscr"
PAYPAL_EMAIL = "dev+pa_1239041752_biz@hashrocket.com"
2 changes: 2 additions & 0 deletions config/environments/production.rb
Original file line number Diff line number Diff line change
Expand Up @@ -17,3 +17,5 @@
:password => APP_CONFIG[:gateway][:password],
})
end
PAYPAL_POST_URL = "https://www.paypal.com/cgi-bin/webscr"
PAYPAL_EMAIL = "info@spot.us"
3 changes: 3 additions & 0 deletions config/environments/test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -25,3 +25,6 @@
ActiveMerchant::Billing::Base.mode = :test
Purchase.gateway = ActiveMerchant::Billing::BogusGateway.new
end

PAYPAL_POST_URL = "https://www.sandbox.paypal.com/cgi-bin/webscr"
PAYPAL_EMAIL = "dev+pa_1239041752_biz@hashrocket.com"
2 changes: 1 addition & 1 deletion config/routes.rb
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@
myspot.resources :pitches, :member => {:accept => :put}
myspot.resources :posts
myspot.resources :pledges
myspot.resources :purchases
myspot.resources :purchases, :collection => {:paypal_response => :post}
myspot.resources :tips
end

Expand Down
16 changes: 16 additions & 0 deletions db/migrate/20090414205424_create_paypal_transactions.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
class CreatePaypalTransactions < ActiveRecord::Migration
def self.up
create_table :paypal_transactions do |t|
t.string :txn_id
t.integer :purchase_id
t.string :status
t.text :paypal_response

t.timestamps
end
end

def self.down
drop_table :paypal_transactions
end
end
83 changes: 83 additions & 0 deletions spec/controllers/myspot/purchases_controller_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -208,6 +208,89 @@ def do_create
post :create, :purchase => Factory.attributes_for(:purchase)
end
end

describe "PayPal response" do
before do
@params = {"protection_eligibility"=>"Eligible",
"tax"=>"0.00",
"payment_status"=>"Completed",
"address_name"=>"Test User",
"business"=>"dev+pa_1239041752_biz@hashrocket.com",
"address_country"=>"United States",
"address_city"=>"San Jose",
"payer_email"=>"dev+pa_1239041717_per@hashrocket.com",
"receiver_id"=>"HQ79EJ4KFPWAU",
"residence_country"=>"US",
"payment_gross"=>"440.00",
"mc_shipping"=>"0.00",
"receiver_email"=>"dev+pa_1239041752_biz@hashrocket.com",
"mc_gross_1"=>"200.00",
"address_street"=>"1 Main St",
"mc_handling1"=>"0.00",
"verify_sign"=>"AVv2fa0VdjSSQ6PhgLCpIgZ3PpygAdpa6bB9AXSMUbqY5xLKMkkniUrh",
"mc_gross_2"=>"200.00",
"address_zip"=>"95131",
"mc_handling2"=>"0.00",
"item_name1"=>"PITCH: testing testing",
"txn_type"=>"cart",
"mc_currency"=>"USD",
"mc_gross_3"=>"40.00",
"transaction_subject"=>"Shopping Cart",
"charset"=>"windows-1252",
"address_country_code"=>"US",
"mc_handling3"=>"0.00",
"txn_id"=>"0PB07920SY555151E",
"item_name2"=>"PITCH: testing testing",
"item_number1"=>"",
"notify_version"=>"2.8",
"payer_status"=>"verified",
"tax1"=>"0.00",
"address_state"=>"CA",
"payment_fee"=>"13.06",
"item_name3"=>"Support Spot.Us",
"quantity1"=>"1",
"address_status"=>"confirmed",
"item_number2"=>"",
"payment_date"=>"13:16:29 Apr 14, 2009 PDT",
"mc_handling"=>"0.00",
"mc_fee"=>"13.06",
"tax2"=>"0.00",
"quantity2"=>"1",
"item_number3"=>"",
"first_name"=>"Test",
"num_cart_items"=>"3",
"mc_shipping1"=>"0.00",
"tax3"=>"0.00",
"payment_type"=>"instant",
"quantity3"=>"1",
"test_ipn"=>"1",
"mc_gross"=>"440.00",
"payer_id"=>"XABGXAPTSL7QS",
"mc_shipping2"=>"0.00",
"last_name"=>"User",
"custom"=>"",
"mc_shipping3"=>"0.00"}
end

describe "when the payment has been completed" do
it "posts to the paypal url with an additional cmd parameter" do
HTTParty.expects(:post).with(PAYPAL_POST_URL, @params.merge({"cmd" => "_notify_validate"}))
post :paypal_response, @params
end
end

describe "when a payment was been successful" do
it "creates a purchase"
it "marks donations paid"
it "marks the spot-us donation paid"
end

describe "when a payment was unsuccessful" do
it "does not mark donations paid"
it "does not mark the spot-us donation paid"
end
end

requires_login_for :get, :new
requires_login_for :post, :create

Expand Down
13 changes: 13 additions & 0 deletions spec/fixtures/paypal_transactions.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
# Read about fixtures at http://ar.rubyonrails.org/classes/Fixtures.html

one:
txn_id: MyString
purchase_id: 1
status: MyString
paypal_response: MyText

two:
txn_id: MyString
purchase_id: 1
status: MyString
paypal_response: MyText
10 changes: 10 additions & 0 deletions spec/models/paypal_transaction_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
require File.expand_path(File.dirname(__FILE__) + '/../spec_helper')

describe PaypalTransaction do

describe "validations" do
it "requires a transaction id" do
PaypalTransaction.new.should have(1).error_on(:txn_id)
end
end
end
4 changes: 2 additions & 2 deletions spec/views/myspot/purchases/new.html.haml_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,10 @@
it "has a Pay with PayPal section" do
do_render
template.should have_tag("h3", "Pay With PayPal")
template.should have_tag("form[action=?]", "https://www.paypal.com/cgi-bin/webscr") do
template.should have_tag("form[action=?]", PAYPAL_POST_URL) do
with_tag("input[name='cmd'][value=?]", "_cart")
with_tag("input[name='upload'][value=?]", "1")
with_tag("input[name='business'][value=?]", Purchase::PAYPAL_EMAIL)
with_tag("input[name='business'][value=?]", PAYPAL_EMAIL)
end
end
it "includes all donations as line items" do
Expand Down

0 comments on commit 3bd00e0

Please sign in to comment.