Created
May 5, 2020 11:43
-
-
Save abhianair/f2c9552161c482c9e2a285c1049df93b to your computer and use it in GitHub Desktop.
create and capture the order in paypal
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| # this wont support recurring subscription | |
| def create | |
| @subscription = current_user.subscriptions.build(subscription_params) | |
| if @subscription.save | |
| create_subscription(@subscription, debug=true) #paypal checkout sdk | |
| else | |
| end | |
| end | |
| def create_subscription (subscription, debug=true) | |
| return_path = subscription_path(subscription) | |
| cancel_path = settings_path | |
| plan = subscription.plan | |
| body = { | |
| intent: 'CAPTURE', | |
| application_context: { | |
| brand_name: 'test', | |
| return_url: "#{Rails.application.secrets.app_host}#{return_path}", | |
| cancel_url: "#{Rails.application.secrets.app_host}#{cancel_path}" | |
| }, | |
| purchase_units: [ | |
| { | |
| amount: { | |
| currency_code: 'USD', | |
| value: "#{plan.fee}", | |
| } | |
| } | |
| ] | |
| } | |
| request = OrdersCreateRequest::new | |
| request.headers["prefer"] = "return=representation" | |
| request.request_body(body) | |
| begin | |
| response = PayPalClient::client.execute(request) | |
| if debug | |
| puts "Status Code: #{response.status_code}" | |
| puts "Status: #{response.result.status}" | |
| puts "Order ID: #{response.result.id}" | |
| puts "Intent: #{response.result.intent}" | |
| puts "Links:" | |
| for link in response.result.links | |
| # this could also be called as link.rel or link.href but as method is a reserved keyword for ruby avoid calling link.method | |
| puts "\t#{link["rel"]}: #{link["href"]}\tCall Type: #{link["method"]}" | |
| end | |
| approve_link = response.result.links[1]['href'] | |
| puts "Gross Amount: #{response.result.purchase_units[0].amount.currency_code} #{response.result.purchase_units[0].amount.value}" | |
| puts PayPalClient::openstruct_to_hash(response.result).to_json | |
| subscription.update_attributes(:transaction_id => response.result.id) | |
| end | |
| redirect_to approve_link | |
| # return response | |
| rescue PayPalHttp::HttpError => ioe | |
| # Exception occured while processing the refund. | |
| puts " Status Code: #{ioe.status_code}" | |
| puts " Debug Id: #{ioe.result.debug_id}" | |
| puts " Response: #{ioe.result}" | |
| end | |
| end | |
| def show | |
| if @subscription.status != 'COMPLETED' | |
| @response, @status = capture_order(@subscription, @subscription.transaction_id, true) | |
| else | |
| @response = '' | |
| @status = false | |
| end | |
| end | |
| # capture order fromm show page which is the return url | |
| def capture_order (subscription, order_id, debug=false) | |
| request = OrdersCaptureRequest::new(order_id) | |
| request.prefer("return=representation") | |
| begin | |
| response = PayPalClient::client.execute(request) | |
| if debug | |
| p "Status Code: #{response.status_code}" | |
| p "Status: #{response.result.status}" | |
| p "Order ID: #{response.result.id}" | |
| p "Intent: #{response.result.intent}" | |
| p "Links:" | |
| for link in response.result.links | |
| # this could also be called as link.rel or link.href but as method is a reserved keyword for ruby avoid calling link.method | |
| puts "\t#{link["rel"]}: #{link["href"]}\tCall Type: #{link["method"]}" | |
| end | |
| p "Capture Ids: " | |
| for purchase_unit in response.result.purchase_units | |
| for capture in purchase_unit.payments.captures | |
| p "\t #{capture.id}" | |
| end | |
| end | |
| p "Buyer:" | |
| buyer = response.result.payer | |
| p "\tEmail Address: #{buyer.email_address}\n\tName: #{buyer.name.full_name}" | |
| p PayPalClient::openstruct_to_hash(response.result).to_json | |
| end | |
| subscription.update_attributes(:status => 'COMPLETED') | |
| return response, true | |
| rescue PayPalHttp::HttpError => ioe | |
| # Exception occured while processing the refund. | |
| p " Status Code: #{ioe.status_code}" | |
| p " Debug Id: #{ioe.result.debug_id}" | |
| p " Response: #{ioe.result}" | |
| return response, false | |
| end | |
| end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #should save in lib/filename.rb | |
| module PayPalClient | |
| class << self | |
| # Set up and return PayPal Ruby SDK environment with PayPal access credentials. | |
| # This sample uses SandboxEnvironment. In production, use LiveEnvironment. | |
| def environment | |
| client_id = 'asdfghjkjhgfdsadfgh......' | |
| client_secret = 'defghjgfdsfghfddsfgh....' | |
| PayPal::SandboxEnvironment.new(client_id, client_secret) | |
| end | |
| # Returns PayPal HTTP client instance with environment that has access | |
| # credentials context. Use this instance to invoke PayPal APIs, provided the | |
| # credentials have access. | |
| def client | |
| PayPal::PayPalHttpClient.new(self.environment) | |
| end | |
| # Utility to convert Openstruct Object to JSON hash. | |
| def openstruct_to_hash(object, hash = {}) | |
| object.each_pair do |key, value| | |
| hash[key] = value.is_a?(OpenStruct) ? openstruct_to_hash(value) : value.is_a?(Array) ? array_to_hash(value) : value | |
| end | |
| hash | |
| end | |
| # Utility to convert array of OpenStruct to hash. | |
| def array_to_hash(array, hash= []) | |
| array.each do |item| | |
| x = item.is_a?(OpenStruct) ? openstruct_to_hash(item) : item.is_a?(Array) ? array_to_hash(item) : item | |
| hash << x | |
| end | |
| hash | |
| end | |
| end | |
| end | |
| #remember to add below line in config/application.rb | |
| config.autoload_paths += %W(#{config.root}/lib) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment