Skip to content

Instantly share code, notes, and snippets.

@chundo
Created July 23, 2018 01:06
Show Gist options
  • Save chundo/a540d82f3919762bbda0cce6fa670949 to your computer and use it in GitHub Desktop.
Save chundo/a540d82f3919762bbda0cce6fa670949 to your computer and use it in GitHub Desktop.
Create plan, suscriptor y payment.
def paypal_test
if ((request.domain != 'localhost') && (request.port != 3000)) || ((request.domain == 'todeploy.com') || (request.domain == 'www.todeploy.com'))
@host = request.env['HTTP_HOST']
else
@host = 'localhost:3000'
end
plan = Plan.new(
name: 'Food of the World Club Membership: Standard',
description: 'Monthly plan for getting the t-shirt of the month.',
type: 'fixed',
payment_definitions: [{
name: 'Standard Plan',
type: 'REGULAR',
frequency_interval: '1',
frequency: 'MONTH',
cycles: '11',
amount: {
currency: 'USD',
value: '19.99'
}
}],
merchant_preferences: {
setup_fee: {
currency: 'USD',
value: '1'
},
return_url: 'http://' + @host + '/verificar',
cancel_url: 'http://' + @host + '/dasboard',
max_fail_attempts: '0',
auto_bill_amount: 'YES',
initial_fail_amount_action: 'CONTINUE'
}
)
# Create plan
if plan.create
# Plan update activation object
plan_update = {
op: 'replace',
path: '/',
value: {
state: 'ACTIVE'
}
}
# Activate plan
if plan.update(plan_update)
puts '*************************************'
puts("Billing plan activated with ID [#{plan.id}]")
puts '*************************************'
else
logger.error payment.error.inspect
end
else
logger.error payment.error.inspect
end
plan_id = plan.id;
# Agreement definition
agreement = Agreement.new({
:name => 'Standard Membership',
:description => 'Food of the World Club Standard Membership',
:start_date => '2019-06-17T9:45:04Z',
:plan => {
:id => plan_id
},
:payer => {
:payment_method => 'paypal'
},
:shipping_address => {
:line1 => 'W 34th St',
:city => 'New York',
:state => 'NY',
:postal_code => '10001',
:country_code => 'US'
}
})
if agreement.create
redirect = agreement.links.find{|v| v.rel == "approval_url" }.href
# REDIRECT USER TO redirect
else
logger.error agreement.error.inspect
end
render json: agreement
end
def verificar
if params['token']
agreement = Agreement.new()
agreement.token = params['token']
if agreement.execute
puts("billing agreement created with ID [#{agreement.id}]")
else
logger.error agreement.error.inspect
end
render json: agreement
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment