Adaptive paypal to pay multiple paypal accounts at once
Add gem: gem 'paypal-sdk-adaptivepayments'
In views:
<div class="modal-body">
<%= form_for cart.build_payment, class: "form-horizontal", role: "form" do |f| %>
<%= f.hidden_field :cart_id, :value => cart.id %>
<% url = "#{baseUrl}/subscriptions/subscription_create?cart_id=#{cart.id}&subscriptions=true" %>
<%= f.hidden_field :url, :value => url %>
<div class="form-group">
<label class="control-label col-sm-2" for="email">First Name:</label>
<div class="col-sm-10">
<%= f.text_field :first_name, class: "form-control", placeholder: "Enter the First Name", required:true %>
</div>
</div>
<div class="form-group">
<label class="control-label col-sm-2" for="pwd">Last Name:</label>
<div class="col-sm-10">
<%= f.text_field :last_name, class: "form-control", placeholder: "Enter the Last Name", required:true %>
</div>
</div>
<div class="form-group">
<label class="control-label col-sm-2" for="pwd">Email:</label>
<div class="col-sm-10">
<%= f.email_field :email, class: "form-control", placeholder: "Enter the Email", required:true %>
</div>
</div>
<div class="form-group">
<label class="control-label col-sm-2" for="pwd">Contact:</label>
<div class="col-sm-10">
<%= f.text_field :contact_no, class: "form-control", placeholder: "Enter the Contact NO", required:true %>
</div>
</div>
<div class="form-group">
<div class="col-sm-offset-2 col-sm-10">
<%= f.submit "#{cart.total_price > 0 ? 'Save Details and Go for Payment' : 'Save Details and Get Free Ticket'}", class:"btn btn-primary" %>
</div>
<% end %>
</div>
<div class="modal-footer">
<button type="button" class="btn btn-default" data-dismiss="modal">Close</button>
<!-- <button type="button" class="btn btn-primary">Save changes</button> -->
</div>
</div>
In Controller:
class PaymentsController < ApplicationController
def create
@cart = Cart.find(params[:payment][:cart_id])
@payment = @cart.build_payment(payment_params).save
url = params[:payment][:url]
require 'paypal-sdk-adaptivepayments'
PayPal::SDK.configure(
:mode => "sandbox", # Set "live" for production
:app_id => "APP-80W284485P519543T",
:username => "your-facilitator_api1.gmail.com",
:password => "5UFQJ9NRAQY92BBBBNNB",
:signature => "AFcWxV21C7fd0v3bYLLDYYRCpSSRl31AkFv0tOdoWXLUJks431Ga" )
@api = PayPal::SDK::AdaptivePayments.new
# Build request object
@pay = @api.build_pay({
:actionType => "PAY",
:cancelUrl => request.url,
:currencyCode => "USD",
:feesPayer => "SENDER",
:ipnNotificationUrl => request.url,
:receiverList => {
:receiver => [{
:amount => @cart.total_price.to_f,
:email => "youremail@gmail.com" },{
:amount => @cart.total_price.to_f,
:email => "arvindkushwah9@gmail.com" }] },
:returnUrl => request.url })
# Make API call & get response
@response = @api.pay(@pay)
# Access response
if @response.success? && @response.payment_exec_status != "ERROR"
@response.payKey
# Transaction.create(starter_id: @current_user.id, listing_id: @listing.id, community_id: @current_community.id, payment_gateway: 'paypal', :unit_tr_key => @response.payKey, listing_author_id: @listing.author_id)
redirect_to @api.payment_url(@response) # Url to complete payment
else
@response.error[0].message
redirect_to :back
end
end
end
In Routes:
resources :payments
In views:
<div class="modal-body">
<%= form_for cart.build_payment, class: "form-horizontal", role: "form" do |f| %>
<%= f.hidden_field :cart_id, :value => cart.id %>
<% url = "#{baseUrl}/subscriptions/subscription_create?cart_id=#{cart.id}&subscriptions=true" %>
<%= f.hidden_field :url, :value => url %>
<div class="form-group">
<label class="control-label col-sm-2" for="email">First Name:</label>
<div class="col-sm-10">
<%= f.text_field :first_name, class: "form-control", placeholder: "Enter the First Name", required:true %>
</div>
</div>
<div class="form-group">
<label class="control-label col-sm-2" for="pwd">Last Name:</label>
<div class="col-sm-10">
<%= f.text_field :last_name, class: "form-control", placeholder: "Enter the Last Name", required:true %>
</div>
</div>
<div class="form-group">
<label class="control-label col-sm-2" for="pwd">Email:</label>
<div class="col-sm-10">
<%= f.email_field :email, class: "form-control", placeholder: "Enter the Email", required:true %>
</div>
</div>
<div class="form-group">
<label class="control-label col-sm-2" for="pwd">Contact:</label>
<div class="col-sm-10">
<%= f.text_field :contact_no, class: "form-control", placeholder: "Enter the Contact NO", required:true %>
</div>
</div>
<div class="form-group">
<div class="col-sm-offset-2 col-sm-10">
<%= f.submit "#{cart.total_price > 0 ? 'Save Details and Go for Payment' : 'Save Details and Get Free Ticket'}", class:"btn btn-primary" %>
</div>
<% end %>
</div>
<div class="modal-footer">
<button type="button" class="btn btn-default" data-dismiss="modal">Close</button>
<!-- <button type="button" class="btn btn-primary">Save changes</button> -->
</div>
</div>
In Controller:
class PaymentsController < ApplicationController
def create
@cart = Cart.find(params[:payment][:cart_id])
@payment = @cart.build_payment(payment_params).save
url = params[:payment][:url]
require 'paypal-sdk-adaptivepayments'
PayPal::SDK.configure(
:mode => "sandbox", # Set "live" for production
:app_id => "APP-80W284485P519543T",
:username => "your-facilitator_api1.gmail.com",
:password => "5UFQJ9NRAQY92BBBBNNB",
:signature => "AFcWxV21C7fd0v3bYLLDYYRCpSSRl31AkFv0tOdoWXLUJks431Ga" )
@api = PayPal::SDK::AdaptivePayments.new
# Build request object
@pay = @api.build_pay({
:actionType => "PAY",
:cancelUrl => request.url,
:currencyCode => "USD",
:feesPayer => "SENDER",
:ipnNotificationUrl => request.url,
:receiverList => {
:receiver => [{
:amount => @cart.total_price.to_f,
:email => "youremail@gmail.com" },{
:amount => @cart.total_price.to_f,
:email => "arvindkushwah9@gmail.com" }] },
:returnUrl => request.url })
# Make API call & get response
@response = @api.pay(@pay)
# Access response
if @response.success? && @response.payment_exec_status != "ERROR"
@response.payKey
# Transaction.create(starter_id: @current_user.id, listing_id: @listing.id, community_id: @current_community.id, payment_gateway: 'paypal', :unit_tr_key => @response.payKey, listing_author_id: @listing.author_id)
redirect_to @api.payment_url(@response) # Url to complete payment
else
@response.error[0].message
redirect_to :back
end
end
end
In Routes:
resources :payments
Comments
Post a Comment