เช็คเลขที่ผู้เสียภาษีจาก TIN Service ของ กรมสรรพากร ด้วย REST Client ของ Delphi

Piyanat Nimkhuntod
3 min readNov 8, 2022

--

สรรพากรมีบริการ SOAP API ให้เช็คความถูกต้องของผู้เสียภาษีโดยมี Document เขียนไว้ที่ https://www.rd.go.th/42533.html

โดย API ตัวแรก https://rdws.rd.go.th/serviceRD3/checktinpinservice.asmx?wsdl จะส่งค่ากลับมาเป็นเป็น XML

ส่วน https://rdws.rd.go.th/jsonRD/checktinpinservice.asmx ต้องเติม ?wsdl เพิ่มเข้าไปไม่งั้นจะไม่สามารถทำงานได้ครับ (ความเห็นส่วนตัวในตอนทำ Project นี้กับ Delphi) ก็จะกลายเป็น

https://rdws.rd.go.th/jsonRD/checktinpinservice.asmx?wsdl ซึ่งจะส่งค่ากลับมาเป็น JSON ครับ

ตอนนี้เรามาเตรียมส่วนผสมกันก่อน

ก่อนอื่นเลยก็ออกแบบหน้าจอโปรแกรมกันก่อนนะครับ ก็มี Label Textbox , button , Memo และ HTTPRIO ครับ

ทีนี้เรามาตั้งค่า HTTPIO1 กันครับ มาดูที่ Properties

**** โปรเจคบน Delphi นี้จะสำเร็จไปไม่ได้เลยถ้าไม่ Import WSDL มาใช้ ****

ขั้นตอนการนำเข้าก็มีดังนี้ครับ

  1. Component → Import WSDL

2. ใส่ URL : https://rdws.rd.go.th/jsonRD/checktinpinservice.asmx?WSDL

3. เลือก Automatic SOAP versioning. (Recommended) กด Next

4. ตรงนี้ก็กด Finish เลยครับ

5. เราจะได้ File นามสกุล .pas มา ซึ่งจะใช้เป็น Library ในการเชื่อมต่อกับ WSDL ให้ง่ายขึ้น

เท่านี้เราก็เตรียมส่วนผสมเรียบร้อยแล้วครับ ขั้นตอนต่อไปจะเป็นการลงมือ code แล้วครับ

สำหรับการเขียน Code ของ Delphi เมื่อเรามี Library แล้วเราก็ทำการ use ก่อนครับ โดยเราจะ Use library ที่เรานำเข้า wsdl เข้ามาครับ แล้วก็ System.Json

uses  checktinpinservice,System.JSON;

คราวนี้เราก็มาถึง event ปุ่มกดของเราแล้วครับ

procedure TForm7.BtnCheckClick(Sender: TObject);
var
RDNID : checktinpinserviceSoap;
JsonValue: TJSonValue ;
JSONString : String;
myarr: TJSONArray;
IsExist: string;
ErrMessage : String;
begin
try
JSONString := (HTTPRIO1 as checktinpinserviceSoap).ServiceTIN('anonymous','anonymous',NID.Text);
Memo1.Clear;
Memo1.Lines.Add(JSONString);
JsonValue := TJSonObject.ParseJSONValue(JSONString);
if JsonValue.TryGetValue('IsExist', myarr) and (myarr.Count > 0) then
begin
IsExist := myarr.Items[0].ToString;
if IsExist = 'Yes' then
showmessage('พบบัตรประชาชน');
end;
if JsonValue.TryGetValue('MessageErr', myarr) and (myarr.Count > 0) then
begin
ErrMessage := myarr.Items[0].ToString;
ErrMessage := StringReplace(ErrMessage,'<br> Data not found <br> => \"','',[rfReplaceAll, rfIgnoreCase]);
ErrMessage := StringReplace(ErrMessage,'\"','',[rfReplaceAll, rfIgnoreCase]);
showmessage(ErrMessage);
end;
Except
on E : Exception do
begin
MessageDlg(E.Message,mtError,[MbOK],0);
end; end;
end;

มาดูคำอธิบายกันครับ

JSONString := (HTTPRIO1 as checktinpinserviceSoap).ServiceTIN('anonymous','anonymous',NID.Text);

คำสั่งนี้จะเป็นการไปเรียก WSDL โดยส่ง User และ Password เป็น anonymous และในส่วน NID.Text คือ เลขที่ผู้เสียภาษี ตามเงื่อนไขที่เขาให้มาครับ

ต่อไปคือการถอด JSON มาแสดงผลด้วยคำสั่ง

JsonValue := TJSonObject.ParseJSONValue(JSONString);

ที่นี้เราก็เรียกดูค่า Value ที่ JSON Return มา ดังตัวอย่างจะเป็นการเช็ค Node ที่ชื่อว่า IsExist

if JsonValue.TryGetValue('IsExist', myarr) and (myarr.Count > 0) then
begin
IsExist := myarr.Items[0].ToString;
if IsExist = 'Yes' then
showmessage('พบบัตรเลขที่ผู้เสียภาษี');
end;

เมื่อกดเช็คข้อมูลโปรแกรมจะ Return JSON ออกมาใน Memo ก็จะได้

*** 0000000000000 คือเลขที่ที่อยู่ใน NID.Text แต่ขออนุญาติไม่เอาของจริงมาแสดงครับ

{
"MessageErr": [],
"ID": [
"0000000000000"
],
"DigitOk": [
"True"
],
"IsExist": [
"Yes"
],
"vMessageErr": [],
"vID": [
"3670800140248"
],
"vDigitOk": [
"True"
],
"vIsExist": [
"Yes"
]
}

เป็นไงบ้างครับ ลองทำตามดูได้ไหม ถ้าได้ ลุยต่อเลยครับ ถ้าไม่ได้

Github https://github.com/piyanatn/DelphiTINService

--

--