Hi I'm Integrating Paypal express checkout using Activemerchant.
I'm able to make successful transaction but I'm not seeing My Item's description in transaction details page.It display all the other option correctly but the Options column is empty.
However I'm seeing Items details in the checkout page.
Here is my code.I probably doing something wrong.
def express_checkout
listing = Listing.find(session[:listing_id].to_f)
response = EXPRESS_GATEWAY.setup_purchase(session[:amount].to_f,
ip: request.remote_ip,
return_url: "http://esignature.lvh.me:3000/en/transactions/status",
cancel_return_url: "http://esignature.lvh.me:3000/",
currency: "USD",
allow_guest_checkout: true,
items: [{name: listing.title, description: listing.description, quantity: session[:number_of_days], amount: listing.price_cents},
{name: "Service Charge" ,description: "Service Charge", amount: session[:service_charge]}
]
)
redirect_to EXPRESS_GATEWAY.redirect_url_for(response.token)
end
def status
if (params[:token].present? && params[:PayerID].present?)
token = params[:token]
#response = EXPRESS_GATEWAY.details_for(token).params
listing = Listing.find(session[:listing_id].to_f)
express_purchase_options = {
:ip => request.remote_ip,
:token => params[:token],
:payer_id => params[:PayerID],
items: [{ name: listing.title, description: listing.description, quantity: session[:number_of_days], amount: listing.price_cents},
{ name: "Service Charge", description: "Service Charge", amount: session[:service_charge ]}
]
}
response = EXPRESS_GATEWAY.purchase(session[:amount].to_f, express_purchase_options)
if response.message == "Success"
listing = Listing.find(session[:listing_id].to_f)
BookingInfo.create!(listing_id: listing.id, start_on: session[:start_date].to_date, end_on: session[:end_date].to_date)
render 'status'
else
render 'status_error'
end
reset_session_params
else
reset_session_params
redirect_to homepage_without_locale_path
end
end
here I'm setting Item details in both setup_purchase as well as in purchase.Any suggestion will be appreciated.Thank you
Related
I am currently building an app that uses stripe, when a payment is successful the user follows redirect_to session.delete(:return_to). How do I control where the user goes when the payment is declined or there is an error?
Perhaps I need an if statement? If success?
def create
#account = User.find_by_id(params[:account_id])
key = #account.access_code
Stripe.api_key = key
account_suid = #account.uid
#order = Order.find(params[:order])
charge = #order.amount * 100
fee = #order.amount * 1
token = params[:stripeToken]
customer = Stripe::Customer.create(email: #order.email, source: token)
Stripe::PaymentIntent.create({
customer: customer,
amount: (charge).to_i,
confirm: true,
currency: 'gbp',
payment_method_types: ['card'],
application_fee_amount: (fee).to_i,
}, {
stripe_account: account_suid
})
options = {
stripe_id: customer.id
}
options.merge!(
card_last4: params[:user][:card_last4],
card_exp_month: params[:user][:card_exp_month],
card_exp_year: params[:user][:card_exp_year],
card_type: params[:user][:card_brand]
)
OrderFiniJob.perform_now(#order)
redirect_to session.delete(:return_to), notice: "Your order has been successful 👌🏼"
end
When an error occurs or the payment is declined, stripe raises an exception (some stripe erros).
So, you could rescue the error and respond what you want, for example 400
def create
...
redirect_to ...
rescue Stripe::StripeError
render status: :bad_request
end
I am applying paypal payments to my website developed in rails 5, I am using the gem 'paypal-sdk-rest', it works quite well for unit payments but i need to implement memberships with monthly and annual fee automatically. is it possible to create memberships with the gem'paypal-sdk-rest'? What would be the necessary parameters to send to the paypal api? this is what I have so far:
class Paypal::CheckoutsController < ApplicationController
include PayPal::SDK::REST
def create
payment = Payment.new({
intent: 'sale',
payer: {
payment_method: 'paypal'
},
redirect_urls: {
return_url: complete_paypal_checkouts_url,
cancel_url: line_items_url
},
transactions: [
{
amount: {
total: current_cart.total,
currency: 'USD',
},
description: 'Pago de productos',
item_list: {
items: current_cart.line_items.map(&:to_paypal)
}
},
]
})
if payment.create
redirect_url = payment.links.find{|v| v.rel == "approval_url" }.href
redirect_to redirect_url
else
redirect_to line_items_path, alert: 'Something went wrong, please try later'
end
end
def complete
payment = Payment.find(params[:paymentId])
if payment.execute(payer_id: params[:PayerID]) #return true or false
session[:cart_id] = nil
redirect_to root_path, notice: 'Thanks for purchasing!'
else
redirect_to line_items_path, alert: 'There was a problem processing your payment'
end
end
end
My json API is below
and I am using careerwave gem
{
"districtcity_id": 23,
"logo": "/home/amulyatanksale/Desktop/the_hulk.jpg",
"last_name": "Legend",
"regionstate_id": 33,
"name": "John",
"role_id": 10,
"email": "jwsohny#gmail.com",
"password": "12345678",
"password_confirmation": "12345678",
"address": "cape town",
"firstplanid": 12,
"contactno": "7897875887",
"is_auction_notification": true
}
And it shows the following error
No such file or directory # rb_sysopen - /home/amulyatanksale/Desktop/the_hulk.jpg
But if run the same API on my local machine it is working
Below is the code for call
def create_user
begin
Rails.logger.info "---------------ERROR WHILE REGISTER------#{params[:logo]}, #{params[:logo].split("/").last}"
parse_image_data(params[:logo], params[:logo].split("/").last)
#user = User.new(register_user_params)
#user.save
Rails.logger.info "---------------ERROR WHILE REGISTER------#{#user.to_json}"
#subscribeplan = Subscription.create(user_id: #user.id, planid: params[:plan_id], name: params[:sub_name], description: params[:description], price: params[:price], is_free: params[:is_free], totalpartscount: params[:totalpartscount] , startdate: Date.today, enddate: Date.today + 1.months)
#subscribeplan.save
#plan_id = #user.subscriptions.first.planid rescue nil
user_type = #user.role.nil? ? "" : #user.role.name
user_state = #user.regionstate.nil? ? "" : #user.regionstate.name
user_city = #user.districtcity.nil? ? "" : #user.districtcity.name
render status: 200, json: { result: :success, message: "Buyer Created.", first_name: #user.name, last_name: #user.last_name, email: #user.email, password: #user.password, logo: #user.logo, phone_no: #user.contactno, address: #user.address, user_type: user_type, registration_date: #user.created_at.to_date, state: user_state, city:user_city, email_auction_notification: #user.is_auction_notification, approval_status: #user.is_approved, user_id: #user.id, subscription_plan: #plan_id }
rescue Exception => e
Rails.logger.info "---------------ERROR WHILE REGISTER------#{#user.to_json}"
Rails.logger.info "---------------ERROR WHILE REGISTER------#{e.to_json}"
render status: 400, json: {errors: #user.errors.messages}, status: :unprocessable_entity
end
end
def parse_image_data(base64_image, img_name)
if img_name
begin
uploaded_file = ActionDispatch::Http::UploadedFile.new({
:tempfile => File.new(Rails.root.join(base64_image)),
:filename => img_name,
:type => Mime::Type.lookup_by_extension(File.extname(img_name)[1..-1]).to_s
})
rescue Exception => e
end
params[:register_user][:logo] = uploaded_file
end
You are passing the path to the file so that is because it works in your local only. You need to submit the form as "multipart/form-data
http://guides.rubyonrails.org/form_helpers.html#uploading-files
I'm using ActiveMerchantI'm trying to Integrate PayPal using express checkout and Adaptive Payment. In the Adaptive Payment I was able to view Shopping Cart Contents as below . My code was like this which works as expected.
def adaptive_checkout
listing = Listing.find(session[:listing_id].to_f)
total_amount_in_cents = listing.price_cents
service_charge_rate = 5 #in percentage 5%
service_charge_in_cents = total_amount_in_cents * service_charge_rate / 100
service_charge_in_dollor = service_charge_in_cents / 100.00 # this will be for secondary user (Admin of the system)
total_amount_in_dollar = total_amount_in_cents / 100.00 # this will be for primary receiver
seller_email = PaypalAccount.where(person_id: listing.author_id).last.email # This is the Primary receiver
system_admin_email = PaypalAccount.where(active: true)
.where("paypal_accounts.community_id IS NOT NULL && paypal_accounts.person_id IS NULL")
.first.email # This is the Secondary receiver
recipients = [
{
email: seller_email,
amount: total_amount_in_dollar ,
primary: true
},
{
email: system_admin_email,
amount: service_charge_in_dollor,
primary: false
}
]
response = ADAPTIVE_GATEWAY.setup_purchase(
action_type: "CREATE",
return_url: "http://esignature.lvh.me:3000/en/transactions/status",
cancel_url: "http://esignature.lvh.me:3000/",
ipn_notification_url: "http://0dbf7871.ngrok.io/en/transactions/notification",
receiver_list: recipients
)
ADAPTIVE_GATEWAY.set_payment_options(
pay_key: response["payKey"],
receiver_options: [
{
description: "Your purchase of #{listing.title}",
invoice_data: {
item: [
{
name: listing.title,
item_count: 1,
item_price: total_amount_in_dollar,
price: total_amount_in_dollar
}
]
},
receiver: {email: seller_email}
},
{
description: "Service charge for purchase of #{listing.title} ",
invoice_data: {
item: [
{
name: "Service charge for purchase of #{listing.title}",
item_count: 1,
item_price: service_charge_in_dollor,
price: service_charge_in_dollor
}
]
},
receiver: {email: system_admin_email}
}
]
)
# For redirecting the customer to the actual paypal site to finish the payment.
redirect_to (ADAPTIVE_GATEWAY.redirect_url_for(response["payKey"]))
end
But using express_checkout, I can't see my shopping cart Contents in transaction details
however during the transaction, item details appears,
For the express checkout my code is like this
def express_checkout
listing = Listing.find(session[:listing_id].to_f)
response = EXPRESS_GATEWAY.setup_purchase(session[:amount].to_f,
ip: request.remote_ip,
return_url: "http://esignature.lvh.me:3000/en/transactions/status",
cancel_return_url: "http://esignature.lvh.me:3000/",
currency: "USD",
allow_guest_checkout: true,
items: [{name: listing.title, description: listing.description, quantity: session[:number_of_days], amount: listing.price_cents},
{name: "Service Charge", amount: session[:service_charge]}
]
)
redirect_to EXPRESS_GATEWAY.redirect_url_for(response.token)
end
def status
if (params[:token].present? && params[:PayerID].present?)
token = params[:token]
#response = EXPRESS_GATEWAY.details_for(token).params
express_purchase_options = {
:ip => request.remote_ip,
:token => params[:token],
:payer_id => params[:PayerID]
}
response = EXPRESS_GATEWAY.purchase(session[:amount].to_f, express_purchase_options)
if response.message == "Success"
listing = Listing.find(session[:listing_id].to_f)
BookingInfo.create!(listing_id: listing.id, start_on: session[:start_date].to_date, end_on: session[:end_date].to_date)
render 'status'
else
render 'status_error'
end
reset_session_params
else
reset_session_params
redirect_to homepage_without_locale_path
end
end
I tried to use set_payment_options method but raise method missing error. Is there any way other way to attach Item details on express checkout.
Are you including the line item details in the DoExpressCheckout call as well? If you are only sending them in the SetExpressCheckout call, they would show in the checkout screen as listed above, but if not included in the DoExpressCheckout call, they would not show in the transaction details.
https://github.com/activemerchant/active_merchant/issues/1912
I want to sent email to user after transcation done. now paypal is working fine for me, but user is not getting mail notification after transcation done? how to get email notification for user .
Here is my code
This is my controller code
This is my paypal function
def pay
if #order.update order_params
#order.update_attributes(:invoice_id => rand.to_s[2..11])
if current_user.billing_address.blank?
current_user.create_billing_address(#order.billing_address.dup.attributes)
end
if current_user.shipping_address.blank?
current_user.create_shipping_address(#order.shipping_address.dup.attributes)
end
# #cart.calculate_shipping
if #order.total == 0
return redirect_to checkout_thank_you_path
end
# if !params['payment'].present?
# return redirect_to :back, notice: 'Select Payment Gateway!'
# end
# if params['payment']=='paypal'
#order.order_statuses.create(status_type: 1)
item_details=[]
#order.line_items.each do |item|
item_details << {:name => item.title, :quantity => item.quantity, :amount => item.amount.fractional}
end
logger.info item_details.inspect
response = EXPRESS_GATEWAY.setup_purchase(#cart.total.fractional,
:ip => request.remote_ip,
:currency =>"USD",
:items => item_details,
:order_id => #order.invoice_id,
:return_url => checkout_thank_you_url,
:cancel_return_url => cart_url
)
return redirect_to EXPRESS_GATEWAY.redirect_url_for(response.token)
# else
# return redirect_to 'https://www.payumoney.com/'
# end
else
flash[:alert] = 'Billing and shipping address fields are required!'
render :addresses
end
end
This is my thank function for paypal
def thank_you
#order = Order.find(session[:order_id])
details = EXPRESS_GATEWAY.details_for(params[:token])
response = EXPRESS_GATEWAY.purchase(#cart.total.fractional, {
ip: request.remote_ip,
token: params[:token],
payer_id: details.payer_id,
items: #order.line_items.map{|l| {name: l.title, quantity: l.quantity, amount: l.amount.fractional}}
})
if response.success?
# logger.info payment_params.inspect
payment_params = {gateway: 'PayPal Express Checkout', transaction_id: response.params['token'], ip: request.remote_ip, amount: response.params['gross_amount']}
#cart.order.created_at = DateTime.now
#cart.order.status = 'Paid'
#cart.order.save
session.delete :order_id
# OrderMailer.order_confirmation(#order).deliver
# OrderMailer.admin_receipt(#order).deliver
else
redirect_to :cart_checkout, alert: 'Something went wrong. Please try again. If the problem persists, please contact us.'
end
#cart = Cart.new current_or_null_user.id, session[:order_id], session[:currency] # Start a new cart
end
Any help is appreciatable