Browse Source

Updated to support source actions

1-2-stable-sysmocom
Brian Quinn 12 years ago
parent
commit
1e318b6a78
  1. 43
      app/controllers/admin/paypal_payments_controller.rb
  2. 75
      app/models/paypal_account.rb
  3. 121
      app/views/admin/payments/edit.html.erb
  4. 74
      app/views/admin/payments/source_views/_paypalexpress.erb
  5. 2
      config/locales/en-GB.yml
  6. 9
      config/locales/en-US.yml
  7. 31
      lib/spree/paypal_express.rb

43
app/controllers/admin/paypal_payments_controller.rb

@ -1,43 +0,0 @@
class Admin::PaypalPaymentsController < Admin::BaseController
resource_controller
belongs_to :order
ssl_required
# to allow capture (NB also included in checkout controller...)
include Spree::PaypalExpress
def capture
load_object
if !@order.paypal_payments.empty? && (payment = @order.paypal_payments.last).can_capture?
paypal_capture(payment.find_authorization)
flash[:notice] = t("paypal_capture_complete")
else
flash[:error] = t("unable_to_capture_paypal")
end
redirect_to edit_admin_order_payment_url(@order, @paypal_payment)
end
def refund
load_object
if params.has_key? :amount
if !@order.paypal_payments.empty?
payment = @order.paypal_payments.first
paypal_refund(payment.find_capture, params[:amount].to_f)
flash[:notice] = t("paypal_refund_complete")
else
flash[:error] = t("unable_to_refund_paypal")
end
redirect_to edit_admin_order_payment_url(@order, @paypal_payment)
end
end
end

75
app/models/paypal_account.rb

@ -1,29 +1,72 @@
class PaypalAccount < ActiveRecord::Base
has_many :payments, :as => :source
def finalize!(payment)
def actions
%w{capture credit}
end
def capture(payment)
authorization = find_authorization(payment)
ppx_response = payment.payment_method.provider.capture((100 * payment.amount).to_i, authorization.transaction_id)
if ppx_response.success?
transaction = PaypalTxn.new(:payment => payment,
:amount => ppx_response.params["gross_amount"].to_f,
:message => ppx_response.params["message"],
:payment_status => ppx_response.params["payment_status"],
:pending_reason => ppx_response.params["pending_reason"],
:transaction_id => ppx_response.params["transaction_id"],
:transaction_type => ppx_response.params["transaction_type"],
:payment_type => ppx_response.params["payment_type"],
:response_code => ppx_response.params["ack"],
:token => ppx_response.params["token"],
:avs_response => ppx_response.avs_result["code"],
:cvv_response => ppx_response.cvv_result["code"])
PaypalTxn.create(:payment => payment,
:txn_type => PaypalTxn::TxnType::CAPTURE,
:amount => ppx_response.params["gross_amount"].to_f,
:message => ppx_response.params["message"],
:payment_status => ppx_response.params["payment_status"],
:pending_reason => ppx_response.params["pending_reason"],
:transaction_id => ppx_response.params["transaction_id"],
:transaction_type => ppx_response.params["transaction_type"],
:payment_type => ppx_response.params["payment_type"],
:response_code => ppx_response.params["ack"],
:token => ppx_response.params["token"],
:avs_response => ppx_response.avs_result["code"],
:cvv_response => ppx_response.cvv_result["code"])
payment.finalize!
else
gateway_error(ppx_response)
gateway_error(ppx_response.message)
end
end
def can_capture?(payment)
find_capture(payment).nil?
end
def credit(payment, amount=nil)
authorization = find_capture(payment)
ppx_response = payment.payment_method.provider.credit(amount.nil? ? (100 * amount).to_i : (100 * amount).to_i, authorization.transaction_id)
if ppx_response.success?
payment = authorization.paypal_payment
PaypalTxn.new(:paypal_payment => payment,
:txn_type => PaypalTxn::TxnType::CREDIT,
:gross_amount => ppx_response.params["gross_refund_amount"].to_f,
:message => ppx_response.params["message"],
:payment_status => "Refunded",
:pending_reason => ppx_response.params["pending_reason"],
:transaction_id => ppx_response.params["refund_transaction_id"],
:transaction_type => ppx_response.params["transaction_type"],
:payment_type => ppx_response.params["payment_type"],
:response_code => ppx_response.params["ack"],
:token => ppx_response.params["token"],
:avs_response => ppx_response.avs_result["code"],
:cvv_response => ppx_response.cvv_result["code"])
else
gateway_error(ppx_response.message)
end
end
def can_credit?(payment)
!find_capture(payment).nil?
end
private
def find_authorization(payment)
#find the transaction associated with the original authorization/capture
@ -39,9 +82,7 @@ class PaypalAccount < ActiveRecord::Base
:order => 'created_at DESC')
end
def can_capture?(payment)
find_capture.nil?
end
def gateway_error(text)
msg = "#{I18n.t('gateway_error')} ... #{text}"

121
app/views/admin/payments/edit.html.erb

@ -1,121 +0,0 @@
<%= render :partial => 'admin/shared/order_tabs', :locals => {:current => "Payments"} %>
<h2><%= t("activerecord.models.#{@object.class.to_s.underscore}.one") %></h2>
<%=error_messages_for :creditcard_payment %>
<% form_for(object, :url => object_url, :html => { :method => :put}) do |payment_form| %>
<%= hidden_field_tag :payment_type, object.class.to_s.underscore %>
<p>
<label><%= t("amount") %>:</label></td>
<%= object.amount %>
</p>
<% if object.class == CreditcardPayment %>
<fieldset>
<legend><%= t('creditcard') %></legend>
<table class="index">
<tr>
<th colspan="6"><%= t('card_details') %></th>
</tr>
<tr>
<td><label><%= t("card_number") %>:</label></td>
<td>
XXXX-XXXX-XXXX-<%= object.creditcard.last_digits %>
</td>
<td><label><%= t("expiration") %>:</label></td>
<td>
<%= object.creditcard.month %>/<%= object.creditcard.year %>
</td>
<td><label><%= t("card_code") %>:</label></td>
<td>
<%= object.creditcard.verification_value %>
</td>
</tr>
<tr>
<td><label><%= t("maestro_or_solo_cards") %>:</label></td>
<td>
<%= object.creditcard.issue_number %>
</td>
<td><label><%= t('start_date') %>:</label></td>
<td colspan="3">
<%= object.creditcard.start_month %>/<%= object.creditcard.start_year %>
</td>
</tr>
</table>
<% payment_form.fields_for :order do |order_form| %>
<% order_form.fields_for :checkout do |checkout_form| %>
<% checkout_form.fields_for :bill_address do |ba_form| %>
<%= render :partial => "admin/checkouts/address_form", :locals => {:f => ba_form, :name => t('billing_address')} %>
<% end %>
<% end %>
<% end %>
</fieldset>
<% end %>
<% if object.class == PaypalPayment %>
<fieldset>
<legend><%= t('paypal_payment') %></legend>
<% object.txns.reverse.each do |txn| %>
<table class="index">
<tr>
<th colspan="7"><%= t('transaction') %> <%= txn.transaction_id %> - <%= txn.created_at.to_s(:date_time24) %></th>
</tr>
<tr>
<td width="12%;"><label><%= t("status") %>:</label></td>
<td width="20%;">
<%= txn.payment_status %>
</td>
<td width="8%;"><label><%= t("result") %>:</label></td>
<td width="20%;">
<%= txn.ack %>
</td>
<td width="15%;"><label><%= t("amount") %>:</label></td>
<td width="20%;">
<%= number_to_currency txn.gross_amount %>
</td>
<td width="10%;" rowspan="3">
<% if object.can_capture? %>
<%= link_to t("capture").titleize, capture_admin_order_paypal_payment_url(@order, object) %>
<% end %>
<%= link_to t("refund"), refund_admin_order_paypal_payment_url(@order, object) %>
</td>
</tr>
<tr>
<td><label><%= t("comment") %>:</label></td>
<td colspan="3">
<%= txn.message %>
</td>
<td><label><%= t("paypal_payment_id") %>:</label></td>
<td>
<%= txn.paypal_payment_id %>
</td>
</tr>
<% if txn.payment_status == "Pending" %>
<tr>
<td><label><%= t("pending_reason") %>:</label></td>
<td colspan="6">
<%= txn.pending_reason %>
</td>
</tr>
<% end %>
</table>
<% end %>
</fieldset>
<% end %>
<p class="form-buttons">
<%= button t('continue') %>
</p>
<% end %>
<%#= link_to t("capture").titleize, capture_admin_order_payment_url(@order, @creditcard_payment), :confirm => t('are_you_sure_you_want_to_capture') if object.can_capture? %> &nbsp;

74
app/views/admin/payments/source_views/_paypalexpress.erb

@ -0,0 +1,74 @@
<fieldset>
<legend><%= t('paypal_account') %></legend>
<table class="index">
<tr>
<th colspan="6"><%= t('account_details') %></th>
</tr>
<tr>
<td><label><%= t("email") %>:</label></td>
<td>
<%= payment.source.email %>
</td>
<td><label><%= t("payer_id") %>:</label></td>
<td>
<%= payment.source.payer_id %>
</td>
<td><label><%= t("payer_country") %>:</label></td>
<td>
<%= payment.source.payer_country %>
</td>
</tr>
<tr>
<td><label><%= t("payer_status") %>:</label></td>
<td colspan="5">
<%= payment.source.payer_status %>
</td>
</tr>
</table>
</fieldset>
<fieldset>
<legend><%= t('transactions') %></legend>
<% payment.txns.reverse.each do |txn| %>
<table class="index">
<tr>
<th colspan="6"><%= t('transaction') %> <%= txn.transaction_id %> - <%= txn.created_at.to_s(:date_time24) %></th>
</tr>
<tr>
<td width="12%;"><label><%= t('type') %>:</label></td>
<td width="20%;">
<%= txn.txn_type_name %>
</td>
<td width="8%;"><label><%= t("result") %>:</label></td>
<td width="20%;">
<%= txn.response_code %>
</td>
<td width="15%;"><label><%= t("amount") %>:</label></td>
<td width="20%;">
<%= number_to_currency txn.amount %>
</td>
</tr>
<tr>
<td><label><%= t("comment") %>:</label></td>
<td colspan="3">
<%= txn.message %>
</td>
<td><label><%= t("status") %>:</label></td>
<td>
<%= txn.payment_status %>
</td>
</tr>
<% if txn.payment_status == "Pending" %>
<tr>
<td><label><%= t("pending_reason") %>:</label></td>
<td colspan="5">
<%= txn.pending_reason %>
</td>
</tr>
<% end %>
</table>
<% end %>
</fieldset>

2
config/locales/en-GB.yml

@ -10,6 +10,8 @@ en-GB:
paypal_payment_id: PayPal Payment ID
pending_reason: Pending Reason
result: Result
finalize:
paypalexpress: Capture Payment
activerecord:
attributes:
paypal_payment:

9
config/locales/en-US.yml

@ -10,6 +10,15 @@ en-US:
paypal_payment_id: PayPal Payment ID
pending_reason: Pending Reason
result: Result
review: Review
paypal_account: PayPal Account
payer_id: Payer ID
payer_country: Country
payer_status: Status
comment: Comment
account_details: Account Details
finalize:
paypalexpress: Capture Payment
activerecord:
attributes:
paypal_payment:

31
lib/spree/paypal_express.rb

@ -95,8 +95,10 @@ module Spree::PaypalExpress
if Spree::Config[:auto_capture]
ppx_auth_response = gateway.purchase((@order.total*100).to_i, opts)
txn_type = PaypalTxn::TxnType::CAPTURE
else
ppx_auth_response = gateway.authorize((@order.total*100).to_i, opts)
txn_type = PaypalTxn::TxnType::AUTHORIZE
end
if ppx_auth_response.success?
@ -107,7 +109,7 @@ module Spree::PaypalExpress
:payment_method_id => params[:payment_method_id])
PaypalTxn.create(:payment => payment,
:txn_type => PaypalTxn::TxnType::AUTHORIZE,
:txn_type => txn_type,
:amount => ppx_auth_response.params["gross_amount"].to_f,
:message => ppx_auth_response.params["message"],
:payment_status => ppx_auth_response.params["payment_status"],
@ -135,33 +137,6 @@ module Spree::PaypalExpress
end
end
def paypal_refund(authorization, amount=nil)
ppx_response = paypal_gateway.credit(amount.nil? ? (100 * authorization.gross_amount).to_i : (100 * amount).to_i, authorization.transaction_id)
if ppx_response.success?
payment = authorization.paypal_payment
transaction = PaypalTxn.new(:paypal_payment => payment,
:gross_amount => ppx_response.params["gross_refund_amount"].to_f,
:message => ppx_response.params["message"],
:payment_status => "Refunded",
:pending_reason => ppx_response.params["pending_reason"],
:transaction_id => ppx_response.params["refund_transaction_id"],
:transaction_type => ppx_response.params["transaction_type"],
:payment_type => ppx_response.params["payment_type"],
:ack => ppx_response.params["ack"],
:token => ppx_response.params["token"],
:avs_response => ppx_response.avs_result["code"],
:cvv_response => ppx_response.cvv_result["code"])
payment.paypal_txns << transaction
payment.save
else
gateway_error(ppx_response)
end
end
private
def fixed_opts
if Spree::Config[:paypal_express_local_confirm].nil?

Loading…
Cancel
Save