Thanh toán qua Thẻ/Tài khoản Ngân hàng nội địa

1. Giới thiệu

Cùng với sự phát triển nhanh chóng của thương mại điện tử, nhu cầu mua sắm online của người tiêu dùng ngày càng tăng cao. Để đáp ứng nhu cầu thanh toán online này, TrueMoney phát triển hệ thống Cổng thanh toán online TrueMoney Pay, cung cấp các dịch vụ thanh toán thông qua Thẻ ATM nội địa, Thẻ thanh toán quốc tế (VISA, Master, JCB) và Ví điện tử TrueMoney. Không chỉ đem đến sự tiện lợi trong thanh toán cho người tiêu dùng, chúng tôi còn cung cấp sự tiện lợi trong tích hợp, vận hành, đối soát và thanh toán cho doanh nghiệp.

Đối với phương thức thanh toán qua Thẻ ATM nội địa, sau khi tích hợp, website/ứng dụng của doanh nghiệp có thể hiển thị nút thanh toán TrueMoney Pay để khách hàng hoàn tất thanh toán chỉ trong vài bước đơn giản:

  • Khách hàng lựa chọn phương thức thanh toán qua Thẻ ATM nội địa và click nút thanh toán.
  • Khách hàng được điều hướng tới Website của TrueMoney Pay và lựa chọn ngân hàng. Tại đây, tùy theo ngân hàng, khách hàng có thể nhập số thẻ/số tài khoản hoặc đăng nhập tài khoản internet banking để xác nhận thanh toán đơn hàng.
  • Sau khi thanh toán hoàn tất, khách hàng sẽ được điều hướng trở lại trang mua hàng của doanh nghiệp cùng với kết quả thanh toán. Doanh nghiệp có thể kiểm tra kết quả thanh toán và thực hiện các bước tiếp theo.

2. Mô hình

TrueMoney Pay cung cấp Dịch vụ thanh toán thông qua ATM. Sau khi tích hợp thành công, Merchant có thể cung cấp phương thức thanh toán cho End-user qua các bước đơn giản sau.

Bước 1: End-user mua hàng trên Website/App của Merchant và lựa chọn thanh toán qua Thẻ ATM nội địa.

Bước 2: End-user được chuyển hướng tới trang thanh toán của TrueMoney Pay và lựa chọn ngân hàng thanh toán.

Bước 3: End-user nhập thông tin cá nhân.

Bước 4: Tùy theo ngân hàng, end-user được yêu cầu nhập Số thẻ/Số tài khoản và tài khoản Internet Banking để thực hiện các bước xác thực giao dịch.

Bước 5: End-user nhập OTP xác thực giao dịch.

Bước 6: End-user nhận thông báo thanh toán thành công và được chuyển hướng về trang mua hàng của Merchant.

 

3. Hướng dẫn

Các bước thực hiện:

  1. End-user click nút Thanh toán trên website/app của Merchant để thanh toán đơn hàng.
  2. Merchant thực hiện gọi web service (2) do-payment để lấy payment_url và trans_ref.
  3. Merchant chuyển hướng end-user tới payment_url (được TrueMoney Pay trả về khi gọi request do-payment).
  4. TrueMoney Pay hiển thị trang thanh toán, thực hiện giao dịch với end-user và hiển thị kết quả giao dịch cho end-user. TrueMoney Pay sẽ chuyển hướng end-user về return_url, đồng thời trả kết quả giao dịch cho Merchant qua return_url sau khi giao dịch kết thúc.
  5. TrueMoney Pay trả kết quả cho server của merchant qua IPN_url.
  6. Trong trường hợp quá 15 phút không nhận được kết quả giao dịch, Merchant có thể thực hiện gọi request transaction-detail tới TrueMoney Pay để kiểm tra kết quả giao dịch.

4. APIs kết nối

Mô tả chi tiết

(*): Những tham số bắt buộc phải truyền giá trị.

Tham số truyền dưới dạng Json Object.

(2) Request Do-payment

Địa chỉ nhận Request Charging:

POST  https://partner.truemoney.com.vn/paymentgw/api/v1/do-payment

 

Max request time out: 30 giây

Tham số Mô tả
access_key*
string(255)
Đại diện cho sản phẩm của merchant khai báo trong hệ thống TrueMoney Pay.
amount*
Bigdecimal
Số tiền thanh toán hóa đơn. Yêu cầu số tiền thanh toán chẵn đồng.
order_id*
String(50)
Mã hóa đơn là duy nhất, đại diện cho giao dịch, không vượt quá 50 kí tự. Tiếng Việt không dấu (A-z, 0-9).
order_info*
String(100)
Mô tả hóa đơn. Tiếng Việt không dấu, không quá 100 kí tự. (A-z,0-9 space).
return_url
String(255)
Địa chỉ url sau khi thực hiện thanh toán sẽ được redirect về, merchant cần xây dựng để nhận kết quả từ TrueMoney Pay gửi sang. Request do hệ thống TrueMoney Pay System gửi sang sẽ ở dạng HTTP GET. Nếu không có dữ liệu thì sẽ lấy theo setting của merchant với TrueMoney Pay.
bank_code
String(50)
Mã ngân hàng do TrueMoney Pay định nghĩa(Sử dụng trong trường hợp chọn phương thức thanh qua ATM). Kết nối Wallet Charging thì truyền giá trị rỗng.
wallet_code
String(50)
Mã ví do TrueMoney Pay định nghĩa(Sử dụng trong trường hợp chọn phương thức thanh toán qua ví).
payment_type*
Int
0 (Nếu thanh toán bằng thẻ atm), 1 (Nếu thanh toán bằng thẻ quốc tế), hoặc 2 (thanh toán qua ví). Đối với kết nối Bank Charging thì truyền giá trị 0.
customer_name
String(255)
Tên người thanh toán.
customer_email
String(50)
Email người thanh toán.
customer_phone
String(50)
Số điện thoại người thanh toán.
language
String(2)
vi(Việt Nam),en(English). Nếu Merchant không yêu cầu ngôn ngữ thì sẽ hiển thị ngôn ngữ mặc định là Tiếng Việt
signature*
String(255)
Chữ ký, merchant có thể sử dụng signature để kiểm soát an ninh
Signature là một chuỗi string
access_key=$access_key&amount=$amount&order_id=$order_id&order_info=$order_info&return_url=$return_url
được hmac bằng thuật toán SHA256.

 

(2′) Response Do-payment

Là một chuỗi json có chứa các giá trị sau:

{"response_code":"[code]","response_message":"[message]", "data":{"payment_url":"[url]","trans_ref":"[Mã giao dịch]"},"signature":"[Chữ kí]"}

 

Trong đó

Tham số Mô tả
response_code Map với bảng mã lỗi. Bảng 1
response _message Thông báo chi tiết trạng thái kết nối. (sai chữ kí, sai access_key…)
payment_url Địa chỉ url để thực hiện submit request (redirect).
trans_ref Mã do TrueMoney Pay sinh ra. Mã giao dịch dùng để commit request, recheck và lưu lại trên hệ thống
signature Chữ ký, merchant có thể sử dụng signature để kiểm soát an ninh
Signature là một chuỗi string
response_code=$response_code&response_message=$response_message
được hmac bằng thuật toán SHA256.

 

(3) Submit(redirect) request

Thực hiện redirect request đến địa chỉ payment_url nhận được từ (2′), hệ thống TrueMoney Pay sẽ hiển thị nhập thông tin thẻ và thực hiện giao dịch với end user.

TrueMoney Pay sẽ trả kết quả giao dịch đồng thời qua link return_url và IPN

(3′) Response to return_url

Sau khi end user thực hiện thanh toán thẻ, TrueMoney Pay sẽ trả kết quả về cho Merchant qua địa chỉ return_url khai báo ở (2).

Tham số Mô tả
access_key
String(255)
Đại diện cho sản phẩm của merchant khai báo trong hệ thống TrueMoney Pay.
order_id
String(50)
Mã hóa đơn.
trans_ref
String(50)
Mã do TrueMoney Pay.
payment_type
Int
0 (Nếu thanh toán bằng Thẻ ATM nội địa),
1 (Nếu thanh toán bằng Thẻ quốc tế), hoặc 2 (thanh toán qua Ví).
result_code
String(10)
Mã kết quả của giao dịch. (Bảng mã lỗi 1)
description
String(255)
Mô tả thông tin giao dịch.
language
String(2)
vi (Việt Nam), en (English).
wallet_id
String(50)
Mã Ví thực hiện thanh toán (được trả về trong trường hợp thanh toán qua Ví TrueMoney).
signature
String(255)
Chữ ký, merchant có thể sử dụng signature để kiểm soát an ninh
Signature là một chuỗi string
access_key=$access_key &order_id=$order_id&trans_ref=$trans_ref&payment_type=$payment_type&result_code=$result_code
được hmac bằng thuật toán SHA256.

 

(5) Send IPN (Instance Payment Notification)

Sau khi end user thực hiện thanh toán thẻ, TrueMoney Pay sẽ trả kết quả về cho Merchant qua địa chỉ IPN đã khai báo trên form đăng kí (Qua phương thức POST).

TrueMoney Pay sẽ gọi IPN của merchant tối đa 3 lần, mỗi lần cách nhau 5 phút.

Tham số Mô tả
access_key
String(255)
Đại diện cho sản phẩm của merchant khai báo trong hệ thống TrueMoney Pay.
trans_ref
String(50)
Mã do TrueMoney Pay.
amount
Bigdecimal
Số tiền thanh toán.
order_id
String(50)
Mã hóa đơn do Merchant truyền sang khi request tạo giao dịch.
order_info
String(100)
Mô tả hóa đơn do Merchant truyền sang.
payment_type
Int
0 (Nếu thanh toán bằng Thẻ ATM nội địa),
1 (Nếu thanh toán bằng Thẻ quốc tế), hoặc 2 (thanh toán qua Ví).
bank_code
String(50)
Mã ngân hàng thanh toán. (Bảng mã lỗi 4)
wallet_code
String(50)
Mã ví thanh toán
payment_time
String(50)
Thời gian hoàn thành giao dịch(định dạng hh:mm:ss dd-mm-yyyy)
result_code
String(10)
Map với bảng mã lỗi kết quả giao dịch bảng 1
description
String(255)
Miêu tả chi tiết lỗi.
language
String(2)
vi (Việt Nam), en (English).
wallet_id
String(50)
Mã Ví thực hiện thanh toán (được trả về trong trường hợp thanh toán qua Ví TrueMoney).
signature Chữ ký, merchant có thể sử dụng signature để kiểm soát an ninh
Signature là một chuỗi string
access_key=$access_key&order_id=$order_id&trans_ref=$trans_ref&payment_type=$payment_type&result_code=$result_code
được hmac bằng thuật toán SHA256.

 

(5′) Response IPN

Merchant sau khi nhận được kết quả qua IPN. merchant trả về cho TrueMoney Pay một kết quả là một chuỗi json có chứa các giá trị sau:

{"response_code":"[code]","response _message":"[message]"}

 

Trong đó

Tham số Mô tả
response_code Merchant cần gửi lại response để hệ thống TrueMoney Pay nhận biết trạng thái ghi nhận kết quả giao dịch của Merchant. Yêu cầu sử dụng mã lỗi được định nghĩa cho IPN (Bảng mã lỗi 2). Nếu Merchant trả lại mã 00 hoặc 01, TrueMoney sẽ dừng gửi IPN. Nếu không, TrueMoney sẽ gửi IPN cho Merchant tất cả 3 lần, mỗi lần cách nhau 5 phút.
response _message Miêu tả chi tiết lỗi.

 

(6) Request Transaction- Detail

Thực hiện lệnh để lấy kết quả (trong trường hợp không nhận được kết quả giao dịch):

POST  https://partner.truemoney.com.vn/paymentgw/api/v1/transaction-detail

 

Tham số Mô tả
access_key* Đại diện cho sản phẩm của merchant khai báo trong hệ thống TrueMoney Pay.
trans_ref* Mã giao dịch do TrueMoney Pay sinh ra.
signature* Chữ ký, merchant có thể sử dụng signature để kiểm soát an ninh.
Signature là một chuỗi string:
access_key=$access_key&trans_ref=$trans_ref
được hmac bằng thuật toán SHA256.

 

Là một chuỗi json có chứa các giá trị bao gồm

{"response_code":"[code]","response_message":"[message]", "data":{"trans_ref":"[Mã giao dịch]","amount":"[Số tiền]","order_id":"[Mã hóa đơn]","order_info":"[Mô tả]","payment_type":"[0]","bank_code":"[Mã ngân hàng]","wallet_code":"[Mã ví thanh toán]","status":"[Trạng thái]","refund_status":"[Trạng thái hoàn tiền]","request_time":"[Thời gian bắt đầu]","finish_time":"[Thời gian kết thúc]","result_code":"[Mã kết quả]","description":"[Chi tiết]"},"signature":"[Chữ kí]"}

 

Trong đó

Tham số Mô tả
response_code Mã lỗi bảng 3.
response_message Thông báo chi tiết .
trans_ref Mã giao dịch do TrueMoney Pay sinh ra.
amount Số tiền giao dịch .
order_id Mã hóa đơn .
order_info Mô tả hóa đơn .
payment_type 0 (Nếu thanh toán bằng Thẻ ATM nội địa),
1 (Nếu thanh toán bằng Thẻ quốc tế), hoặc 2 (thanh toán qua Ví).
bank_code Mã ngân hàng thanh toán.
wallet_code Mã ví thanh toán
status init(khởi tạo), processing(đang xử lý), success(thành công), fail(lỗi), timeout(hết hạn giao dịch).
refund_status Trạng thái refund: init(khởi tạo) ,success(thànhcông),fail(lỗi),timeout(hết hạn giao dịch).
request_time Thời gian bắt đầu(định dạng hh:mm:ss dd-mm-yyyy).
finish_time Thời gian hoàn thành giao dịch(định dạng hh:mm:ss dd-mm-yyyy) .
result_code Mã kết quả của giao dịch. (Bảng mã lỗi 1).
description Chi tiết message kết quả giao dich.
signature* Chữ ký, merchant có thể sử dụng signature để kiểm soát an ninh.
Signature là một chuỗi string:
response_code=$response_code&response_message=$response_message
được hmac bằng thuật toán SHA256.

 

Bảng mã lỗi thanh toán qua Thẻ nội địa,Thẻ quốc tế.

Bảng 1. Mã lỗi kết quả giao dịch

Kết quả giao dịch Mô tả
00 Giao dịch thành công.
01 Merchant không hợp lệ (Sai access key).
02 Dữ liệu gửi sang không đúng định dạng.
03 Ngân hàng không được hỗ trợ.
04 Chữ ký không hợp lệ.
05 Tài khoản khách hàng không đủ điều kiện thanh toán.
06 Số tiền giao dịch không hợp lệ(nhỏ hơn hoặc vượt quá hạn mức).
07 Giao dịch nhỏ hơn hạn mức tối thiểu một lần thanh toán.
08 Giao dịch vượt quá hạn mức tối đa một lần thanh toán.
09 Giao dịch vượt quá hạn mức tối đa thanh toán ngày/tuần/tháng.
10 Khách hàng nhập sai OTP quá số lần cho phép.
11 Giao dịch vượt quá số lần tối đa bỏ qua xác thực OTP trong ngày cho một dịch vụ.
12 Khách hàng hủy giao dịch.
13 Giao dịch không thành công do quá thời gian quy định.
14 Lỗi giao dịch,cần kiểm tra lại với ngân hàng.
15 Giao dịch chờ xác nhận từ ngân hàng.
16 Lỗi kết nối đến ngân hàng.
17 Hệ thống đang bảo trì.
18 Địa chỉ IP không được phép truy cập.
19 Không tìm thấy giao dịch.
24 Số lần giao dịch vượt quá hạn mức tối đa ngày/tuần/tháng.
25 Ngân hàng từ chối giao dịch.
26 Thẻ thanh toán nằm trong dánh sách nghi vấn,giao dịch bị tạm giữ tiền để kiểm tra.
99 Lỗi không xác định được nguyên nhân,vui lòng liên hệ để tra soát.

 

Bảng 2. Mã lỗi Merchant trả kết quả cập nhật Instant Payment Notification.

Kết quả giao dịch Mô tả
00 Ghi nhận kết quả giao dịch thành công.
01 Yêu cầu đã được xử lý trước đó.
02 Không tìm thấy mã đơn hàng.
99 Lỗi không xác định được nguyên nhân,vui lòng liên hệ để tra soát.

 

Bảng 3. Mã lỗi kiểm tra kết quả giao dịch

Kết quả giao dịch Mô tả
00 Thành công.
01 Merchant không hợp lệ (Sai access key).
02 Dữ liệu gửi sang không đúng định dạng.
04 Chữ ký không hợp lệ.
17 Hệ thống đang bảo trì.
18 Địa chỉ IP không được phép truy cập.
19 Không tìm thấy giao dịch.
99 Lỗi không xác định được nguyên nhân,vui lòng liên hệ để tra soát.

 

Bảng 4. Bảng danh sách Ngân hàng hỗ trợ

5. Code mẫu

 

package vn.connectbank.controller;

import java.io.UnsupportedEncodingException;
import java.math.BigDecimal;
import java.security.InvalidKeyException;
import java.security.NoSuchAlgorithmException;

import javax.crypto.Mac;
import javax.crypto.spec.SecretKeySpec;

import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders;
import org.springframework.http.MediaType;
import org.springframework.web.client.RestTemplate;

public class CodeExample {

  public static void main(String[] args) {
    CodeExample ex = new CodeExample();
    String paymentUrl = ex.doPayment();
    System.out.println("url thanh toan = "+paymentUrl);
  }
  
  public String doPayment() {
    // url nhan duoc sau khi request thanh cong
    String paymentUrl = "";
    
    /*Thong tin ket noi*/
    String requestUrl = "https://partner.truemoney.com.vn/paymentgw/api/v1/do-payment";
    String accessKey = "";
    String secretKey = "";
    /*end Thong tin ket noi*/
    
    /* Thong tin don hang  */
    // orderId duy nhat ,tu sinh theo thuat toan cua merchant
    String orderId = "";
    // noi dung thong tin don hang kem theo
    String orderInfo = "";
    // merchant dung url nhat ket qua trang thai giao dich.
    String returnUrl = "https://ecom.truemoney.com.vn/";
    // so tien giao dịch
    BigDecimal amount = new BigDecimal(100000);
    //phuong thuc thanh toan . Thanh toan qua vi paymentType=2, qua ngan hang noi dia paymentType=0, qua visa paymentType=1
    String paymentType = "2";
    // ma vi muon thanh toan ,bat buoc neu payment_type=2
    String walletCode = "TRUEMONEY";
    //bank_code co the truyen hoac khong, neu khong truyen se ra man hinh chon bank, neu truyen bank_code se ra trang cua nha cung cap
    String bankCode = "";
    // ngon ngu tieng viet hoac tieng anh (vi or en)
    String language = "vi";
    // chu ki dien tu sinh theo thuat toan HmacSHA256
    String signature = generateSignatures(accessKey, amount, orderId, orderInfo, returnUrl, secretKey);
    /*end thong tin don hang */
    
    PaymentRequest requestPayment = new PaymentRequest();
    requestPayment.setAmount(amount);
    requestPayment.setAccess_key(accessKey);
    requestPayment.setOrder_id(orderId);
    requestPayment.setOrder_info(orderInfo);
    requestPayment.setPayment_type(paymentType);
    requestPayment.setWallet_code(walletCode);
    requestPayment.setBank_code(bankCode);
    requestPayment.setReturn_url(returnUrl);
    requestPayment.setSignature(signature);
    requestPayment.setLanguage(language);
    try {
      PaymentResponse responsePayment = postToHTTPSAddress(requestUrl, requestPayment, PaymentResponse.class);
      if (responsePayment != null) {
        String responseCode = responsePayment.getResponse_code();
        if (responseCode.equals("00")) {
          PaymentBody body = responsePayment.getData();
          if (body != null) {
            paymentUrl = body.getPayment_url();
          }
        }else {
          // merchant handle truong hop request giao dich that bai
        }
      }
    } catch (Exception e) {
      e.printStackTrace();
    }
    return paymentUrl;
  }
  

  public  T postToHTTPSAddress(String url, K req, Class type) throws Exception {
    RestTemplate restTemplate = new RestTemplate();
    try {
      HttpHeaders headers = new HttpHeaders();
      headers.set("Content-Type", MediaType.APPLICATION_JSON_VALUE);
      headers.add("User-Agent",
          "Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/50.0.2661.102 Safari/537.36");

      HttpEntity request = new HttpEntity(req, headers);

      T obj = restTemplate.postForObject(url, request, type);
      return obj;
    } catch (Exception e) {
      e.printStackTrace();
    }

    return null;
  }

  public String generateSignatures(String access_key, BigDecimal amount, String orderId, String orderInfo,
      String returnUrl, String secretkey) {

    String requestParam = "access_key=" + access_key + "&amount=" + amount + "&order_id=" + orderId + "&order_info="
        + orderInfo + "&return_url=" + returnUrl;

    return hmacDigest(requestParam, secretkey, "HmacSHA256");
  }

  public static String hmacDigest(String msg, String keyString, String algo) {
    String digest = "";
    try {
      if (keyString != null && keyString.length() > 0) {
        SecretKeySpec key = new SecretKeySpec((keyString).getBytes("UTF-8"), algo);
        Mac mac = Mac.getInstance(algo);
        mac.init(key);
        byte[] bytes = mac.doFinal(msg.getBytes("ASCII"));
        StringBuffer hash = new StringBuffer();
        for (int i = 0; i < bytes.length; i++) {
          String hex = Integer.toHexString(0xFF & bytes[i]);
          if (hex.length() == 1) {
            hash.append('0');
          }
          hash.append(hex);
        }
        digest = hash.toString();
      }
    } catch (UnsupportedEncodingException e) {
      e.printStackTrace();
    } catch (NoSuchAlgorithmException e) {
      e.printStackTrace();
    } catch (InvalidKeyException e) {
      e.printStackTrace();
    }
    return digest;
  }
  
  
  class PaymentRequest {

    private String access_key;

    private BigDecimal amount;

    private String order_id;

    private String order_info;

    private String return_url;

    private String bank_code;

    private String wallet_code;

    private String payment_type;

    private String language;

    private String signature;

    public String getAccess_key() {
      return access_key;
    }

    public void setAccess_key(String access_key) {
      this.access_key = access_key;
    }

    public BigDecimal getAmount() {
      return amount;
    }

    public void setAmount(BigDecimal amount) {
      this.amount = amount;
    }

    public String getOrder_id() {
      return order_id;
    }

    public void setOrder_id(String order_id) {
      this.order_id = order_id;
    }

    public String getOrder_info() {
      return order_info;
    }

    public void setOrder_info(String order_info) {
      this.order_info = order_info;
    }

    public String getReturn_url() {
      return return_url;
    }

    public void setReturn_url(String return_url) {
      this.return_url = return_url;
    }

    public String getBank_code() {
      return bank_code;
    }

    public void setBank_code(String bank_code) {
      this.bank_code = bank_code;
    }

    public String getWallet_code() {
      return wallet_code;
    }

    public void setWallet_code(String wallet_code) {
      this.wallet_code = wallet_code;
    }

    public String getPayment_type() {
      return payment_type;
    }

    public void setPayment_type(String payment_type) {
      this.payment_type = payment_type;
    }

    public String getLanguage() {
      return language;
    }

    public void setLanguage(String language) {
      this.language = language;
    }

    public String getSignature() {
      return signature;
    }

    public void setSignature(String signature) {
      this.signature = signature;
    }

  }
  
  class PaymentResponse {

    private String response_code;

    private String response_message;

    private PaymentBody data;

    public String getResponse_code() {
      return response_code;
    }

    public void setResponse_code(String response_code) {
      this.response_code = response_code;
    }

    public String getResponse_message() {
      return response_message;
    }

    public void setResponse_message(String response_message) {
      this.response_message = response_message;
    }

    public PaymentBody getData() {
      return data;
    }

    public void setData(PaymentBody data) {
      this.data = data;
    }
  }
  
  class PaymentBody {
    private String payment_url;

    private String trans_ref;

    public String getPayment_url() {
      return payment_url;
    }

    public void setPayment_url(String payment_url) {
      this.payment_url = payment_url;
    }

    public String getTrans_ref() {
      return trans_ref;
    }

    public void setTrans_ref(String trans_ref) {
      this.trans_ref = trans_ref;
    }

  }
  
  
}

functions.php
 
require_once "truemoney.constants.php";

function execPostRequest($url, $data)
{
    try {
        $ch = curl_init();
        curl_setopt($ch, CURLOPT_URL, $url);
        curl_setopt($ch, CURLOPT_POST, 1);
        curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
        curl_setopt($ch, CURLOPT_USERAGENT, $_SERVER['HTTP_USER_AGENT']);
        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
        curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 2);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
        curl_setopt($ch, CURLOPT_HTTPHEADER, array(
            'Content-Type: application/json'
        ));
        $result = curl_exec($ch);
        curl_close($ch);
        return $result;
    } catch (Exception $e) {
        return false;
    }
}

/**
 * Get Payment URL from order information.
 *
 * @param $access_key  The access_key provided by payment system.
 * @param $secret      The secret_key provided by payment system.
 * @param $order_info  The short description for order.
 * @param $amount      Total amount of order.
 * @param $order_id    The Id of order, you will check it on payment system.
 * @param $method      The method you registered on payment system. Current support bank/wallet/visa.
 * @param $bankcode    The code of bank provided in documents at developers.pay.truemoney.com.vn
 * 
 * @return null/object  Object response include error/url payment after request.
 * */
function getPayUrl($access_key, $secret, $order_info, $amount, $order_id, $method = '', $bankcode = '')
{
    $return_url = TRUEMONEY_RETURN_URL;
    $amount = intval(round($amount));
    $data = "access_key=".$access_key.
          "&amount=".$amount.
          "&order_id=".$order_id.
          "&order_info=".$order_info.
          "&return_url=".$return_url;
    $signature = hash_hmac("sha256", $data, $secret);
    $send_data = [
        "access_key"   => $access_key,
        "amount"       => $amount,
        "order_id"     => $order_id,
        "order_info"   => $order_info,
        "return_url"   => $return_url,
        "language"     => 'vi',
        "signature"    => $signature
    ];
    if ($method == 'atm') {
        $send_data['payment_type'] = 0;
        $send_data['bank_code'] = $bankcode;
    }
    else if ($method == 'visa') {
        $send_data['payment_type'] = 1;
    }
    else if ($method == 'wallet') {
        $send_data['payment_type'] = 2;
        $send_data['wallet_code'] = 'TRUEMONEY';
    } else {
        return null;
    }
    $data1 = json_encode($send_data);
    $json_bankCharging = execPostRequest(TRUEMONEY_DO_PAYMENT_URL, $data1);
    if ($json_bankCharging) {
        $decode_bankCharging = json_decode($json_bankCharging, true);  // decode json
        return $decode_bankCharging;
    }
    return null;
}

/**
 * Get Detail of transaction. Use for check information when order in processing.
 *
 * @param $access_key  The access_key provided by payment system.
 * @param $secret      The secret_key provided by payment system.
 * @param $trans_ref   The reference of Transaction after response of request of pay URL method.
 * 
 * @return null/object Object response include error/detail of transaction after request.
 * */
function getTransactionDetail($access_key, $secret, $trans_ref)
{
    $data = [
        'access_key' => $access_key,
        'trans_ref'  => $trans_ref
    ];
    $signature = hash_hmac("sha256", $data, $secret);
    $send_data = [
        "access_key"   => $access_key,
        "trans_ref"    => $trans_ref,
        "language"     => 'vi',
        "signature"    => $signature
    ];
    $data1 = json_encode($send_data);
    $json_trans_detail = execPostRequest(TRUEMONEY_TRANS_DETAIL_URL, $data1);
    if ($json_trans_detail) {
        $decode_trans_detail = json_decode($json_trans_detail, true);  // decode json
        return $decode_trans_detail;
    }
    return null;
}



hook.php
 
require_once "truemoney.constants.php";

$access_key     = $_POST['access_key'];
$order_id       = $_POST['order_id'];
$trans_ref      = $_POST['trans_ref'];
$payment_type   = $_POST['payment_type'];
$result_code    = $_POST['result_code'];
$description    = $_POST['description'];
$signature      = $_POST['signature'];
$secret_key     = TRUEMONEY_SECRET_KEY;

$data = "access_key=$access_key&order_id=$order_id&trans_ref=$trans_ref
&payment_type=$payment_type&result_code=$result_code";
$sign = hash_hmac("sha256", $data, $secret);
header('Content-Type: application/json');
if ($sign == $signature) {
    if ($result_code == "00") {
        echo "{response_code:00, response_message:\"Success\"}";
    }
} else {
    echo "{response_code: 99, response_message:\"Wrong Signature\"}";
}
exit();



result.php
 
require_once "truemoney.constants.php";

$access_key     = $_REQUEST['access_key'];
$order_id       = $_REQUEST['order_id'];
$trans_ref      = $_REQUEST['trans_ref'];
$payment_type   = $_REQUEST['payment_type'];
$result_code    = $_REQUEST['result_code'];
$description    = $_REQUEST['description'];
$signature      = $_REQUEST['signature'];
$secret_key     = TRUEMONEY_SECRET_KEY;

$data = "access_key=$access_key&order_id=$order_id&trans_ref=$trans_ref
&payment_type=$payment_type&result_code=$result_code";
$sign = hash_hmac("sha256", $data, $secret);

if ($sign == $signature) {
    if ($result_code == "00") {
        echo "$result_code-$order_id";
    }
} else {
    echo "Wrong Signature!";
}

  truemoney.constants.php
 
define('TRUEMONEY_ACCESS_KEY','');
define('TRUEMONEY_SECRET_KEY','');
define('TRUEMONEY_DO_PAYMENT_URL','https://partner.truemoney.com.vn/paymentgw/api/v1/do-payment');
define('TRUEMONEY_TRANS_DETAIL_URL', 'https://partner.truemoney.com.vn/paymentgw/api/v1/transaction-detail');
define('TRUEMONEY_RETURN_URL', 'https://google.com');



test.php
 
require "functions.php";
$order_id = $_REQUEST['order_id'];
$order_info = $_REQUEST['order_info'];
$order_amount = $_REQUEST['order_amount'];
$method     = $_REQUEST['method'];
$result = getPayUrl(TRUEMONEY_ACCESS_KEY, TRUEMONEY_SECRET_KEY, $order_info, $order_amount, $order_id, $method);
if ($result->response_code == '00') {
    return $result->data->payment_url;
}
return null;

6. Sandbox

API URL môi trường Sandbox:

POST https://payment.truemoney.com.vn/sandbox/api/v1/do-payment

Tra cứu giao dịch Test trên môi trường Sandbox:

POST https://payment.truemoney.com.vn/sandbox/ui/v1/transaction-log

Chọn ngân hàng thanh toán qua TPBank:

Tài khoản test case thành công, đủ số dư, đủ điều kiện thanh toán

  • Tài khoản: 9704 2300 0000 0001
  • Tên chủ thẻ: NGUYEN VAN A
  • Ngày phát hành: 09/2018
  • OTP: 123456

Tài khoản không đủ số dư

  • Tài khoản: 9704 2300 0000 0002
  • Tên chủ thẻ: NGUYEN VAN A
  • Ngày phát hành: 09/2018

Content