QR
theo số tiền mình mong muốn và được thanh toán qua MOMO
, một vị tiền điện tử ra đời cũng lâu và được nhiều người tin dùng.[C#] Chia sẻ source code tạo mã QR MOMO đa năng Winform.
Khi các bạn tạo tài khoản MOMO, MOMO liên kết tài khoản của bạn với Ngân Hàng Bản Việt.Nghĩa là bạn sẽ có 1 thông tin tài khoản ở ngân hàng Bản Việt.
Để biết được mã số thanh toán của mình ở Ngân Hàng Bản Việt, trên QR MOMO thanh toán cá nhân, các bạn Quét thông tin sẽ thấy.
VIDEO DEMO.
Chúng ta bắt đầu từng bước.
Bước 1. Tạo một class API Request
.
using System; using System.Collections.Generic; using System.Drawing; using System.Drawing.Imaging; using System.IO; using System.Linq; using System.Text; using System.Threading.Tasks; namespace MOMO_QR_DANANG { public class APIRequest { public string accountNo { get; set; } public string accountName { get; set; } public int acqId { get; set; } public int amount { get; set; } public string addInfo { get; set; } public string logo { get; set; } public string format { get; set; } public string template { get; set; } public string theme { get; set; } public static string ImageToBase64(Image image, ImageFormat format) { using (MemoryStream ms = new MemoryStream()) { // Convert Image to byte[] image.Save(ms, format); byte[] imageBytes = ms.ToArray(); // Convert byte[] to Base64 string string base64String = Convert.ToBase64String(imageBytes); // Get the appropriate MIME type string mimeType = GetMimeType(format); // Return the Base64 string with the MIME type prefix return $"data:{mimeType};base64,{base64String}"; } } private static string GetMimeType(ImageFormat format) { if (format.Equals(ImageFormat.Jpeg)) { return "image/jpeg"; } else if (format.Equals(ImageFormat.Png)) { return "image/png"; } else if (format.Equals(ImageFormat.Bmp)) { return "image/bmp"; } else if (format.Equals(ImageFormat.Gif)) { return "image/gif"; } else if (format.Equals(ImageFormat.Tiff)) { return "image/tiff"; } else { throw new ArgumentOutOfRangeException("Unknown image format"); } } public static Image Base64ToImage(string base64String) { byte[] imageBytes = Convert.FromBase64String(base64String); MemoryStream ms = new MemoryStream(imageBytes, 0, imageBytes.Length); ms.Write(imageBytes, 0, imageBytes.Length); System.Drawing.Image image = System.Drawing.Image.FromStream(ms, true); return image; } } public class Data { public int acpId { get; set; } public string accountName { get; set; } public string qrCode { get; set; } public string qrDataURL { get; set; } } public class ApiResponse { public string code { get; set; } public string desc { get; set; } public Data data { get; set; } } }
Bước 2. Code ở Form1.cs
.
using Newtonsoft.Json; using RestSharp; using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Deployment.Internal; using System.Drawing; using System.Drawing.Imaging; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Windows.Forms; namespace MOMO_QR_DANANG { public partial class Form1 : Form { public Form1() { InitializeComponent(); } private void Form1_Load(object sender, EventArgs e) { } private void button1_Click(object sender, EventArgs e) { OpenFileDialog openFileDialog = new OpenFileDialog { Filter = "Image files (*.png)|*.png;", Title = "Select an Image" }; if (openFileDialog.ShowDialog() == DialogResult.OK) { string selectedFileName = openFileDialog.FileName; picLogo.LoadAsync(selectedFileName); } } private void button2_Click(object sender, EventArgs e) { var stk = txtSoTaiKhoan.Text.Trim(); var sotien = Convert.ToInt32(txtSoTien.Text.Trim()); var noidung = txtNoiDung.Text.Trim(); var tentaikhoan = txtTenTaiKhoan.Text.Trim(); string logo = ""; if(picLogo.Image != null) { logo = APIRequest.ImageToBase64(picLogo.Image, ImageFormat.Png); } var apiRequest = new APIRequest(); apiRequest.acqId = 970454; // mã số ngân hàng bản việt apiRequest.accountNo = stk; apiRequest.accountName = tentaikhoan; apiRequest.amount = sotien; apiRequest.logo = logo; //apiRequest.format = "text"; apiRequest.addInfo = noidung; apiRequest.template = "compact2"; apiRequest.theme = "compact2"; var jsonRequest = JsonConvert.SerializeObject(apiRequest); // use restsharp for request api. var client = new RestClient("https://api.vietqr.io/v2/generate"); var request = new RestRequest(); request.Method = Method.Post; request.AddHeader("Accept", "application/json"); request.AddParameter("application/json", jsonRequest, ParameterType.RequestBody); var response = client.Execute(request, Method.Post); var content = response.Content; var dataResult = JsonConvert.DeserializeObject<ApiResponse>(content); var image = APIRequest. Base64ToImage(dataResult.data.qrDataURL.Replace("data:image/png;base64,", "")); picQRMOMO.Image = image; } } }
Như vậy đã hoàn thành, chúc mọi người thành công.
Hoặc có thể tải source code bên dưới để kiểm tra nhé.