Class: PaymentsController

Inherits:
ApplicationController
  • Object
show all
Defined in:
lib/generators/templates/payments_controller.rb

Instance Method Summary collapse

Instance Method Details

#createObject



8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
# File 'lib/generators/templates/payments_controller.rb', line 8

def create
  @payment = make_payment

  if params[:type] == "vtweb"
    @result = Veritrans.charge(
      payment_type: "VTWEB",
      transaction_details: {
        order_id: @payment.order_id,
        gross_amount: @payment.amount
      }
    )
    redirect_to @result.redirect_url
    return
  end

  @result = Veritrans.charge(
    payment_type: "credit_card",
    credit_card: { token_id: params[:payment][:token_id] },
    transaction_details: {
      order_id: @payment.order_id,
      gross_amount: params[:payment][:amount].presence || @payment.amount
    }
  )
end

#newObject



4
5
6
# File 'lib/generators/templates/payments_controller.rb', line 4

def new
  @payment = make_payment
end

#receive_webhookObject



33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
# File 'lib/generators/templates/payments_controller.rb', line 33

def receive_webhook
  post_body = request.body.read

  Veritrans.file_logger.info("Callback for order: " +
    "#{params[:order_id]} #{params[:transaction_status]}\n" +
    post_body + "\n"
  )

  verified_data = Veritrans.status(params["transaction_id"])

  if verified_data.status_code != 404
    puts "--- Transaction callback ---"
    puts "Payment:        #{verified_data.data[:order_id]}"
    puts "Payment type:   #{verified_data.data[:payment_type]}"
    puts "Payment status: #{verified_data.data[:transaction_status]}"
    puts "Fraud status:   #{verified_data.data[:fraud_status]}" if verified_data.data[:fraud_status]
    puts "Payment amount: #{verified_data.data[:gross_amount]}"
    puts "--- Transaction callback ---"

    render text: "ok"
  else
    Veritrans.file_logger.info("Callback verification failed for order: " +
      "#{params[:order_id]} #{params[:transaction_status]}}\n" +
      verified_data.body + "\n"
    )

    render text: "ok", :status => :not_found
  end

end