diff --git a/Gemfile b/Gemfile index 479b1325..ad11e93d 100644 --- a/Gemfile +++ b/Gemfile @@ -12,7 +12,7 @@ gem 'rails', '~> 5.1.0' gem 'mysql2', '>= 0.3.18', '< 0.5' #Use PosgreSQL -#gem 'pg' +gem 'pg' # redis server for cable # gem 'redis', '~> 3.0' diff --git a/Gemfile.lock b/Gemfile.lock index 93c2a58f..051e1595 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -268,4 +268,4 @@ DEPENDENCIES web-console (>= 3.3.0) BUNDLED WITH - 1.15.0 + 1.15.1 diff --git a/app/assets/javascripts/api/origami/paypar.coffee b/app/assets/javascripts/api/origami/paypar.coffee new file mode 100644 index 00000000..24f83d18 --- /dev/null +++ b/app/assets/javascripts/api/origami/paypar.coffee @@ -0,0 +1,3 @@ +# Place all the behaviors and hooks related to the matching controller here. +# All this logic will automatically be available in application.js. +# You can use CoffeeScript in this file: http://coffeescript.org/ diff --git a/app/assets/javascripts/origami/paypar.coffee b/app/assets/javascripts/origami/paypar.coffee new file mode 100644 index 00000000..24f83d18 --- /dev/null +++ b/app/assets/javascripts/origami/paypar.coffee @@ -0,0 +1,3 @@ +# Place all the behaviors and hooks related to the matching controller here. +# All this logic will automatically be available in application.js. +# You can use CoffeeScript in this file: http://coffeescript.org/ diff --git a/app/assets/stylesheets/api/origami/paypar.scss b/app/assets/stylesheets/api/origami/paypar.scss new file mode 100644 index 00000000..3ce106bb --- /dev/null +++ b/app/assets/stylesheets/api/origami/paypar.scss @@ -0,0 +1,3 @@ +// Place all the styles related to the api/origami/paypar controller here. +// They will automatically be included in application.css. +// You can use Sass (SCSS) here: http://sass-lang.com/ diff --git a/app/assets/stylesheets/origami/paypar.scss b/app/assets/stylesheets/origami/paypar.scss new file mode 100644 index 00000000..8cc055f3 --- /dev/null +++ b/app/assets/stylesheets/origami/paypar.scss @@ -0,0 +1,3 @@ +// Place all the styles related to the origami/paypar controller here. +// They will automatically be included in application.css. +// You can use Sass (SCSS) here: http://sass-lang.com/ diff --git a/app/controllers/origami/card_payments_controller.rb b/app/controllers/origami/card_payments_controller.rb new file mode 100644 index 00000000..6a0e99c9 --- /dev/null +++ b/app/controllers/origami/card_payments_controller.rb @@ -0,0 +1,29 @@ +class Origami::CardPaymentsController < BaseOrigamiController + + + def index + @membership_rebate_balance = 0 + @membership_id = 0 + if !@membership_id.nil? + membership_setting = MembershipSetting.find_by_membership_type("smartpay_url") + membership_data = SalePayment.get_paypar_account(membership_setting.gateway_url,membership_setting.auth_token,@membership_id) + if membership_data["status"]==true + membership_account_data = membership_data["data"]; + membership_account_data.each do |acc_data| + if acc_data["accountable_type"] == "REBATEACCOUNT" + @membership_rebate_balance=acc_data["balance"] + else + @membership_rebate_balance = 0 + end + end + else + @membership_rebate_balance = 0 + end + end + + end + + def create + end + +end diff --git a/app/controllers/origami/others_payments_controller.rb b/app/controllers/origami/others_payments_controller.rb index e92c9535..fa50f6a3 100644 --- a/app/controllers/origami/others_payments_controller.rb +++ b/app/controllers/origami/others_payments_controller.rb @@ -2,6 +2,27 @@ class Origami::OthersPaymentsController < BaseOrigamiController def index + @membership_rebate_balance = 0 + @sale_id = params[:sale_id] + sale_data = Sale.find_by_sale_id(@sale_id) + customer_data= Customer.find_by_customer_id(sale_data.customer_id) + @membership_id = customer_data.membership_id + if !@membership_id.nil? + membership_setting = MembershipSetting.find_by_membership_type("smartpay_url") + membership_data = SalePayment.get_paypar_account(membership_setting.gateway_url,membership_setting.auth_token,@membership_id) + if membership_data["status"]==true + membership_account_data = membership_data["data"]; + membership_account_data.each do |acc_data| + if acc_data["accountable_type"] == "REBATEACCOUNT" + @membership_rebate_balance=acc_data["balance"] + else + @membership_rebate_balance = 0 + end + end + else + @membership_rebate_balance = 0 + end + end end def create diff --git a/app/controllers/origami/paypar_payments_controller.rb b/app/controllers/origami/paypar_payments_controller.rb new file mode 100644 index 00000000..af5ce55f --- /dev/null +++ b/app/controllers/origami/paypar_payments_controller.rb @@ -0,0 +1,22 @@ +class Origami::PayparPaymentsController < BaseOrigamiController + def create + sale_id = params[:sale_id] + redeem_amount = params[:redeem_amount] + membership_id = params[:membership_id] + payment_method = "paypar" + if(Sale.exists?(sale_id)) + saleObj = Sale.find(sale_id) + sale_payment = SalePayment.new + status,msg =sale_payment.process_payment(saleObj, @user, redeem_amount,payment_method) + + if status == true + @out = true, "Success!" + else + @out =false, "Please try again payment!" + end + else + @out = false, "There has no sale record!" + end + + end +end \ No newline at end of file diff --git a/app/helpers/api/origami/paypar_helper.rb b/app/helpers/api/origami/paypar_helper.rb new file mode 100644 index 00000000..0e102f10 --- /dev/null +++ b/app/helpers/api/origami/paypar_helper.rb @@ -0,0 +1,2 @@ +module Api::Origami::PayparHelper +end diff --git a/app/helpers/origami/paypar_helper.rb b/app/helpers/origami/paypar_helper.rb new file mode 100644 index 00000000..fe18e897 --- /dev/null +++ b/app/helpers/origami/paypar_helper.rb @@ -0,0 +1,2 @@ +module Origami::PayparHelper +end diff --git a/app/models/sale_payment.rb b/app/models/sale_payment.rb index 8f9b7294..cb17a325 100644 --- a/app/models/sale_payment.rb +++ b/app/models/sale_payment.rb @@ -8,12 +8,10 @@ class SalePayment < ApplicationRecord attr_accessor :received_amount, :card_payment_reference, :voucher_no, :giftcard_no, :customer_id, :external_payment_status - def process_payment(invoice, action_by, cash_amount) + def process_payment(invoice, action_by, cash_amount,payment_method) self.sale = invoice self.received_amount = cash_amount - - payment_method = "cash" amount_due = invoice.grand_total #get all payment for this invoices @@ -54,7 +52,7 @@ class SalePayment < ApplicationRecord end #record an payment in sale-audit - remark = "Payment #{payment_method}- for Invoice #{invoice.receipt_no} Due [#{amount_due}]| pay amount -> #{amount} | Payment Status ->#{payment_status}" + remark = "Payment #{payment_method}- for Invoice #{invoice.receipt_no} Due [#{amount_due}]| pay amount -> #{cash_amount} | Payment Status ->#{payment_status}" sale_audit = SaleAudit.record_payment(invoice.id, remark, action_by) return true, self.sale @@ -66,6 +64,34 @@ class SalePayment < ApplicationRecord return false, "No outstanding Amount" end + end + + def self.get_paypar_account(paypar_url,token,membership_id) + url = paypar_url.to_s + "/api/get_membership_data".to_s + response = HTTParty.get(url, + :body => { app_token: token,membership_id:membership_id}.to_json, + :headers => { + 'Content-Type' => 'application/json', + 'Accept' => 'application/json' + } + ) + return response; + + end + + def self.redeem(paypar_url,token,membership_id,received_amount,sale_id,campaign_type_id) + url = paypar_url.to_s + "/api/redeem".to_s + response = HTTParty.post(url, + :body => { generic_customer_id:membership_id,total_amount:received_amount,receipet_no:sale_id,campaign_type_id:campaign_type_id,account_no:""}.to_json, + :headers => { + 'Content-Type' => 'application/json', + 'Accept' => 'application/json' + } + ) + puts "RRRRRRRR" + puts response.to_json + return response; + end private @@ -148,15 +174,38 @@ class SalePayment < ApplicationRecord end def paypar_payment - ##TODO - Integration with Paypar (SmartPay) + puts "Paypar Payment" + payment_status = false + + #Next time - validate if the vochure number is valid - within + self.payment_method = "paypar" + self.payment_amount = self.received_amount + self.payment_reference = self.voucher_no + self.outstanding_amount = self.sale.grand_total.to_f - self.received_amount.to_f + self.payment_status = "pending" + payment_method = self.save! + + campaign_type_id =1; + customer_data = Customer.find_by_customer_id(self.sale.customer_id) + membership_setting = MembershipSetting.find_by_membership_type("smartpay_url") + membership_data = SalePayment.redeem(membership_setting.gateway_url,membership_setting.auth_token,customer_data.membership_id,self.received_amount,self.sale.sale_id,campaign_type_id) + if membership_data["status"]==true + SalePayment.where(:sale_payment_id => self.sale_payment_id).update_all(:payment_status => 'paid') + sale_update_payment_status(self.received_amount.to_f) + + else + sale_update_payment_status(0) + end + + + return payment_status + end def sale_update_payment_status(paid_amount) - puts "paid_amount" - puts paid_amount #update amount_outstanding self.sale.amount_received = self.sale.amount_received + paid_amount - self.sale.amount_changed = amount - self.sale.amount_received + self.sale.amount_changed = paid_amount - self.sale.amount_received if (self.sale.grand_total <= self.sale.amount_received && self.sale.amount_changed > 0) self.sale.payment_status = "paid" self.sale.sale_status = "completed" diff --git a/app/views/origami/others_payments/index.html.erb b/app/views/origami/others_payments/index.html.erb index 86ef10b0..094c90c9 100644 --- a/app/views/origami/others_payments/index.html.erb +++ b/app/views/origami/others_payments/index.html.erb @@ -1 +1,165 @@ -Hello Card Payment +