Skip to content

Instantly share code, notes, and snippets.

@wildjcrt
Created February 15, 2017 11:06
Show Gist options
  • Save wildjcrt/24daea8b1bd51f954b4964e7e70faa1c to your computer and use it in GitHub Desktop.
Save wildjcrt/24daea8b1bd51f954b4964e7e70faa1c to your computer and use it in GitHub Desktop.
HASH_KEY = YOUR_KEY
HASH_IV = YOUR_IV
STORE = YOUR_STORE_ID
# 假定你有 web 傳回的 params
Digest::SHA256.hexdigest("HashIV=#{HASH_IV}&InvoiceTransNo=#{params[:InvoiceTransNo]}&MerchantID=#{params[:MerchantID]}&MerchantOrderNo=#{params[:MerchantOrderNo]}&RandomNum=#{params[:RandomNum]}&TotalAmt=#{params[:TotalAmt]}&HashKey=#{HASH_KEY}").upcase
HASH_KEY = YOUR_KEY
HASH_IV = YOUR_IV
STORE = YOUR_STORE_ID
query = {
"RespondType" => "JSON",
"Version" => "1.1",
"TimeStamp" => "1444963784", # 請以 time() 格式
"TransNum" => "",
"MerchantOrderNo" => "201409170000001",
"BuyerName" => "王大品",
"BuyerUBN" => "99112233",
"BuyerAddress" => "台北市南港區南港路一段 99 號",
"BuyerEmail" => "[email protected]",
"BuyerPhone" => "0955221144",
"Category" => "B2B",
"TaxType" => "1",
"TaxRate" => "5",
"Amt" => "490",
"TaxAmt" => "10",
"TotalAmt" => "500",
"CarrierType" => "",
"CarrierNum" => "",
"LoveCode" => "",
"PrintFlag" => "Y",
"ItemName" => "商品一|商品二", # 多項商品時,以「|」分開
"ItemCount" => "1|2", # 多項商品時,以「|」分開
"ItemUnit" => "個|個", # 多項商品時,以「|」分開
"ItemPrice" => "300|100", # 多項商品時,以「|」分開
"ItemAmt" => "300|200", # 多項商品時,以「|」分開
"Comment" => "TEST,備註說明",
"Status" => "1", # 1=立即開立,0=待開立,3=延遲開立
"CreateStatusTime" => "",
"NotifyEmail" => "1", # 1=通知,0=不通知
}.to_query
cipher = OpenSSL::Cipher::AES256.new(:CBC)
cipher.encrypt
cipher.key = HASH_KEY
cipher.iv = HASH_IV
crypt = cipher.update(query) + cipher.final
encrypt_data = crypt.unpack("H*").first
uri = URI("https://inv.pay2go.com/API/invoice_issue")
req = Net::HTTP::Post.new(uri.path)
req.set_form_data(:MerchantID_ => STORE,
:PostData_ => encrypt_data)
sock = Net::HTTP.new(uri.host, uri.port)
sock.use_ssl = true
res = sock.start {|http| http.request(req) }
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment