Class: CatarseWepay::WepayController

Inherits:
ApplicationController
  • Object
show all
Defined in:
app/controllers/catarse_wepay/wepay_controller.rb

Constant Summary collapse

SCOPE =
"projects.contributions.checkout"

Instance Method Summary collapse

Instance Method Details

#callback_uri_paramsObject



75
76
77
# File 'app/controllers/catarse_wepay/wepay_controller.rb', line 75

def callback_uri_params
  {host: '52966c09.ngrok.com', port: 80} if Rails.env.development?
end

#contributionObject



92
93
94
95
96
97
98
# File 'app/controllers/catarse_wepay/wepay_controller.rb', line 92

def contribution
  @contribution ||= if params['id']
                PaymentEngines.find_payment(id: params['id'])
              elsif params['checkout_id']
                PaymentEngines.find_payment(payment_token: params['checkout_id'])
              end
end

#gatewayObject



100
101
102
103
# File 'app/controllers/catarse_wepay/wepay_controller.rb', line 100

def gateway
  raise "[WePay] An API Client ID and Client Secret are required to make requests to WePay" unless PaymentEngines.configuration[:wepay_client_id] and PaymentEngines.configuration[:wepay_client_secret]
  @gateway ||= WePay.new(PaymentEngines.configuration[:wepay_client_id], PaymentEngines.configuration[:wepay_client_secret])
end

#ipnObject



25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
# File 'app/controllers/catarse_wepay/wepay_controller.rb', line 25

def ipn
  if contribution && (contribution.payment_method == 'WePay' || contribution.payment_method.nil?)
    response = gateway.call('/checkout', PaymentEngines.configuration[:wepay_access_token], {
        checkout_id: contribution.payment_token,
    })
    PaymentEngines.create_payment_notification contribution_id: contribution.id, extra_data: response
    if response["state"]
      case response["state"].downcase
      when 'captured'
        contribution.confirm!
      when 'refunded'
        contribution.refund!
      when 'cancelled'
        contribution.cancel!
      when 'expired', 'failed'
        contribution.pendent!
      when 'authorized', 'reserved'
        contribution.waiting! if contribution.pending?
      end
    end
    contribution.update_attributes({
      :payment_service_fee => response['fee'],
      :payer_email => response['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



57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
# File 'app/controllers/catarse_wepay/wepay_controller.rb', line 57

def pay
  response = gateway.call('/checkout/create', PaymentEngines.configuration[:wepay_access_token], {
      account_id: PaymentEngines.configuration[:wepay_account_id],
      amount: (contribution.price_in_cents/100).round(2).to_s,
      short_description: t('wepay_description', scope: SCOPE, :project_name => contribution.project.name, :value => contribution.display_value),
      type: 'DONATION',
      redirect_uri: success_wepay_url(id: contribution.id),
      callback_uri: ipn_wepay_index_url(callback_uri_params)
  })
  if response['checkout_uri']
    contribution.update_attributes payment_method: 'WePay', payment_token: response['checkout_id']
    redirect_to response['checkout_uri']
  else
    flash[:failure] = t('wepay_error', scope: SCOPE)
    return redirect_to main_app.edit_project_contribution_path(project_id: contribution.project.id, id: contribution.id)
  end
end

#refundObject



9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
# File 'app/controllers/catarse_wepay/wepay_controller.rb', line 9

def refund
  response = gateway.call('/checkout/refund', PaymentEngines.configuration[:wepay_access_token], {
      account_id: PaymentEngines.configuration[:wepay_account_id],
      checkout_id: contribution.payment_token,
      refund_reason: t('wepay_refund_reason', scope: SCOPE),
  })

  if response['state'] == 'refunded'
    flash[:notice] = I18n.t('projects.contributions.refund.success')
  else
    flash[:alert] = refund_request.try(:message) || I18n.t('projects.contributions.refund.error')
  end

  redirect_to main_app.admin_contributions_path
end

#reviewObject



6
7
# File 'app/controllers/catarse_wepay/wepay_controller.rb', line 6

def review
end

#successObject



79
80
81
82
83
84
85
86
87
88
89
90
# File 'app/controllers/catarse_wepay/wepay_controller.rb', line 79

def success
  response = gateway.call('/checkout', PaymentEngines.configuration[:wepay_access_token], {
      checkout_id: contribution.payment_token,
  })
  if response['state'] == 'authorized'
    flash[:success] = t('success', scope: SCOPE)
    redirect_to main_app.project_contribution_path(project_id: contribution.project.id, id: contribution.id)
  else
    flash[:failure] = t('wepay_error', scope: SCOPE)
    redirect_to main_app.new_project_contribution_path(contribution.project)
  end
end