--- title: 'InitVNPay' disqus: hackmd --- InitVNPay API === Description: SAG call middle to init url payment for sdk ## Flow ```plantuml @startuml Group Proccess 1 User -> SagAPP: Request init SagAPP-> Middleware: pass INIT Request SagAPP <-- Middleware: return INIT Response end SagAPP -> VNPayAPP: redirect VNPay APP bill page User -> VNPayAPP: pay the bill VNPayAPP -> VNPayServer: pass billInfo VNPayServer -> VNPayServer: do payment Middleware <--VNPayServer : VNPayCallbackURL(Return URL for mobile APP) Middleware -> VNPayAPP: redirect VNPayApp and get status VNPayAPP -> SagAPP: show status of transaction SagAPP -> SagAPP: show PaymentDetail Page User <-- SagAPP: got result Group Proccess 2 Middleware <-- VNPayServer: VNPayPaymentNotify(IPN URL Update status payment in core and middleware) Middleware -> Core: Send payment result when success Middleware <-- Core: return OK or Error Middleware -> VNPayServer: Return OK or Error end @enduml ``` ## Proccess 1 Flow ```plantuml @startuml User -> SagAPP: Request init SagAPP-> MiddlewareinitVNPay: pass INIT Request MiddlewareinitVNPay -> MiddlewareinitVNPay: Hash request params(JSON,output) MiddlewareinitVNPay -> MiddlewareDB: Save data to core SagAPP <-- MiddlewareinitVNPay:將output return JSON Response SagAPP -> SagAPP: Get url SagAPP -> VNPayAPP: redirect to VNPay APP bill page(url) @enduml ``` 1. [VNPayPay SampleCode](https://sandbox.vnpayment.vn/apis/docs/thanh-toan-pay/pay.html) 2. [VNPayPay Response code](https://sandbox.vnpayment.vn/apis/docs/bang-ma-loi/) ## API 1. Name:**InitVNPay** 2. URL | Env | URL | | -------- | -------- | | Test API | https://api-sit.cathay-ins.com.vn:9527/utility/api/Payment/InitVNPay | | Test Gateway| https://api-sit.cathay-ins.com.vn:9527/utility/api/Payment/InitVNPay | | UAT API | https://api-uat.cathay-ins.com.vn:9527/utility/api/Payment/InitVNPay | ### Header request | | | | ----------------------- | --------------------------------- | | Method | POST | | Required Request Header | Content-Type : application/json; charset=UTF-8 | | Required Request Header | x-api-version=1 | | Bearer Token | 登入時取得access_token | ## InitVNPay ### Request ```json= { "loginId": "0300000739", "reference1": "202000000021", "reference2": "0300000739", "client": "ios_app", "callbackUrl": "smartagentgo://payment/pay-detail", "totalAmount": 10000000, "checksumKey": "ChecksumKey", "accountInfo": { "name": "What Your Name" }, "billList": [ { "billId": "202000000021", "totalAmount": 10000000, "description": "0300000739" } ] } ``` ### Response ```json= { "status": 200, "message": "成功", "success": true, "total": 1, "data": "https://sandbox.vnpayment.vn/paymentv2/vpcpay.html?vnp_Amount=1000000000&vnp_Command=pay&vnp_CreateDate=20230227102552&vnp_CurrCode=VND&vnp_ExpireDate=20230227104052&vnp_IpAddr=0%3A0%3A0%3A0%3A0%3A0%3A0%3A1&vnp_Locale=vn&vnp_OrderInfo=202000000021&vnp_OrderType=25000&vnp_ReturnUrl=https%3A%2F%2Fapi-sit.cathay-ins.com.vn%3A9527%2Futility%2Fapi%2FPayment%2FVNPayCallbackURL&vnp_TmnCode=CATHAYAP&vnp_TxnRef=55719318-5f04-4ecc-9144-c4bed27e4837&vnp_Version=2.1.0&vnp_SecureHash=819f02b5aeae29b1891a62d6110a339725f054eab3cc478d2a0ba4ea315cc26f7463d334b98b7d45e152df634bd46f30a72be4031ae5fe4fbc566f96d1c7f91e" } ``` ``` ## VNPayPay Response ### HTTP Status Codes | Status Code | Status Code | | -------- | -------- | | 200 | Success | | 400 | Malformed request | | 401 | Unauthorized | | 404 | Not found | | 500 | Server error | ``` ### Table vn_pay_record 1. Database for saving log record DB Field | Params | Type | Nullable ---- | --- | --- | --- seq_no |-| int | Y pd_no | request/reference1 | String | Y agnt_empno | request/reference2 | String | Y agnt_name | request/accountInfo.name | String | Y client_app | request/client | String | Y total_amount | request/totalAmount | Number | Y pay_status | | String | Y result_code | resultCode | String | Y result_message | resultMessage | String | Y payment_id | paymentId | String | Y payment_date | paymentDate | DateTime | Y create_date | - | String | Y update_date | | String | Y updated_core | - | String | Y ###### tags: `agentportal` `Documentation`