Class: WechatPaymentService

Inherits:
Object
  • Object
show all
Includes:
OmiseHelper
Defined in:
app/services/wechat_payment_service.rb

Overview

Service for creating WeChat Pay payments via Omise or GB PrimePay

Supports WeChat Pay QR code payments across Thailand, Singapore, and Malaysia:

  • Thailand (THA): WeChat Pay via Omise or GB PrimePay

  • Singapore (SGP): WeChat Pay via Omise

  • Malaysia (MYS): WeChat Pay via Omise

The service automatically configures the correct Omise keys based on the restaurant's country code (THA, SGP, MYS) to ensure payments are processed through the appropriate regional Omise account.

Examples:

Creating a WeChat Pay payment for Malaysia

reservation = Reservation.find(id) # Malaysia restaurant
service = WechatPaymentService.new(reservation, 'omise')
service.execute # Creates charge using Malaysia Omise credentials

Instance Method Summary collapse

Methods included from OmiseHelper

#configure_omise_keys, #omise_public_key

Constructor Details

#initialize(reservation, provider) ⇒ WechatPaymentService

Note:

When provider is 'omise', the service automatically configures country-specific Omise keys based on restaurant.country.alpha3:

  • Thailand (THA): Uses OMISE_SKEY_THA and OMISE_VAULT_KEY_THA

  • Singapore (SGP): Uses OMISE_SKEY_SGP and OMISE_VAULT_KEY_SGP

  • Malaysia (MYS): Uses OMISE_SKEY_MYS and OMISE_VAULT_KEY_MYS

Initialize the service with reservation and payment provider

Parameters:

  • reservation (Reservation)

    The reservation to create payment for

  • provider (String)

    Payment provider - :gb_primepay or :omise



29
30
31
32
33
34
35
36
37
38
39
# File 'app/services/wechat_payment_service.rb', line 29

def initialize(reservation, provider)
  @reservation = reservation
  @provider = provider

  # configure omise keys based on restaurant country code
  if reservation.restaurant.present? && provider.to_s == Externals::Omise::Source::OMISE_SOURCE_ID
    configure_omise_keys(reservation.restaurant.country.alpha3)
  end

  BUSINESS_LOGGER.set_business_context({ reservation_id: reservation.id })
end

Instance Method Details

#executeObject



41
42
43
44
45
46
47
48
49
50
51
# File 'app/services/wechat_payment_service.rb', line 41

def execute
  return true if reservation.charge_amount.to_i.zero?

  success = false
  ActiveRecord::Base.transaction do
    reservation.charges.build(charge_attribute(reservation, provider))
    success = true
  end
  BUSINESS_LOGGER.info 'Done processing WeChat Payment', reservation_id: reservation.id
  success
end

#normalize_phone_number(phone) ⇒ Object



53
54
55
56
57
58
# File 'app/services/wechat_payment_service.rb', line 53

def normalize_phone_number(phone)
  parsed_phone = Phonelib.parse(phone)
  return phone unless parsed_phone.country_code != 0

  parsed_phone.national.gsub(' ', '')
end