Skip to content

Commit e0cd430

Browse files
committed
+ Receiver API object, fix leaks due exceptions
1 parent 7919ade commit e0cd430

File tree

2 files changed

+255
-21
lines changed

2 files changed

+255
-21
lines changed

tests/test_yookassa_api.pas

Lines changed: 134 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,8 @@ interface
1414

1515
TTestableCreatePayment = class(TYookassaCreatePaymentRequest)
1616
public
17-
function BuildRequestJSONTest: String;
17+
function BuildRequestJSON: String; override;
18+
procedure TestToJSON;
1819
end;
1920

2021
{ TTestableCreateReceiptRequest }
@@ -44,6 +45,7 @@ TTestYooKassa = class(TTestCase)
4445
FReceiptRequest: TTestableCreateReceiptRequest;
4546
FTestItem: TYookassaReceiptItem;
4647
procedure CallToJSON;
48+
procedure FillPaymentData(aPayment: TYookassaCreatePaymentRequest);
4749
procedure LogHandler(aEvent: TEventType; const aMsg: string);
4850
procedure CreatePaymentStaticHandler;
4951
class procedure AddProductToReceipt(aReceipt: TYookassaReceipt; const aProductDescription: String; aVatCode: Integer=1;
@@ -68,6 +70,10 @@ TTestYooKassa = class(TTestCase)
6870
procedure TestReceiptRequestBuildRefundJSON;
6971
procedure TestReceiptRequestCreate;
7072
procedure TestReceiptResponseParsing;
73+
procedure TestReceiver_BankAccount_ToJSON;
74+
procedure TestReceiver_MobileBalance_ToJSON;
75+
procedure TestReceiver_DigitalWallet_ToJSON;
76+
procedure TestReceiver_Validation_MissingFields;
7177
procedure TestSettlements_ToJSON;
7278
procedure TestSupplier_InReceiptItemToJSON;
7379
procedure TestLogging;
@@ -82,9 +88,14 @@ implementation
8288

8389
{ TTestableCreatePayment }
8490

85-
function TTestableCreatePayment.BuildRequestJSONTest: String;
91+
function TTestableCreatePayment.BuildRequestJSON: String;
8692
begin
87-
Result:=BuildRequestJSON;
93+
Result:=inherited BuildRequestJSON;
94+
end;
95+
96+
procedure TTestableCreatePayment.TestToJSON;
97+
begin
98+
BuildRequestJSON;
8899
end;
89100

90101
{ TTestableCreateReceiptRequest }
@@ -123,6 +134,14 @@ procedure TTestYooKassa.CallToJSON;
123134
aJSON.Free;
124135
end;
125136

137+
procedure TTestYooKassa.FillPaymentData(aPayment: TYookassaCreatePaymentRequest);
138+
begin
139+
aPayment.Amount := 123.45;
140+
aPayment.Currency := 'RUB';
141+
aPayment.Description := 'Test payment';
142+
aPayment.ReturnUrl:='https://sample.com/';
143+
end;
144+
126145
procedure TTestYooKassa.LogHandler(aEvent: TEventType; const aMsg: string);
127146
begin
128147
AssertEquals('Test message', aMsg);
@@ -169,10 +188,7 @@ procedure TTestYooKassa.TestBuildRequestData;
169188
var
170189
aJSON: String;
171190
begin
172-
FPaymentRequest.Amount := 123.45;
173-
FPaymentRequest.Currency := 'RUB';
174-
FPaymentRequest.Description := 'Test payment';
175-
FPaymentRequest.ReturnUrl:='https://sample.com/';
191+
FillPaymentData(FPaymentRequest);
176192
aJSON := FPaymentRequest.BuildRequestJSON;
177193
AssertTrue(Pos('"amount"', aJSON) > 0);
178194
AssertTrue(Pos('"value" : "123.45"', aJSON) > 0);
@@ -499,6 +515,117 @@ procedure TTestYooKassa.TestReceiptResponseParsing;
499515
end;
500516
end;
501517

518+
procedure TTestYooKassa.TestReceiver_BankAccount_ToJSON;
519+
var
520+
aPayment: TTestableCreatePayment;
521+
aJSON, aReceiver: TJSONObject;
522+
begin
523+
aPayment := TTestableCreatePayment.Create;
524+
try
525+
FillPaymentData(aPayment);
526+
527+
aPayment.Receiver.ReceiverType := rtBankAccount;
528+
aPayment.Receiver.AccountNumber := '12345678901234567890';
529+
aPayment.Receiver.Bic := '044525225';
530+
531+
aJSON := TJSONObject(GetJSON(aPayment.BuildRequestJSON));
532+
try
533+
AssertTrue('receiver must be in JSON', Assigned(aJSON.Find('receiver')));
534+
aReceiver := aJSON.Objects['receiver'];
535+
AssertEquals('bank_account', aReceiver.Get('type', ''));
536+
AssertEquals('12345678901234567890', aReceiver.Get('account_number', ''));
537+
AssertEquals('044525225', aReceiver.Get('bic', ''));
538+
finally
539+
aJSON.Free;
540+
end;
541+
finally
542+
aPayment.Free;
543+
end;
544+
end;
545+
546+
procedure TTestYooKassa.TestReceiver_MobileBalance_ToJSON;
547+
var
548+
aPayment: TTestableCreatePayment;
549+
aJSON, aReceiver: TJSONObject;
550+
begin
551+
aPayment := TTestableCreatePayment.Create;
552+
try
553+
FillPaymentData(aPayment);
554+
aPayment.Receiver.ReceiverType := rtMobileBalance;
555+
aPayment.Receiver.Phone := '79001234567';
556+
557+
aJSON := TJSONObject(GetJSON(aPayment.BuildRequestJSON));
558+
try
559+
AssertTrue('receiver must be in JSON', Assigned(aJSON.Find('receiver')));
560+
aReceiver := aJSON.Objects['receiver'];
561+
AssertEquals('mobile_balance', aReceiver.Get('type', ''));
562+
AssertEquals('79001234567', aReceiver.Get('phone', ''));
563+
AssertFalse('account_number must not be present', Assigned(aReceiver.Find('account_number')));
564+
AssertFalse('bic must not be present', Assigned(aReceiver.Find('bic')));
565+
finally
566+
aJSON.Free;
567+
end;
568+
finally
569+
aPayment.Free;
570+
end;
571+
end;
572+
573+
procedure TTestYooKassa.TestReceiver_DigitalWallet_ToJSON;
574+
var
575+
aPayment: TTestableCreatePayment;
576+
aJSON, aReceiver: TJSONObject;
577+
begin
578+
aPayment := TTestableCreatePayment.Create;
579+
try
580+
FillPaymentData(aPayment);
581+
aPayment.Receiver.ReceiverType := rtDigitalWallet;
582+
aPayment.Receiver.AccountNumber := 'W1234567890';
583+
584+
aJSON := TJSONObject(GetJSON(aPayment.BuildRequestJSON));
585+
try
586+
AssertTrue('receiver must be in JSON', Assigned(aJSON.Find('receiver')));
587+
aReceiver := aJSON.Objects['receiver'];
588+
AssertEquals('digital_wallet', aReceiver.Get('type', ''));
589+
AssertEquals('W1234567890', aReceiver.Get('account_number', ''));
590+
AssertFalse('bic must not be present', Assigned(aReceiver.Find('bic')));
591+
AssertFalse('phone must not be present', Assigned(aReceiver.Find('phone')));
592+
finally
593+
aJSON.Free;
594+
end;
595+
finally
596+
aPayment.Free;
597+
end;
598+
end;
599+
600+
procedure TTestYooKassa.TestReceiver_Validation_MissingFields;
601+
var
602+
aPayment: TTestableCreatePayment;
603+
begin
604+
aPayment := TTestableCreatePayment.Create;
605+
try
606+
FillPaymentData(aPayment);
607+
// An attempt to serialize without required fields
608+
aPayment.Receiver.ReceiverType := rtBankAccount;
609+
aPayment.Receiver.AccountNumber := '';
610+
aPayment.Receiver.Bic := '044525225';
611+
AssertException(EYooKassaValidationError, @aPayment.TestToJSON);
612+
613+
aPayment.Receiver.AccountNumber := '12345678901234567890';
614+
aPayment.Receiver.Bic := '';
615+
AssertException(EYooKassaValidationError, @aPayment.TestToJSON);
616+
617+
aPayment.Receiver.ReceiverType := rtMobileBalance;
618+
aPayment.Receiver.Phone := '';
619+
AssertException(EYooKassaValidationError, @aPayment.TestToJSON);
620+
621+
aPayment.Receiver.ReceiverType := rtDigitalWallet;
622+
aPayment.Receiver.AccountNumber := '';
623+
AssertException(EYooKassaValidationError, @aPayment.TestToJSON);
624+
finally
625+
aPayment.Free;
626+
end;
627+
end;
628+
502629
procedure TTestYooKassa.TestSettlements_ToJSON;
503630
var
504631
aReceiptReq: TTestableCreateReceiptRequest;

0 commit comments

Comments
 (0)