Advertisement
Guest User

Untitled

a guest
Dec 30th, 2015
89
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Ruby 29.38 KB | None | 0 0
  1. # TODO JB : should be restful and renamed !
  2.  
  3. class Api::CommerceController < ApplicationController
  4.  
  5.   before_filter :authenticate_user!
  6.  
  7.  
  8.  
  9.   # Payin to MangoPay wallet
  10.  
  11.   def deposit
  12.  
  13.     # /card/:mango_pay_card_id/wallet/:mango_pay_wallet_id/amount/:amount'
  14.  
  15.     if current_user.mango_pay_id.nil?
  16.  
  17.       render :json => {status: 422, :error => "Missing MangoPay ID"}, :status => 422
  18.  
  19.     end
  20.  
  21.  
  22.  
  23.     payin = MangoPay::PayIn::Card::Direct.create({
  24.  
  25.                                                      AuthorId: current_user.mango_pay_id,
  26.  
  27.                                                      CardId: params[:mango_pay_card_id],
  28.  
  29.                                                      CardType: 'CB_VISA_MASTERCARD',
  30.  
  31.                                                      CreditedUserId: current_user.mango_pay_id,
  32.  
  33.                                                      CreditedWalletId: params[:mango_pay_wallet_id],
  34.  
  35.                                                      DebitedFunds: {Currency: 'EUR', Amount: params[:amount]},
  36.  
  37.                                                      Fees: {Currency: 'EUR', Amount: 0},
  38.  
  39.                                                      SecureModeReturnURL: 'http://test.com',
  40.  
  41.                                                      Tag: params["Test"]
  42.  
  43.                                                  })
  44.  
  45.     render json: payin
  46.  
  47.   end
  48.  
  49.  
  50.  
  51.   def wire_in
  52.  
  53.     organisation_id = params[:organisation_id]
  54.  
  55.     org = Organisation.find(organisation_id)
  56.  
  57.     fund_raiser_id = params[:fund_raiser_id]
  58.  
  59.     mango_pay_wallet_id = params[:mango_pay_wallet_id]
  60.  
  61.     iban = params[:iban]
  62.  
  63.     bic = params[:bic]
  64.  
  65.     amount = params[:amount]
  66.  
  67.     tip = 0
  68.  
  69.     if params[:tip]
  70.  
  71.       tip = params[:tip].to_f
  72.  
  73.     end
  74.  
  75.     ldl_rate = 0
  76.  
  77.     if org.tarif
  78.  
  79.       ldl_rate = org.tarif.rate
  80.  
  81.     end
  82.  
  83.     ldl_fee = 0
  84.  
  85.     mango_fee = 0
  86.  
  87.     fees = 0
  88.  
  89.     tag = params[:tag] or "Test Donation"
  90.  
  91.     anonymous = params[:anonymous]
  92.  
  93.  
  94.  
  95.     ##
  96.  
  97.     # Validations
  98.  
  99.     ##
  100.  
  101.  
  102.  
  103.     # Check that active user has MangoPay ID
  104.  
  105.     @user = UserDecorator.decorate(current_user)
  106.  
  107.     if @user.mango_pay_id.nil?
  108.  
  109.       render :json => {status: 422, :error => "Donating user missing MangoPay ID"}, :status => 422
  110.  
  111.     end
  112.  
  113.  
  114.  
  115.     # Validate organisation
  116.  
  117.     if not organisation_id.nil?
  118.  
  119.       organisation = OrganisationDecorator.decorate(Organisation.find(organisation_id))
  120.  
  121.       donatee = organisation
  122.  
  123.       if donatee.nil?
  124.  
  125.         render :json => {status: 422, :error => "Donation target organisation does not exist"}, :status => 422
  126.  
  127.       end
  128.  
  129.     else
  130.  
  131.       render :json => {status: 422, :error => "Organisation parameter missing"}, :status => 422
  132.  
  133.     end
  134.  
  135.  
  136.  
  137.     # Validate fund raiser
  138.  
  139.     if not fund_raiser_id.nil?
  140.  
  141.       fund_raiser = FundRaiserDecorator.decorate(FundRaiser.find(fund_raiser_id))
  142.  
  143.       donatee = fund_raiser
  144.  
  145.       if donatee.nil?
  146.  
  147.         render :json => {status: 422, :error => "Donation target fund raiser does not exist"}, :status => 422
  148.  
  149.       end
  150.  
  151.     end
  152.  
  153.  
  154.  
  155.     # Validate donatee's MangoPay integration
  156.  
  157.     begin
  158.  
  159.       if donatee.mango_pay_id.nil? or donatee.wallet.nil?
  160.  
  161.         render :json => {status: 422, :error => "Donation target missing MangoPay ID and/or wallet"}, :status => 422
  162.  
  163.       end
  164.  
  165.     rescue
  166.  
  167.       logger.warn($!)
  168.  
  169.       # render :json => {status: 422, :error => $!}, :status => 422
  170.  
  171.     end
  172.  
  173.  
  174.  
  175.     # Check for missing donation target
  176.  
  177.     if organisation.nil?
  178.  
  179.     elsif not fund_raiser.nil?
  180.  
  181.     else
  182.  
  183.       if organisation.mango_pay_id.nil?
  184.  
  185.         render :json => {status: 422, :error => "Donation target missing MangoPay ID"}, :status => 422
  186.  
  187.       end
  188.  
  189.     end
  190.  
  191.  
  192.  
  193.     # register bank acct and donate
  194.  
  195.     if iban
  196.  
  197.       begin
  198.  
  199.         self.register_bank iban, bic, tag
  200.  
  201.  
  202.  
  203.         if amount.to_f * 0.019 < 0.55
  204.  
  205.           if ldl_rate > 0
  206.  
  207.             ldl_fee = amount.to_f * ldl_rate
  208.  
  209.           else
  210.  
  211.             ldl_fee = amount.to_f * 0.05
  212.  
  213.           end
  214.  
  215.           mango_fee = 0.55
  216.  
  217.           fees = ldl_fee + mango_fee
  218.  
  219.         else
  220.  
  221.           if ldl_rate > 0
  222.  
  223.             ldl_fee = amount.to_f * ldl_rate
  224.  
  225.           else
  226.  
  227.             ldl_fee = amount.to_f * 0.05
  228.  
  229.           end
  230.  
  231.           mango_fee = amount.to_f * 0.019
  232.  
  233.           fees = ldl_fee + mango_fee
  234.  
  235.         end
  236.  
  237.  
  238.  
  239.         # Add TVA
  240.  
  241.         tva = ldl_fee * 0.2
  242.  
  243.         fees += tva
  244.  
  245.  
  246.  
  247.         debit_funds = ((amount.to_f + tip.to_f) * 100)
  248.  
  249.         debit_fees = ((fees.to_f + tip.to_f) * 100)
  250.  
  251.  
  252.  
  253.         payin = MangoPay::PayIn::BankWire::Direct.create({
  254.  
  255.                                                          AuthorId: @user.mango_pay_id.to_s,
  256.  
  257.                                                          Tag: tag,
  258.  
  259.                                                          CreditedUserId: donatee.mango_pay_id.to_s,
  260.  
  261.                                                          CreditedWalletId: donatee.wallet[:id].to_s,
  262.  
  263.                                                          DeclaredDebitedFunds: {Currency: 'EUR', Amount: debit_funds},
  264.  
  265.                                                          DeclaredFees: {Currency: 'EUR', Amount: debit_fees}
  266.  
  267.                                                      })
  268.  
  269.  
  270.  
  271.         if payin['Status'] == 'CREATED'
  272.  
  273.           # Create donation from transaction
  274.  
  275.           @donation = Donation.new
  276.  
  277.           @donation.user = @user
  278.  
  279.           @donation.organisation = organisation
  280.  
  281.           @donation.fund_raiser = fund_raiser unless fund_raiser.nil?
  282.  
  283.           @donation.mango_pay_id = payin['Id']
  284.  
  285.           @donation.name = "Test Donation"
  286.  
  287.           @donation.amount = amount
  288.  
  289.           @donation.tip = tip
  290.  
  291.           @donation.mango_fee = mango_fee
  292.  
  293.           @donation.ldl_fee = ldl_fee
  294.  
  295.           @donation.fee = fees
  296.  
  297.           @donation.tva = tva
  298.  
  299.           @donation.total = amount.to_f - fees
  300.  
  301.           @donation.anonymous = anonymous
  302.  
  303.           @donation.save
  304.  
  305.  
  306.  
  307.           #Now give them the money!
  308.  
  309.           performPayout donatee, amount.to_f, fees.to_f, @donation, fund_raiser
  310.  
  311.         else
  312.  
  313.           # Error
  314.  
  315.           render :json => {status: payin['Status'], :error => payin}, :status => 422
  316.  
  317.         end
  318.  
  319.       end
  320.  
  321.     end
  322.  
  323.   end
  324.  
  325.  
  326.  
  327.   def pay_saved_card
  328.  
  329.     ##
  330.  
  331.     # Parameters
  332.  
  333.     ##
  334.  
  335.  
  336.  
  337.     organisation_id = params[:organisation_id]
  338.  
  339.     org = Organisation.find(organisation_id)
  340.  
  341.     fund_raiser_id = params[:fund_raiser_id]
  342.  
  343.     mango_pay_wallet_id = params[:mango_pay_wallet_id]
  344.  
  345.     amount = params[:amount]
  346.  
  347.     tip = 0
  348.  
  349.     if params[:tip]
  350.  
  351.       tip = params[:tip].to_f
  352.  
  353.     end
  354.  
  355.     ldl_rate = 0
  356.  
  357.     if org.tarif
  358.  
  359.       ldl_rate = org.tarif.rate
  360.  
  361.     end
  362.  
  363.     ldl_fee = 0
  364.  
  365.     mango_fee = 0
  366.  
  367.     fees = 0
  368.  
  369.     tag = params[:tag] or "Test Donation"
  370.  
  371.     anonymous = params[:anonymous]
  372.  
  373.  
  374.  
  375.     ##
  376.  
  377.     # Validations
  378.  
  379.     ##
  380.  
  381.  
  382.  
  383.     # Check that active user has MangoPay ID
  384.  
  385.     @user = UserDecorator.decorate(current_user)
  386.  
  387.     if @user.mango_pay_id.nil?
  388.  
  389.       render :json => {status: 422, :error => "Donating user missing MangoPay ID"}, :status => 422
  390.  
  391.     end
  392.  
  393.  
  394.  
  395.     # Validate organisation
  396.  
  397.     if not organisation_id.nil?
  398.  
  399.       organisation = OrganisationDecorator.decorate(Organisation.find(organisation_id))
  400.  
  401.       donatee = organisation
  402.  
  403.       if donatee.nil?
  404.  
  405.         render :json => {status: 422, :error => "Donation target organisation does not exist"}, :status => 422
  406.  
  407.       end
  408.  
  409.     else
  410.  
  411.       render :json => {status: 422, :error => "Organisation parameter missing"}, :status => 422
  412.  
  413.     end
  414.  
  415.  
  416.  
  417.     # Validate fund raiser
  418.  
  419.     if not fund_raiser_id.nil?
  420.  
  421.       fund_raiser = FundRaiserDecorator.decorate(FundRaiser.find(fund_raiser_id))
  422.  
  423.       if donatee.nil?
  424.  
  425.         render :json => {status: 422, :error => "Donation target fund raiser does not exist"}, :status => 422
  426.  
  427.       end
  428.  
  429.     end
  430.  
  431.  
  432.  
  433.     # Validate donatee's MangoPay integration
  434.  
  435.     begin
  436.  
  437.       if donatee.mango_pay_id.nil? or donatee.wallet.nil?
  438.  
  439.         render :json => {status: 422, :error => "Donation target missing MangoPay ID and/or wallet"}, :status => 422
  440.  
  441.       end
  442.  
  443.     rescue
  444.  
  445.       logger.warn($!)
  446.  
  447.       # render :json => {status: 422, :error => $!}, :status => 422
  448.  
  449.     end
  450.  
  451.  
  452.  
  453.     # Check for missing donation target
  454.  
  455.     if organisation.nil?
  456.  
  457.     else
  458.  
  459.       if organisation.mango_pay_id.nil?
  460.  
  461.         render :json => {status: 422, :error => "Donation target missing MangoPay ID"}, :status => 422
  462.  
  463.       end
  464.  
  465.     end
  466.  
  467.  
  468.  
  469.     # Validate that card/wallet is actually active user's card/wallet
  470.  
  471.     #    if not mango_pay_card_id.nil? and not @user.cards.any? { |card| card[:id].to_s == mango_pay_card_id }
  472.  
  473.     #      raise Exception, "Donation card does not belong to active user"
  474.  
  475.     #    elsif not mango_pay_wallet_id.nil? and not @user.wallets.any? { |wallet| wallet[:id].to_s == mango_pay_wallet_id }
  476.  
  477.     #      raise Exception, "Donation wallet does not belong to active user"
  478.  
  479.     #    end
  480.  
  481.  
  482.  
  483.     # Check for card payment
  484.  
  485.     begin
  486.  
  487.  
  488.  
  489.       amount = amount.to_f
  490.  
  491.  
  492.  
  493.       if amount.to_f * 0.019 < 0.55
  494.  
  495.         if ldl_rate > 0
  496.  
  497.           ldl_fee = amount.to_f * ldl_rate
  498.  
  499.         else
  500.  
  501.           ldl_fee = amount.to_f * 0.05
  502.  
  503.         end
  504.  
  505.         mango_fee = 0.55
  506.  
  507.         fees = ldl_fee + mango_fee
  508.  
  509.       else
  510.  
  511.         if ldl_rate > 0
  512.  
  513.           ldl_fee = amount.to_f * ldl_rate
  514.  
  515.         else
  516.  
  517.           ldl_fee = amount.to_f * 0.05
  518.  
  519.         end
  520.  
  521.         mango_fee = amount.to_f * 0.019
  522.  
  523.         fees = ldl_fee + mango_fee
  524.  
  525.       end
  526.  
  527.  
  528.  
  529.       # Add TVA
  530.  
  531.       tva = ldl_fee * 0.2
  532.  
  533.       fees += tva
  534.  
  535.  
  536.  
  537.       # Create donation from transaction
  538.  
  539.       @donation = Donation.new
  540.  
  541.       @donation.user = @user
  542.  
  543.       @donation.organisation = organisation
  544.  
  545.       @donation.fund_raiser = fund_raiser unless fund_raiser.nil?
  546.  
  547.       # @donation.mango_pay_id = payin['Id']
  548.  
  549.       @donation.name = 'Test Donation'
  550.  
  551.       @donation.amount = amount
  552.  
  553.       @donation.tip = tip
  554.  
  555.       @donation.mango_fee = mango_fee
  556.  
  557.       @donation.ldl_fee = ldl_fee
  558.  
  559.       @donation.fee = fees
  560.  
  561.       @donation.tva = tva
  562.  
  563.       @donation.total = amount.to_f - fees
  564.  
  565.       @donation.anonymous = anonymous
  566.  
  567.       @donation.save
  568.  
  569.  
  570.  
  571.       payin_payload = {
  572.  
  573.           AuthorId: @user.mango_pay_id.to_s,
  574.  
  575.           CreditedUserId: donatee.mango_pay_id.to_s,
  576.  
  577.           CreditedWalletId: donatee.wallet[:id].to_s,
  578.  
  579.           DebitedFunds: {Currency: 'EUR', Amount: ((amount + tip) * 100)},
  580.  
  581.           Fees: {Currency: 'EUR', Amount: ((fees + tip) * 100)},
  582.  
  583.           CardId: @user.mango_pay_card.to_s,
  584.  
  585.           CardType: 'CB_VISA_MASTERCARD',
  586.  
  587.           SecureMode: "DEFAULT",
  588.  
  589.       }
  590.  
  591.       if !fund_raiser.nil?
  592.  
  593.         payin_payload['SecureModeReturnURL'] = Rails.application.config.FRONTEND + '/merci/collectes/' + @donation.order
  594.  
  595.       else
  596.  
  597.         payin_payload['SecureModeReturnURL'] = Rails.application.config.FRONTEND + '/merci/' + @donation.order
  598.  
  599.       end
  600.  
  601.  
  602.  
  603.       payin = MangoPay::PayIn::Card::Direct.create(payin_payload)
  604.  
  605.  
  606.  
  607.       if payin['Status'] == 'SUCCEEDED' || payin['Status'] == 'CREATED'
  608.  
  609.  
  610.  
  611.         @donation.mango_pay_id = payin['Id']
  612.  
  613.         @donation.save
  614.  
  615.  
  616.  
  617.         #Now give them the money!
  618.  
  619.         if payin['Status'] == 'SUCCEEDED'
  620.  
  621.           performPayout donatee, amount, fees, @donation, fund_raiser
  622.  
  623.         else
  624.  
  625.           @donation.awaiting_confirmation = true
  626.  
  627.           @donation.save
  628.  
  629.           render :json => {callback:  payin['SecureModeRedirectURL']}
  630.  
  631.         end
  632.  
  633.       else
  634.  
  635.         if payin['ResultCode'] == '002999'
  636.  
  637.           #KYC error
  638.  
  639.           if !@donation.user.enterprise
  640.  
  641.             render :json => {needs_kyc: '/kyc/user/' + @donation.order}
  642.  
  643.           else
  644.  
  645.             render :json => {needs_kyc: '/kyc/entreprise/' + @donation.order}
  646.  
  647.           end
  648.  
  649.         else
  650.  
  651.           # Error
  652.  
  653.           @donation.destroy
  654.  
  655.           render :json => {status: payin['Status'], :error => payin}, :status => 422
  656.  
  657.         end
  658.  
  659.       end
  660.  
  661.     end
  662.  
  663.   end
  664.  
  665.  
  666.  
  667.   # Payin to MangoPay wallet
  668.  
  669.   def donate
  670.  
  671.     ##
  672.  
  673.     # Parameters
  674.  
  675.     ##
  676.  
  677.  
  678.  
  679.     if not params[:iban].nil?
  680.  
  681.       wire_in
  682.  
  683.       return
  684.  
  685.     end
  686.  
  687.  
  688.  
  689.     organisation_id = params[:organisation_id]
  690.  
  691.     org = Organisation.find(organisation_id)
  692.  
  693.     fund_raiser_id = params[:fund_raiser_id]
  694.  
  695.     mango_pay_wallet_id = params[:mango_pay_wallet_id]
  696.  
  697.     card_number = params[:card_number]
  698.  
  699.     card_expiry = params[:card_expiry]
  700.  
  701.     card_code = params[:card_code]
  702.  
  703.     amount = params[:amount]
  704.  
  705.     tip = 0
  706.  
  707.     if params[:tip]
  708.  
  709.       tip = params[:tip].to_f
  710.  
  711.     end
  712.  
  713.     ldl_rate = 0
  714.  
  715.     if org.tarif
  716.  
  717.       ldl_rate = org.tarif.rate
  718.  
  719.     end
  720.  
  721.     ldl_fee = 0
  722.  
  723.     mango_fee = 0
  724.  
  725.     fees = 0
  726.  
  727.     tag = params[:tag] or "Test Donation"
  728.  
  729.     anonymous = params[:anonymous]
  730.  
  731.  
  732.  
  733.     ##
  734.  
  735.     # Validations
  736.  
  737.     ##
  738.  
  739.  
  740.  
  741.     # Check that active user has MangoPay ID
  742.  
  743.     @user = UserDecorator.decorate(current_user)
  744.  
  745.     if @user.mango_pay_id.nil?
  746.  
  747.       render :json => {status: 422, :error => "Donating user missing MangoPay ID"}, :status => 422
  748.  
  749.     end
  750.  
  751.  
  752.  
  753.     ##
  754.  
  755.     # Limit test
  756.  
  757.     ##
  758.  
  759.     @last_changed = nil
  760.  
  761.     donation_total = 0
  762.  
  763.  
  764.  
  765.     if @user.mango_pay_date != nil
  766.  
  767.       @last_changed = @user.mango_pay_date
  768.  
  769.     else
  770.  
  771.       @last_changed = Date.new(Time.now.year, 1, 1)
  772.  
  773.     end
  774.  
  775.  
  776.  
  777.     @user.donations.each do |donation|
  778.  
  779.       if Date.parse(donation.created_at.to_s) > @last_changed
  780.  
  781.         donation_total += (donation.amount + donation.tip)
  782.  
  783.       end
  784.  
  785.     end
  786.  
  787.  
  788.  
  789.     if donation_total.to_f + amount.to_f + tip.to_f > 2500
  790.  
  791.       if !@user.enterprise
  792.  
  793.         # Create MangoPay Natural User
  794.  
  795.         nattyPayload = {
  796.  
  797.             Tag: 'LDL_ID_USER_' + @user.id.to_s,
  798.  
  799.             Email: @user.email,
  800.  
  801.             FirstName: @user.first_name,
  802.  
  803.             LastName: @user.last_name,
  804.  
  805.             Nationality: CountryHelper.nationalityToIso(@user.nationality),
  806.  
  807.             CountryOfResidence: CountryHelper.countryToIso(@user.country)
  808.  
  809.         }
  810.  
  811.         # address
  812.  
  813.         if !@user.address || !@user.postcode || !@user.city || !@user.country
  814.  
  815.           nattyPayload['Address'] = "Temp Address"
  816.  
  817.         else
  818.  
  819.           if !@user.address2.nil?
  820.  
  821.             nattyPayload['Address'] = @user.address + ', ' + @user.address2 + ', ' + @user.postcode +
  822.  
  823.                 ', ' + @user.city + ', ' + @user.country
  824.  
  825.           else
  826.  
  827.             nattyPayload['Address'] = @user.address + ', ' + @user.postcode + ', ' +
  828.  
  829.                 @user.city + ', ' + @user.country
  830.  
  831.           end
  832.  
  833.         end
  834.  
  835.         # birthday
  836.  
  837.         if !@user.dob
  838.  
  839.           nattyPayload['Birthday'] = 489193200
  840.  
  841.         else
  842.  
  843.           nattyPayload['Birthday'] = @user.dob.utc.to_time.to_i + 3600
  844.  
  845.         end
  846.  
  847.         user = MangoPay::NaturalUser.create(nattyPayload)
  848.  
  849.         @user.mango_pay_id = user['Id']
  850.  
  851.         @user.mango_pay_date = DateTime.now
  852.  
  853.         @user.save
  854.  
  855.       else
  856.  
  857.         # Create MangoPay Legal User
  858.  
  859.         legalPayload = {
  860.  
  861.             Tag: 'LDL_ID_USER_' + @user.id.to_s,
  862.  
  863.             Email: @user.email,
  864.  
  865.             Name: @user.username,
  866.  
  867.             LegalPersonType: 'BUSINESS',
  868.  
  869.             LegalRepresentativeFirstName: @user.first_name,
  870.  
  871.             LegalRepresentativeLastName: @user.last_name,
  872.  
  873.             LegalRepresentativeNationality: CountryHelper.nationalityToIso(@user.nationality),
  874.  
  875.             LegalRepresentativeCountryOfResidence: CountryHelper.countryToIso(@user.country_residence)
  876.  
  877.         }
  878.  
  879.  
  880.  
  881.         if @user.email_representative
  882.  
  883.           legalPayload['LegalRepresentativeEmail'] = @user.email_representative
  884.  
  885.         end
  886.  
  887.  
  888.  
  889.         # address
  890.  
  891.         if !@user.address || !@user.postcode || !@user.city || !@user.country
  892.  
  893.           legalPayload['HeadquartersAddress'] = "Temp Headquarters Address"
  894.  
  895.         else
  896.  
  897.           if !@user.address2.nil?
  898.  
  899.             legalPayload['HeadquartersAddress'] = @user.address + ', ' + @user.address2 + ', ' + @user.postcode + ', ' + @user.city + ', ' + @user.country
  900.  
  901.           else
  902.  
  903.             legalPayload['HeadquartersAddress'] = @user.address + ', ' + @user.postcode + ', ' + @user.city + ', ' + @user.country
  904.  
  905.           end
  906.  
  907.         end
  908.  
  909.         # birthday
  910.  
  911.         if !@user.dob
  912.  
  913.           legalPayload['LegalRepresentativeBirthday'] = 489193200
  914.  
  915.         else
  916.  
  917.           legalPayload['LegalRepresentativeBirthday'] = @user.dob.utc.to_time.to_i + 3600
  918.  
  919.         end
  920.  
  921.         user = MangoPay::LegalUser.create(legalPayload)
  922.  
  923.         @user.mango_pay_id = user['Id']
  924.  
  925.         @user.mango_pay_date = Date.new
  926.  
  927.         @user.save
  928.  
  929.       end
  930.  
  931.     end
  932.  
  933.  
  934.  
  935.     # Validate organisation
  936.  
  937.     if not organisation_id.nil?
  938.  
  939.       organisation = OrganisationDecorator.decorate(Organisation.find(organisation_id))
  940.  
  941.       donatee = organisation
  942.  
  943.       if donatee.nil?
  944.  
  945.         render :json => {status: 422, :error => "Donation target organisation does not exist"}, :status => 422
  946.  
  947.       end
  948.  
  949.     else
  950.  
  951.       render :json => {status: 422, :error => "Organisation parameter missing"}, :status => 422
  952.  
  953.     end
  954.  
  955.  
  956.  
  957.     # Validate fund raiser
  958.  
  959.     if not fund_raiser_id.nil?
  960.  
  961.       fund_raiser = FundRaiserDecorator.decorate(FundRaiser.find(fund_raiser_id))
  962.  
  963.       if donatee.nil?
  964.  
  965.         render :json => {status: 422, :error => "Donation target fund raiser does not exist"}, :status => 422
  966.  
  967.       end
  968.  
  969.     end
  970.  
  971.  
  972.  
  973.     # Validate donatee's MangoPay integration
  974.  
  975.     begin
  976.  
  977.       if donatee.mango_pay_id.nil? or donatee.wallet.nil?
  978.  
  979.         render :json => {status: 422, :error => "Donation target missing MangoPay ID and/or wallet"}, :status => 422
  980.  
  981.       end
  982.  
  983.     rescue
  984.  
  985.       logger.warn($!)
  986.  
  987.       # render :json => {status: 422, :error => $!}, :status => 422
  988.  
  989.     end
  990.  
  991.  
  992.  
  993.     # Check for missing donation target
  994.  
  995.     if organisation.nil?
  996.  
  997.     else
  998.  
  999.       if organisation.mango_pay_id.nil?
  1000.  
  1001.         render :json => {status: 422, :error => "Donation target missing MangoPay ID"}, :status => 422
  1002.  
  1003.       end
  1004.  
  1005.     end
  1006.  
  1007.  
  1008.  
  1009.     # Validate that card/wallet is actually active user's card/wallet
  1010.  
  1011. #    if not mango_pay_card_id.nil? and not @user.cards.any? { |card| card[:id].to_s == mango_pay_card_id }
  1012.  
  1013. #      raise Exception, "Donation card does not belong to active user"
  1014.  
  1015. #    elsif not mango_pay_wallet_id.nil? and not @user.wallets.any? { |wallet| wallet[:id].to_s == mango_pay_wallet_id }
  1016.  
  1017. #      raise Exception, "Donation wallet does not belong to active user"
  1018.  
  1019. #    end
  1020.  
  1021.  
  1022.  
  1023.     # Check for card payment
  1024.  
  1025.     if card_number
  1026.  
  1027.       begin
  1028.  
  1029.         card = self.register(card_number, card_expiry, card_code)
  1030.  
  1031.  
  1032.  
  1033.         if card['ResultMessage'] == 'Success'
  1034.  
  1035.           #save card if applicable
  1036.  
  1037.           if params[:save] == true
  1038.  
  1039.             if @user.mango_pay_card
  1040.  
  1041.               MangoPay::Card.update(@user.mango_pay_card, {
  1042.  
  1043.                                                       Active: 'false'
  1044.  
  1045.                                                   })
  1046.  
  1047.             end
  1048.  
  1049.             cc = params[:card_number].split('')
  1050.  
  1051.             cc[4..11] = %w(X X X X X X X X)
  1052.  
  1053.             @user.active_card_number = cc.join('')
  1054.  
  1055.             @user.mango_pay_card = card['CardId']
  1056.  
  1057.             # @user.active_card_month = params[:card_expiry].to_s[0,2]
  1058.  
  1059.             # @user.active_card_year = params[:card_expiry].to_s[2,3]
  1060.  
  1061.             # @user.active_card_code = params[:card_code]
  1062.  
  1063.             @user.save
  1064.  
  1065.           end
  1066.  
  1067.         else
  1068.  
  1069.           logger.warn(card['ResultMessage'])
  1070.  
  1071.         end
  1072.  
  1073.  
  1074.  
  1075.         amount = amount.to_f
  1076.  
  1077.  
  1078.  
  1079.         if amount.to_f * 0.019 < 0.55
  1080.  
  1081.           if ldl_rate > 0
  1082.  
  1083.             ldl_fee = amount.to_f * ldl_rate
  1084.  
  1085.           else
  1086.  
  1087.             ldl_fee = amount.to_f * 0.05
  1088.  
  1089.           end
  1090.  
  1091.           mango_fee = 0.55
  1092.  
  1093.           fees = ldl_fee + mango_fee
  1094.  
  1095.         else
  1096.  
  1097.           if ldl_rate > 0
  1098.  
  1099.             ldl_fee = amount.to_f * ldl_rate
  1100.  
  1101.           else
  1102.  
  1103.             ldl_fee = amount.to_f * 0.05
  1104.  
  1105.           end
  1106.  
  1107.           mango_fee = amount.to_f * 0.019
  1108.  
  1109.           fees = ldl_fee + mango_fee
  1110.  
  1111.         end
  1112.  
  1113.  
  1114.  
  1115.         # Add TVA
  1116.  
  1117.         tva = ldl_fee * 0.2
  1118.  
  1119.         fees += tva
  1120.  
  1121.  
  1122.  
  1123.         # Create donation from transaction
  1124.  
  1125.         @donation = Donation.new
  1126.  
  1127.         @donation.user = @user
  1128.  
  1129.         @donation.organisation = organisation
  1130.  
  1131.         @donation.fund_raiser = fund_raiser unless fund_raiser.nil?
  1132.  
  1133.         # @donation.mango_pay_id = payin['Id']
  1134.  
  1135.         @donation.name = 'Test Donation'
  1136.  
  1137.         @donation.amount = amount
  1138.  
  1139.         @donation.tip = tip
  1140.  
  1141.         @donation.mango_fee = mango_fee
  1142.  
  1143.         @donation.ldl_fee = ldl_fee
  1144.  
  1145.         @donation.fee = fees
  1146.  
  1147.         @donation.tva = tva
  1148.  
  1149.         @donation.total = amount.to_f - fees
  1150.  
  1151.         @donation.anonymous = anonymous
  1152.  
  1153.         @donation.save
  1154.  
  1155.  
  1156.  
  1157.         payin_payload = {
  1158.  
  1159.             AuthorId: @user.mango_pay_id.to_s,
  1160.  
  1161.             CreditedUserId: donatee.mango_pay_id.to_s,
  1162.  
  1163.             CreditedWalletId: donatee.wallet[:id].to_s,
  1164.  
  1165.             DebitedFunds: {Currency: 'EUR', Amount: ((amount + tip) * 100)},
  1166.  
  1167.             Fees: {Currency: 'EUR', Amount: ((fees + tip) * 100)},
  1168.  
  1169.             CardId: card['CardId'].to_s,
  1170.  
  1171.             CardType: 'CB_VISA_MASTERCARD',
  1172.  
  1173.             SecureMode: "DEFAULT",
  1174.  
  1175.         }
  1176.  
  1177.  
  1178.  
  1179.         # Forcing update
  1180.  
  1181.  
  1182.  
  1183.         if !fund_raiser.nil?
  1184.  
  1185.           payin_payload['SecureModeReturnURL'] = Rails.application.config.FRONTEND + '/merci/collectes/' + @donation.order
  1186.  
  1187.         else
  1188.  
  1189.           payin_payload['SecureModeReturnURL'] = Rails.application.config.FRONTEND + '/merci/' + @donation.order
  1190.  
  1191.         end
  1192.  
  1193.  
  1194.  
  1195.         payin = MangoPay::PayIn::Card::Direct.create(payin_payload)
  1196.  
  1197.  
  1198.  
  1199.         if payin['Status'] == 'SUCCEEDED' || payin['Status'] == 'CREATED'
  1200.  
  1201.  
  1202.  
  1203.           @donation.mango_pay_id = payin['Id']
  1204.  
  1205.           @donation.save
  1206.  
  1207.  
  1208.  
  1209.  
  1210.  
  1211.           if params[:save] != true
  1212.  
  1213.             MangoPay::Card.update(card['CardId'], {
  1214.  
  1215.                                       Active: 'false'
  1216.  
  1217.                                   })
  1218.  
  1219.           end
  1220.  
  1221.  
  1222.  
  1223.           #Now give them the money!
  1224.  
  1225.           if payin['Status'] == 'SUCCEEDED'
  1226.  
  1227.             performPayout donatee, amount, fees, @donation, fund_raiser
  1228.  
  1229.           else
  1230.  
  1231.             @donation.awaiting_confirmation = true
  1232.  
  1233.             @donation.save
  1234.  
  1235.             render :json => {callback:  payin['SecureModeRedirectURL']}
  1236.  
  1237.           end
  1238.  
  1239.         else
  1240.  
  1241.           if payin['ResultCode'] == '002999'
  1242.  
  1243.             #KYC error
  1244.  
  1245.             if !@donation.user.enterprise
  1246.  
  1247.               render :json => {callback: '/kyc/user/' + @donation.order}
  1248.  
  1249.             else
  1250.  
  1251.               render :json => {callback: '/kyc/entreprise/' + @donation.order}
  1252.  
  1253.             end
  1254.  
  1255.           else
  1256.  
  1257.             # Error
  1258.  
  1259.             @donation.destroy
  1260.  
  1261.             render :json => {status: payin['Status'], :error => payin}, :status => 422
  1262.  
  1263.           end
  1264.  
  1265.         end
  1266.  
  1267.       end
  1268.  
  1269.     end
  1270.  
  1271.   end
  1272.  
  1273.  
  1274.  
  1275.   def profile
  1276.  
  1277.     unless current_user.nil?
  1278.  
  1279.       @user = UserDecorator.decorate(current_user)
  1280.  
  1281.     end
  1282.  
  1283.   end
  1284.  
  1285.  
  1286.  
  1287.   # Refund payin or transfer from a MangoPay wallet
  1288.  
  1289.   def refund
  1290.  
  1291.     # Hmm
  1292.  
  1293.   end
  1294.  
  1295.  
  1296.  
  1297.   # Unregister MangoPay card
  1298.  
  1299.   def unregister
  1300.  
  1301.     # /card/:mango_pay_card_id
  1302.  
  1303.   end
  1304.  
  1305.  
  1306.  
  1307.   def withdraw
  1308.  
  1309.     # /wallet/:mango_pay_wallet_id/amount/:amount
  1310.  
  1311.   end
  1312.  
  1313.  
  1314.  
  1315.   # Register MangoPay card
  1316.  
  1317.   def register(card_number, card_expiry, card_code)
  1318.  
  1319.     ##
  1320.  
  1321.     # Parameters
  1322.  
  1323.     ##
  1324.  
  1325.  
  1326.  
  1327.     @user = UserDecorator.decorate(current_user)
  1328.  
  1329.  
  1330.  
  1331.     tag = "Card_LDL_ID_USER_" + @user.id.to_s
  1332.  
  1333.  
  1334.  
  1335.     card_registration = MangoPay::CardRegistration.create({
  1336.  
  1337.                                                               UserId: @user.mango_pay_id,
  1338.  
  1339.                                                               Currency: 'EUR',
  1340.  
  1341.                                                               Tag: tag
  1342.  
  1343.                                                           })
  1344.  
  1345.  
  1346.  
  1347.     # 2nd step: tokenize by payline (fills-in RegistrationData)
  1348.  
  1349.     card_injection = {
  1350.  
  1351.         data: card_registration['PreregistrationData'],
  1352.  
  1353.         accessKeyRef: card_registration['AccessKey'],
  1354.  
  1355.         cardNumber: card_number,
  1356.  
  1357.         cardExpirationDate: card_expiry,
  1358.  
  1359.         cardCvx: card_code
  1360.  
  1361.     }
  1362.  
  1363.  
  1364.  
  1365.     card_tokenisation = Net::HTTP.post_form(URI(card_registration['CardRegistrationURL']), card_injection)
  1366.  
  1367.  
  1368.  
  1369.     # TODO Better way to do this?
  1370.  
  1371.     render :json => {status: 422, errors: [card_tokenisation.body]}, :status => 422 unless (card_tokenisation.is_a?(Net::HTTPOK) && card_tokenisation.body.start_with?('data='))
  1372.  
  1373.  
  1374.  
  1375.     card_registration['RegistrationData'] = card_tokenisation.body
  1376.  
  1377.  
  1378.  
  1379.     # 3rd step: update (fills-in CardId) and return it
  1380.  
  1381.     card_confirmation = MangoPay::CardRegistration.update(card_registration['Id'], {
  1382.  
  1383.         RegistrationData: card_registration['RegistrationData']
  1384.  
  1385.     })
  1386.  
  1387.  
  1388.  
  1389.     return card_confirmation
  1390.  
  1391.   end
  1392.  
  1393.  
  1394.  
  1395.   # Automatic Payout via Bank Wire
  1396.  
  1397.   def performPayout(donatee, amount, fees, donation, fund_raiser)
  1398.  
  1399.     unless donatee.mango_pay_bank_id.nil?
  1400.  
  1401.       payout = MangoPay::PayOut::BankWire.create({
  1402.  
  1403.                                             AuthorId: donatee.mango_pay_id,
  1404.  
  1405.                                             DebitedFunds: { Currency: 'EUR', Amount: (amount - fees) * 100 },
  1406.  
  1407.                                             Fees: { Currency: 'EUR', Amount: 0 },
  1408.  
  1409.                                             DebitedWalletId: donatee.wallet[:id],
  1410.  
  1411.                                             BankAccountId: donatee.mango_pay_bank_id,
  1412.  
  1413.                                             Communication: 'This is a test',
  1414.  
  1415.                                             Tag: 'Test Bank Wire'
  1416.  
  1417.                                         })
  1418.  
  1419.       logger.warn(payout)
  1420.  
  1421.       # Send Confirmation Emails
  1422.  
  1423.       if !fund_raiser.nil?
  1424.  
  1425.         if fund_raiser.alert_creator
  1426.  
  1427.           PlatformMailer.donation_notification(fund_raiser, donation).deliver
  1428.  
  1429.         end
  1430.  
  1431.       end
  1432.  
  1433.  
  1434.  
  1435.       json = @donation.to_json(:except => [:receipt_file_name, :receipt_content_type], :methods => [:receipt], :include => [{:fund_raiser => {:include => [{:user => {:include => [:organisation => {:only => [:id, :name]}], :only => [:id, :first_name, :last_name, :enterprise, :username]}}, {:organisation => {:only =>[:id, :name]}}], :methods => [:img_fb]}}, {:organisation => {:only => [:id, :name, :abbrev, :address, :address2, :postcode, :city, :country, :statute, :org_object, :article_200, :article_238_bis, :article_885_0, :president_name], :methods => [:logo_thumb, :logo_original, :signature_thumb, :signature_original], :except => [:organisation_id, :mango_pay_id, :receipt_file_name, :receipt_content_type, :created_at, :updated_at]}}])
  1436.  
  1437.  
  1438.  
  1439.  
  1440.  
  1441.       Rails.logger.debug "================= JSON: #{json}"
  1442.  
  1443.       render :json => json
  1444.  
  1445.     end
  1446.  
  1447.   end
  1448.  
  1449.  
  1450.  
  1451.   def register_bank(iban, bic, tag)
  1452.  
  1453.     # Determine and set address
  1454.  
  1455.     @baddress = ''
  1456.  
  1457.     if !@user.address2.nil?
  1458.  
  1459.       @baddress = @user.address + ', ' + @user.address2 + ', ' + @user.postcode + ', ' + @user.city + ', ' + @user.country
  1460.  
  1461.     else
  1462.  
  1463.       @baddress = @user.address + ', ' + @user.postcode + ', ' + @user.city + ', ' + @user.country
  1464.  
  1465.     end
  1466.  
  1467.  
  1468.  
  1469.     # First get bank account if present
  1470.  
  1471.     if @user.mango_pay_bank_id
  1472.  
  1473.       acct =  MangoPay::BankAccount.fetch(@user.mango_pay_id, @user.mango_pay_bank_id)
  1474.  
  1475.       # compare and reg if no longer valid
  1476.  
  1477.       if @baddress != acct['OwnerAddress'] || iban != acct['IBAN'] || bic != acct['BIC']
  1478.  
  1479.         #update
  1480.  
  1481.         perform_bank_registration(iban, bic, tag, @baddress)
  1482.  
  1483.       end
  1484.  
  1485.     else
  1486.  
  1487.       # No saved account
  1488.  
  1489.       perform_bank_registration(iban, bic, tag, @baddress)
  1490.  
  1491.     end
  1492.  
  1493.   end
  1494.  
  1495.  
  1496.  
  1497.   def perform_bank_registration(iban, bic, tag, address)
  1498.  
  1499.     if !@user.enterprise
  1500.  
  1501.       reg = MangoPay::BankAccount.create(@user.mango_pay_id, {
  1502.  
  1503.                                                                Type: 'IBAN',
  1504.  
  1505.                                                                OwnerName: @user.first_name + ' ' + @user.last_name,
  1506.  
  1507.                                                                OwnerAddress: address,
  1508.  
  1509.                                                                IBAN: iban,
  1510.  
  1511.                                                                BIC: bic,
  1512.  
  1513.                                                                Tag: tag
  1514.  
  1515.                                                            })
  1516.  
  1517.     else
  1518.  
  1519.       reg = MangoPay::BankAccount.create(@user.mango_pay_id, {
  1520.  
  1521.                                                                Type: 'IBAN',
  1522.  
  1523.                                                                OwnerName: @user.username,
  1524.  
  1525.                                                                OwnerAddress: address,
  1526.  
  1527.                                                                IBAN: iban,
  1528.  
  1529.                                                                BIC: bic,
  1530.  
  1531.                                                                Tag: tag
  1532.  
  1533.                                                            })
  1534.  
  1535.     end
  1536.  
  1537.     @user.mango_pay_bank_id = reg['Id']
  1538.  
  1539.     @user.save
  1540.  
  1541.   end
  1542.  
  1543.  
  1544.  
  1545. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement