33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
|
# File 'app/controllers/unlock_paypal/contributions_controller.rb', line 33
def edit
authorize @contribution
@initiative = @contribution.initiative
@gateways = @initiative.gateways.without_state(:draft).order(:ordering)
paypal = PayPal::Recurring.new({token: params[:token]}.merge(@contribution.paypal_auth))
details = paypal.checkout_details
@contribution.gateway_data = {} unless @contribution.gateway_data
@contribution.gateway_data["token"] = params[:token]
@contribution.gateway_data["PayerID"] = params[:PayerID]
@contribution.gateway_data["checkout_status"] = details.status
@contribution.gateway_data["email"] = details.email
@contribution.gateway_data["payer_status"] = details.payer_status
@contribution.gateway_data["payer_status"] = details.payer_status
@contribution.gateway_data["first_name"] = details.first_name
@contribution.gateway_data["last_name"] = details.last_name
@contribution.gateway_data["country"] = details.country
@contribution.gateway_data["currency"] = details.currency
@contribution.gateway_data["amount"] = details.amount
@contribution.gateway_data["description"] = details.description
@contribution.gateway_data["ipn_url"] = details.ipn_url
@contribution.gateway_data["agreed"] = details.agreed?
@contribution.save!
paypal = PayPal::Recurring.new({
token: params[:token],
payer_id: params[:PayerID],
description: details.description,
amount: details.amount,
currency: details.currency
}.merge(@contribution.paypal_auth))
payment = paypal.request_payment
if payment.approved? && payment.completed?
recurring = PayPal::Recurring.new({
ipn_url: details.ipn_url,
description: details.description,
amount: details.amount,
currency: details.currency,
frequency: 1,
token: params[:token],
period: :monthly,
payer_id: params[:PayerID],
start_at: Time.now,
failed: 3,
outstanding: :next_billing
}.merge(@contribution.paypal_auth))
profile = recurring.create_recurring_profile
if profile.valid? && profile.profile_id.present? && profile.status == "ActiveProfile"
profile_data = {}
profile_data["profile_id"] = profile.profile_id
profile_data["profile_status"] = profile.status
@contribution.update gateway_data: @contribution.gateway_data.merge(profile_data)
@contribution.activate!
return redirect_to initiative_contribution_path(@initiative.id, @contribution)
else
if payment.errors.size > 0
payment.errors.each do |error|
error[:messages].each do |message|
@contribution.errors.add(:base, "#{error[:code]} #{message} (PayPal)")
end
end
else
error = t('flash.actions.create.alert', resource_name: @contribution.class.model_name.human)
@contribution.errors.add(:base, "#{error} (PayPal - create_recurring_profile)")
end
return render '/initiatives/contributions/new'
end
else
if payment.errors.size > 0
payment.errors.each do |error|
error[:messages].each do |message|
@contribution.errors.add(:base, "#{error[:code]} #{message} (PayPal)")
end
end
else
error = t('flash.actions.create.alert', resource_name: @contribution.class.model_name.human)
@contribution.errors.add(:base, "#{error} (PayPal - request_payment)")
end
return render '/initiatives/contributions/new'
end
end
|