Class: CatarsePaypalExpress::PaypalExpressController

Inherits:
ApplicationController
  • Object
show all
Includes:
ActiveMerchant::Billing::Integrations
Defined in:
app/controllers/catarse_paypal_express/paypal_express_controller.rb

Constant Summary collapse

SCOPE =
"projects.contributions.checkout"

Instance Method Summary collapse

Instance Method Details

#cancelObject



69
70
71
72
# File 'app/controllers/catarse_paypal_express/paypal_express_controller.rb', line 69

def cancel
  flash[:failure] = t('paypal_cancel', scope: SCOPE)
  redirect_to main_app.new_project_contribution_path(contribution.project)
end

#contributionObject



74
75
76
77
78
79
80
# File 'app/controllers/catarse_paypal_express/paypal_express_controller.rb', line 74

def contribution
  @contribution ||= if params['id']
                PaymentEngines.find_payment(id: params['id'])
              elsif params['txn_id']
                PaymentEngines.find_payment(payment_id: params['txn_id']) || (params['parent_txn_id'] && PaymentEngines.find_payment(payment_id: params['parent_txn_id']))
              end
end

#gatewayObject



104
105
106
# File 'app/controllers/catarse_paypal_express/paypal_express_controller.rb', line 104

def gateway
  @gateway ||= CatarsePaypalExpress::Gateway.instance
end

#ipnObject



11
12
13
14
15
16
17
18
19
20
21
22
23
24
# File 'app/controllers/catarse_paypal_express/paypal_express_controller.rb', line 11

def ipn
  if contribution && notification.acknowledge && (contribution.payment_method == 'PayPal' || contribution.payment_method.nil?)
    process_paypal_message params
    contribution.update_attributes({
      :payment_service_fee => params['mc_fee'],
      :payer_email => params['payer_email']
    })
  else
    return render status: 500, nothing: true
  end
  return render status: 200, nothing: true
rescue Exception => e
  return render status: 500, text: e.inspect
end

#payObject



26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
# File 'app/controllers/catarse_paypal_express/paypal_express_controller.rb', line 26

def pay
  begin
    response = gateway.setup_purchase(contribution.price_in_cents, {
      ip: request.remote_ip,
      return_url: success_paypal_express_url(id: contribution.id),
      cancel_return_url: cancel_paypal_express_url(id: contribution.id),
      currency_code: 'BRL',
      description: t('paypal_description', scope: SCOPE, :project_name => contribution.project.name, :value => contribution.display_value),
      notify_url: ipn_paypal_express_index_url
    })

    process_paypal_message response.params
    contribution.update_attributes payment_method: 'PayPal', payment_token: response.token

    redirect_to gateway.redirect_url_for(response.token)
  rescue Exception => e
    Rails.logger.info "-----> #{e.inspect}"
    flash[:failure] = t('paypal_error', scope: SCOPE)
    return redirect_to main_app.new_project_contribution_path(contribution.project)
  end
end

#process_paypal_message(data) ⇒ Object



82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
# File 'app/controllers/catarse_paypal_express/paypal_express_controller.rb', line 82

def process_paypal_message(data)
  extra_data = (data['charset'] ? JSON.parse(data.to_json.force_encoding(data['charset']).encode('utf-8')) : data)
  PaymentEngines.create_payment_notification contribution_id: contribution.id, extra_data: extra_data

  if data["checkout_status"] == 'PaymentActionCompleted'
    contribution.confirm!
  elsif data["payment_status"]
    case data["payment_status"].downcase
    when 'completed'
      contribution.confirm!
    when 'refunded'
      contribution.refund!
    when 'canceled_reversal'
      contribution.cancel!
    when 'expired', 'denied'
      contribution.pendent!
    else
      contribution.waiting! if contribution.pending?
    end
  end
end

#reviewObject



8
9
# File 'app/controllers/catarse_paypal_express/paypal_express_controller.rb', line 8

def review
end

#successObject



48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
# File 'app/controllers/catarse_paypal_express/paypal_express_controller.rb', line 48

def success
  begin
    purchase = gateway.purchase(contribution.price_in_cents, {
      ip: request.remote_ip,
      token: contribution.payment_token,
      payer_id: params[:PayerID]
    })

    # we must get the deatils after the purchase in order to get the transaction_id
    process_paypal_message purchase.params
    contribution.update_attributes payment_id: purchase.params['transaction_id'] if purchase.params['transaction_id']

    flash[:success] = t('success', scope: SCOPE)
    redirect_to main_app.project_contribution_path(project_id: contribution.project.id, id: contribution.id)
  rescue Exception => e
    Rails.logger.info "-----> #{e.inspect}"
    flash[:failure] = t('paypal_error', scope: SCOPE)
    return redirect_to main_app.new_project_contribution_path(contribution.project)
  end
end