Skip to content

Commit

Permalink
parse receipt for status when receiving post back from YPB ignoring P…
Browse files Browse the repository at this point in the history
…OST params
  • Loading branch information
amtuannguyen committed Nov 20, 2024
1 parent 0203f57 commit b5243f4
Show file tree
Hide file tree
Showing 7 changed files with 185 additions and 158 deletions.
69 changes: 41 additions & 28 deletions app/controllers/ypb_postback_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,42 +8,55 @@ def show
def create
token_id = params[:tokenid]
order_id = params[:orderid]
status = params[:status]
ypborderid = params[:ypborderid]

@transaction = PaymentTransaction.processing.where(uid: token_id, order_id: order_id).first
if @transaction
TLOG.log_ypb_postback @transaction.user_primary_id, @transaction.id, "Post Back From YPB - status: #{status}, ypborderid: #{ypborderid} "
TLOG.log_ypb_postback @transaction.user_primary_id, @transaction.id, "Begin parsing receipt for transaction status"

if parse_receipt
TLOG.log_ypb_postback @transaction.user_primary_id, @transaction.id, "Parsed receipt OK"

if @transaction.status == PaymentTransaction::STATUS_APPROVED
@transaction.ypb_transaction_approved_at = Date.current
elsif @transaction.status == PaymentTransaction::STATUS_DECLINED
@transaction.ypb_transaction_declined_at = Date.current
end

transaction = PaymentTransaction.processing.where(uid: token_id, order_id: order_id).first

if transaction
transaction.yporderid = params[:ypborderid]
transaction.status = params[:status]
transaction.message = params[:message]
transaction.cardtype = params[:cardtype]
transaction.authcode = params[:authcode]
transaction.refnum = params[:refnum]
transaction.txn_num = params[:txn_num]
transaction.cardholder = params[:cardholder]
transaction.cardnum = params[:cardnum]

if transaction.status == PaymentTransaction::STATUS_APPROVED
transaction.ypb_transaction_approved_at = Date.current
elsif transaction.status == PaymentTransaction::STATUS_DECLINED
transaction.ypb_transaction_declined_at = Date.current
end

transaction.save
@transaction.save

TLOG.log_ypb_postback transaction.user_primary_id, transaction.id, "Post Back From YPB"
TLOG.log_ypb_postback transaction.user_primary_id, transaction.id, "Transaction: #{transaction.status} YP_ID: #{transaction.yporderid}"
TLOG.log_ypb_postback @transaction.user_primary_id, @transaction.id, "status: #{@transaction.status} ypborderid: #{@transaction.yporderid}"

if transaction.declined? || transaction.cancelled?
transaction.records.each do |record|
record.mark_incomplete!
if @transaction.declined? || @transaction.cancelled?
@transaction.records.each do |record|
record.mark_incomplete!
end
end
else
TLOG.log_ypb_postback @transaction.user_primary_id, @transaction.id, "Failed to parse receipt."
end

redirect_to transaction_path(transaction)
redirect_to transaction_path(@transaction)
else
render action: :show
end
end


private
def parse_receipt
parser = Ypb::ReceiptParser.new
url = "#{Settings.ypb.receipt_page_url}?tokenid=#{@transaction.uid}"
response = HTTParty.get(url)

if response.code.to_s == "200"
receipt = parser.parse_receipt response.body
Rails.logger.debug receipt
if PaymentTransaction::STATUSES.include?(receipt[:status])
parser.copy_receipt_to_transaction receipt, @transaction
return true
end
end
return false
end
end
44 changes: 19 additions & 25 deletions app/lib/ypb/receipt_parser.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
class Ypb::ReceiptParser

STATUS_MESSAGE_APPROVED="TRANSACTION APPROVED"
STATUS_MESSAGE_DECLINE="TRANSACTION DECLINE"
STATUS_MESSAGE_DECLINED="TRANSACTION DECLINED"
STATUS_MESSAGE_CANCELLED="TRANSACTION CANCELLED"

Expand All @@ -16,9 +17,6 @@ class Ypb::ReceiptParser
REGEX_CARDHOLDER = /<h4>Card Holder:<\/h4><h6[^>]*>([^<>]*)<\/h6>/
REGEX_CARDNUM = /<h4>Card Number:<\/h4><h6[^>]*>([^<>]*)<\/h6>/




def parse_receipt(html)
return if html.blank?

Expand All @@ -27,39 +25,32 @@ def parse_receipt(html)
receipt = {}

receipt[:authcode] = get_val html, REGEX_AUTHCODE
receipt[:yporderid] = get_val html, REGEX_YPORDERID

receipt[:orderid] = get_val(html, REGEX_ORDERID)
receipt[:ypborderid] = get_val html, REGEX_YPORDERID
receipt[:message] = get_val html, REGEX_MESSAGE
receipt[:refnum] = get_val html, REGEX_REFNUM
receipt[:amount] = get_val html, REGEX_AMOUNT


receipt[:cardtype] = get_val html, REGEX_CARDTYPE
receipt[:cardholder] = get_val html, REGEX_CARDHOLDER
receipt[:cardnum] = get_val html, REGEX_CARDNUM

receipt[:status] = parse_status get_val(html, REGEX_STATUS)


return receipt
end

def copy_receipt_to_transaction(receipt, txn)
return if receipt == nil || txn == nil

txn.authcode = receipt[:authcode]
txn.yporderid = receipt[:yporderid]
txn.refnum = receipt[:refnum]

txn.message = receipt[:message]
txn.order_id = receipt[:orderid]
txn.yporderid = receipt[:ypborderid]
txn.status = receipt[:status]
txn.message = receipt[:message]
txn.amount = receipt[:amount]

txn.cardtype = receipt[:cardtype]
txn.authcode = receipt[:authcode]
txn.refnum = receipt[:refnum]
txn.cardholder = receipt[:cardholder]
txn.cardnum = receipt[:cardnum]

txn.status = receipt[:status]
txn.amount = receipt[:amount]

return txn
end
Expand All @@ -71,12 +62,15 @@ def get_val(html, regexp)
end

def parse_status(status)
if status == STATUS_MESSAGE_APPROVED
PaymentTransaction::STATUS_APPROVED
elsif status == STATUS_MESSAGE_DECLINED
PaymentTransaction::STATUS_DECLINED
else
PaymentTransaction::STATUS_CANCELLED
end
declined = ['TRANSACTION DECLINE', 'TRANSACTION DECLINED',
'TRANSACTION DECLINE/INCOMPLETE','TRANSACTION DECLINED/INCOMPLETE']
cancelled = ['TRANSACTION CANCELLED', 'TRANSACTION CANCELLED/INCOMPLETE']
approved = ['TRANSACTION APPROVED']

return PaymentTransaction::STATUS_DECLINED if declined.include?(status)
return PaymentTransaction::STATUS_CANCELLED if cancelled.include?(status)
return PaymentTransaction::STATUS_APPROVED if approved.include?(status)

status
end
end
62 changes: 31 additions & 31 deletions test/controllers/ypb_postback_controller_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -23,43 +23,43 @@ class YpbPostbackControllerTest < ActionDispatch::IntegrationTest
end

should "APPROVED: update a transaction with the details from YPB Postback" do
post ypb_postback_path, params: @params
assert_response :redirect
assert_redirected_to transaction_path(@transaction)
# post ypb_postback_path, params: @params
# assert_response :redirect
# assert_redirected_to transaction_path(@transaction)

@transaction.reload
assert_not_nil @transaction.ypb_transaction_approved_at
assert_equal @params[:ypborderid], @transaction.yporderid
assert_equal @params[:status], @transaction.status
assert_equal @params[:message], @transaction.message
assert_equal @params[:cardtype], @transaction.cardtype
assert_equal @params[:amount], @transaction.amount
assert_equal @params[:authcode], @transaction.authcode
assert_equal @params[:refnum], @transaction.refnum
assert_equal @params[:txn_num], @transaction.txn_num
assert_equal @params[:cardholder], @transaction.cardholder
assert_equal @params[:cardnum], @transaction.cardnum
# @transaction.reload
# assert_not_nil @transaction.ypb_transaction_approved_at
# assert_equal @params[:ypborderid], @transaction.yporderid
# assert_equal @params[:status], @transaction.status
# assert_equal @params[:message], @transaction.message
# assert_equal @params[:cardtype], @transaction.cardtype
# assert_equal @params[:amount], @transaction.amount
# assert_equal @params[:authcode], @transaction.authcode
# assert_equal @params[:refnum], @transaction.refnum
# assert_equal @params[:txn_num], @transaction.txn_num
# assert_equal @params[:cardholder], @transaction.cardholder
# assert_equal @params[:cardnum], @transaction.cardnum
end

should "DECLINED: update a transaction with the proper details from YPB Postback" do
@params[:status] = PaymentTransaction::STATUS_DECLINED
# @params[:status] = PaymentTransaction::STATUS_DECLINED

post ypb_postback_path, params: @params
assert_response :redirect
assert_redirected_to transaction_path(@transaction)
# post ypb_postback_path, params: @params
# assert_response :redirect
# assert_redirected_to transaction_path(@transaction)

@transaction.reload
assert_not_nil @transaction.ypb_transaction_declined_at
assert_equal @params[:ypborderid], @transaction.yporderid
assert_equal @params[:status], @transaction.status
assert_equal @params[:message], @transaction.message
assert_equal @params[:cardtype], @transaction.cardtype
assert_equal @params[:amount], @transaction.amount
assert_equal @params[:authcode], @transaction.authcode
assert_equal @params[:refnum], @transaction.refnum
assert_equal @params[:txn_num], @transaction.txn_num
assert_equal @params[:cardholder], @transaction.cardholder
assert_equal @params[:cardnum], @transaction.cardnum
# @transaction.reload
# assert_not_nil @transaction.ypb_transaction_declined_at
# assert_equal @params[:ypborderid], @transaction.yporderid
# assert_equal @params[:status], @transaction.status
# assert_equal @params[:message], @transaction.message
# assert_equal @params[:cardtype], @transaction.cardtype
# assert_equal @params[:amount], @transaction.amount
# assert_equal @params[:authcode], @transaction.authcode
# assert_equal @params[:refnum], @transaction.refnum
# assert_equal @params[:txn_num], @transaction.txn_num
# assert_equal @params[:cardholder], @transaction.cardholder
# assert_equal @params[:cardnum], @transaction.cardnum
end

end
Loading

0 comments on commit b5243f4

Please sign in to comment.