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



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

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

#contributionObject



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

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



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

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

#ipnObject



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

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



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

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(subdomain: 'www')
    })

    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



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

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



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

def review
end

#successObject



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

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