Class: Api::V5::VoucherTransactionsController

Inherits:
BaseController
  • Object
show all
Includes:
Concerns::Authorization, Concerns::PaginationParam
Defined in:
app/controllers/api/v5/voucher_transactions_controller.rb

Constant Summary

Constants inherited from BaseController

BaseController::CACHE_NAMESPACE, BaseController::INTERNAL_SERVER_ERROR_MESSAGE, BaseController::ResponseSchema

Instance Method Summary collapse

Methods inherited from BaseController

#identity_cache_memoization

Methods included from LogrageCustomLogger

#append_info_to_payload

Methods included from ResponseCacheConcern

#my_response_cache

Instance Method Details

#createObject



24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
# File 'app/controllers/api/v5/voucher_transactions_controller.rb', line 24

def create
  transaction_params = {
    user_id: params.key?(:access_token) && params[:access_token].present? && authorize! && user_signed_in? ? current_user.id : nil,
    guest_user: if params.key?(:guest_user) && params[:guest_user].present?
                  params.require(:guest_user).permit(:name,
                                                     :email, :phone)
                end,
    vouchers: params.require(:vouchers).permit(custom_amount: %i[amount quantity], packages: %i[id quantity]),
    restaurant_id: params.key?(:restaurant_id) ? params.permit(:restaurant_id) : {},
    country_code: params.fetch(:country_code, nil),
    gb_primepay_card: params.fetch(:gb_primepay_card, {}),
    omise_token: params.fetch(:omise_token, ''),
    payment_type: params.key?(:payment_type) && params[:payment_type].present? ? params[:payment_type] : nil,
    use_3d_secure: params.fetch(:use_3d_secure, false),
    ip_address: request.remote_ip,
  }

  service = VoucherService::CreateTransaction.new(transaction_params)
  result = service.execute

  if result.success?
    set_status_header(true)
    options = { include: [:vouchers] }
    render json: resource_as_json(result.data, Api::V5::VoucherTransactionSerializer, options).merge(
      success: true,
      message: 'Purchase vouchers success',
    )
  else
    set_status_header(false)
    render json: { success: false, message: service.error_message_simple, data: nil }
  end
end

#showObject



10
11
12
13
14
15
16
17
18
19
20
21
22
# File 'app/controllers/api/v5/voucher_transactions_controller.rb', line 10

def show
  voucher_transaction = VoucherTransaction.find(VoucherTransaction.decrypt_id(params[:id]))
  my_response_cache voucher_transaction.cache_key, :json do
    JSON.parse(
      render_to_string(
        json: voucher_transaction,
        serializer: Api::V5::VoucherTransactionSerializer,
        adapter: :json_api,
        include: [:vouchers],
      ),
    ).merge(success: true)
  end
end