Skip to content

Commit

Permalink
Pecunix integration should be working
Browse files Browse the repository at this point in the history
  • Loading branch information
David FRANCOIS committed Feb 1, 2011
1 parent 7b8f708 commit 59d4648
Show file tree
Hide file tree
Showing 14 changed files with 120 additions and 23 deletions.
42 changes: 42 additions & 0 deletions app/controllers/third_party_callbacks_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -29,4 +29,46 @@ def lr_create_from_sci

render :text => ""
end

# Pecunix cancel callback
def px_cancel
flash[:error] = "Your Pecunix transfer was canceled"
redirect_to account_transfers_path
end

# Pecunix success redirection URL
def px_payment
redirect_to account_transfers_path,
:notice => "You successfully transferred #{params["PAYMENT_AMOUNT"]} PGAU to your account"
end

# Pecunix success callback
def px_status
config = YAML::load(File.read(File.join(Rails.root, "config", "pecunix.yml")))[Rails.env]

# Check that the payee account is correct
raise "Payee account was different than the configured one" unless (params["PAYEE_ACCOUNT"] == config["account"])

# Check that the payment units is GAU
raise "Wrong payment units" unless (params["PAYMENT_UNITS"] == "GAU")

# Check payment hash
px_data = %w{PAYEE_ACCOUNT PAYMENT_AMOUNT PAYMENT_UNITS PAYER_ACCOUNT PAYMENT_REC_ID PAYMENT_GRAMS PAYMENT_ID PAYMENT_FEE TXN_DATETIME}
px_hash = Digest::SHA1.hexdigest(px_data.map{ |i| i or "" }.join(":")).upcase
raise "Verification hash was wrong" unless (params["PAYMENT_HASH"] == px_hash)

# We want to make sure it is the first time the callback is called for this
# particular PGAU deposit (according to Pecunix docs, multiple calls are possible)
unless Transfer.find_by_px_tx_id(params["PAYMENT_REC_ID"])
Transfer.create!(
:user => User.find(params["PAYMENT_ID"]),
:currency => "PGAU",
:amount => params["PAYMENT_GRAMS"].to_f,
:px_tx_id => params["PAYMENT_REC_ID"],
:px_payer => params["PAYER_ACCOUNT"]
)
end

render :text => ""
end
end
7 changes: 5 additions & 2 deletions app/controllers/transfers_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,10 @@ def new
end

def pecunix_deposit_form
@pecunix_config = YAML::load(File.read(File.join(Rails.root, "config", "pecunix.yml")))[Rails.env]
@amount = params[:amount]
@payment_id = @current_user.id
@config = YAML::load(File.read(File.join(Rails.root, "config", "pecunix.yml")))[Rails.env]
@hash = Digest::SHA1.hexdigest("#{@config['account']}:#{@amount}:GAU:#{@payment_id}:PAYER:#{@config['secret']}").upcase
end

def create
Expand All @@ -19,7 +22,7 @@ def create
Transfer.transaction do
if @transfer.save
redirect_to account_transfers_path,
:notice =>"You successfuly transferred #{@transfer.amount.abs} #{@transfer.currency}"
:notice => "You successfuly transferred #{@transfer.amount.abs} #{@transfer.currency}"
else
render :action => :new
end
Expand Down
2 changes: 1 addition & 1 deletion app/models/trade_order.rb
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ class TradeOrder < ActiveRecord::Base

validates :currency,
:presence => true,
:inclusion => { :in => ["LRUSD", "LREUR", "EUR"] }
:inclusion => { :in => Transfer::CURRENCIES }

validates :category,
:presence => true,
Expand Down
2 changes: 1 addition & 1 deletion app/models/transfer.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
class Transfer < ActiveRecord::Base
MIN_BTC_CONFIRMATIONS = 5
CURRENCIES = ["LRUSD", "LREUR", "EUR", "BTC"]
CURRENCIES = ["LRUSD", "LREUR", "EUR", "BTC", "PGAU"]

attr_protected :skip_min_amount
attr_accessor :skip_min_amount
Expand Down
7 changes: 3 additions & 4 deletions app/views/accounts/show.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,9 @@
</td>
<td class="account-balance">
<%
btc_balance = @current_user.balance(:btc)
unconfirmed_btc_balance = @current_user.balance(:btc, :unconfirmed => true) - btc_balance
%>
btc_balance = @current_user.balance(:btc)
unconfirmed_btc_balance = @current_user.balance(:btc, :unconfirmed => true) - btc_balance
%>

<span class="<%= @current_user.balance(:btc) > 0 ? "green" : (@current_user.balance(:btc) < 0 ? "red" : "") %>">
<%= btc_balance > 0 ? "+" : (btc_balance < 0 ? "-" : "") %> <%= number_to_currency btc_balance.abs, :unit => "" %>
Expand Down Expand Up @@ -83,5 +83,4 @@
<%= @current_user.balance(:pgau) > 0 ? "+" : (@current_user.balance(:pgau) < 0 ? "-" : "") %> <%= number_to_currency @current_user.balance(:pgau).abs, :unit => "" %>
</td>
</tr>

</table>
11 changes: 5 additions & 6 deletions app/views/transfers/_px_sci_form.html.erb
Original file line number Diff line number Diff line change
@@ -1,22 +1,21 @@
<form id="pecunix-sci-form" action="https://pri.pecunix.com/money.refined" method="post">
<input type="hidden" name="PAYEE_ACCOUNT" value="<%= config["account"] %>" />
<input name="PAYMENT_AMOUNT" value="" />
<input name="PAYMENT_AMOUNT" value="<%= amount %>" />

<input type="hidden" name="PAYMENT_URL" value="<%= config["payment_url"] %>" />
<input type="hidden" name="PAYMENT_URL_METHOD" value="POST" />

<input type="hidden" name="NOPAYMENT_URL" value="<%= config["no_payment_url"] %>" />
<input type="hidden" name="NOPAYMENT_URL_METHOD" value="POST" />
<input type="hidden" name="NOPAYMENT_URL_METHOD" value="GET" />

<input type="hidden" name="STATUS_URL" value="<%= config["status_url"] %>" />
<input type="hidden" name="STATUS_TYPE" value="FORM" />

<input type="hidden" name="INPUT_HASH" value="F9834F5C15191E9DDF60CA783682E3D030E7F23F" />

<input type="hidden" name="INPUT_HASH" value="<%= hash %>" />

<input type="hidden" name="PAYMENT_UNITS" value="GAU" />
<input type="hidden" name="WHO_PAYS_FEES" value="PAYER" />
<input type="hidden" name="PAYMENT_ID" value="" />
<input type="hidden" name="PAYMENT_ID" value="<%= payment_id %>" />

<input type="hidden" name="SUGGESTED_MEMO" value="MEMO" />
<input type="hidden" name="SUGGESTED_MEMO" value="Transfer funds to account <%= @current_user.account %>" />
</form>
2 changes: 1 addition & 1 deletion app/views/transfers/deposit.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@

<%= form_tag pecunix_deposit_form_account_transfers_path, :method => :get, :remote => true do %>
<div class="form-field">
<%= label_tag :amount %>
<%= label_tag :amount, "Amount (PGAU)" %>
<%= text_field_tag :amount, nil %>
</div>

Expand Down
2 changes: 1 addition & 1 deletion app/views/transfers/pecunix_deposit_form.js.erb
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
$("div#pecunix-sci-form-container").html("<%= escape_javascript(render(:partial => "transfers/px_sci_form.html", :locals => { :config => @pecunix_config })).html_safe %>");
$("div#pecunix-sci-form-container").html("<%= escape_javascript(render(:partial => "transfers/px_sci_form.html", :locals => { :config => @config, :hash => @hash, :amount => @amount, :payment_id => @payment_id })).html_safe %>");
$("#pecunix-sci-form").submit();
12 changes: 6 additions & 6 deletions config/pecunix.yml
Original file line number Diff line number Diff line change
@@ -1,18 +1,18 @@
development:
account: test@bitcoin-central.net
account: support@bitcoin-central.net
secret: secret word is secret

# Pecunix POSTs here to confirm payment
status_url: https://bitcoin-central.net/third_party_callbacks/px_status
status_url: http://localhost:3030/third_party_callbacks/px_status

# Pecunix POSTs here from the users browser
payment_url: https://bitcoin-central.net/third_party_callbacks/px_payment
# Pecunix POSTs here from the users browser when payment succeeded
payment_url: http://localhost:3030/third_party_callbacks/px_payment

# Pecunix POSTs here from the browser if payment was cancelled
no_payment_url: https://bitcoin-central.net/third_party_callbacks/px_cancel
no_payment_url: http://localhost:3030/third_party_callbacks/px_cancel

test:
account: test@bitcoin-central.net
account: support@bitcoin-central.net
secret: secret word is secret
status_url: https://bitcoin-central.net/third_party_callbacks/px_status
payment_url: https://bitcoin-central.net/third_party_callbacks/px_payment
Expand Down
3 changes: 3 additions & 0 deletions config/routes.rb
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,9 @@
post :lr_create_from_sci
get :lr_transfer_success
get :lr_transfer_fail
get :px_cancel
get :px_payment
post :px_status
end
end

Expand Down
11 changes: 11 additions & 0 deletions db/migrate/20110201144233_add_pecunix_fields.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
class AddPecunixFields < ActiveRecord::Migration
def self.up
add_column :transfers, :px_tx_id, :string
add_column :transfers, :px_payer, :string
end

def self.down
remove_column :transfers, :px_tx_id
remove_column :transfers, :px_payer
end
end
4 changes: 3 additions & 1 deletion db/schema.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
#
# It's strongly recommended to check this file into your version control system.

ActiveRecord::Schema.define(:version => 20110126134715) do
ActiveRecord::Schema.define(:version => 20110201144233) do

create_table "invoices", :force => true do |t|
t.integer "payee_id"
Expand Down Expand Up @@ -77,6 +77,8 @@
t.string "lr_account_id"
t.integer "payee_id"
t.string "email"
t.string "px_tx_id"
t.string "px_payer"
end

create_table "users", :force => true do |t|
Expand Down
Binary file added public/images/PGAU.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
38 changes: 38 additions & 0 deletions test/functional/third_party_callbacks_controller_test.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
require 'test_helper'

class ThirdPartyCallbacksControllerTest < ActionController::TestCase
test "should create a pecunix deposit if nicely asked" do
params = {
"PAYEE_ACCOUNT" => "support@bitcoin-central.net",
"PAYMENT_AMOUNT" => "1000.00",
"PAYMENT_UNITS" => "GAU",
"PAYMENT_REC_ID" => "000014568",
"PAYER_ACCOUNT" => "customer@gold-cart.com",
"PAYMENT_HASH" => "EC764D0A216D875269B24003C48297037583BA1A",
"PAYMENT_GRAMS" => "1000.0000",
"PAYMENT_ID" => users(:trader1).id.to_s,
"PAYMENT_FEE" => "0.0002",
"TXN_DATETIME" => "2002-04-10 10:14:54",
"SUGGESTED_MEMO" => "Payment Bitcoin Central",
}

assert !Transfer.find_by_px_tx_id("000014568")
assert users(:trader1).balance(:pgau).zero?

assert_difference "Transfer.count" do
post :px_status, params
assert_response :success
end

# Posting the same data a second time should not result in any transfer
# being created
assert_no_difference "Transfer.count" do
post :px_status, params
assert_response :success
end

assert_equal 1000.0, users(:trader1).balance(:pgau)
assert Transfer.find_by_px_tx_id("000014568")
assert Transfer.find_by_px_payer("customer@gold-cart.com")
end
end

0 comments on commit 59d4648

Please sign in to comment.