Created
July 29, 2012 11:58
-
-
Save adriand/3198178 to your computer and use it in GitHub Desktop.
ActiveMerchant and PayPal
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# controller | |
def notify | |
order = Order.capture_payment(request.raw_post) | |
render :nothing => true | |
end | |
# Order model | |
def self.capture_payment(raw_post) | |
notify = Paypal::Notification.new(raw_post) | |
order = Order.find(notify.invoice) | |
if order | |
if notify.acknowledge | |
begin | |
if notify.complete? | |
order.pay! | |
else | |
order.fail! | |
end | |
rescue => e | |
order.fail! | |
end | |
end | |
order.save | |
order | |
end | |
end | |
# payment form | |
= form_tag ActiveMerchant::Billing::Integrations::Paypal.service_url do | |
= hidden_field_tag "business", paypal_account | |
= hidden_field_tag "cancel_return", "http://" + request.host + "/orders/cancel" | |
= hidden_field_tag "notify_url", "http://" + request.host + "/hosted_payment/notify" | |
= hidden_field_tag "return", "http://" + request.host + paid_order_path(@cart_order, :key => @cart_order.key) | |
= hidden_field_tag "redirect_cmd", "_xclick" | |
= hidden_field_tag "cmd", "_cart" | |
= hidden_field_tag "upload", 1 | |
= hidden_field_tag "charset", "utf-8" | |
= hidden_field_tag "currency_code", Forge::Settings[:currency] | |
= hidden_field_tag "invoice", invoice_number_for_paypal(order) | |
= hidden_field_tag "tax_cart", order.tax | |
- if MySettings.use_coupons | |
= hidden_field_tag "discount_amount_cart", order.discount | |
= hidden_field_tag "item_name", "Order #{invoice_number_for_paypal(order)}" | |
= hidden_field_tag "handling_cart", order.shipping_and_handling | |
- i = 1 | |
- order.line_items.each do |item| | |
= hidden_field_tag "item_name_#{i}", item.product.title | |
= hidden_field_tag "amount_#{i}", item.price | |
= hidden_field_tag "quantity_#{i}", item.quantity | |
= hidden_field_tag "tax_#{i}", 0 | |
- i += 1 | |
= hidden_field_tag "no_note", 0 | |
= hidden_field_tag "no_shipping", 2 | |
= submit_tag 'Pay With Paypal', :class => "order-button" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment