Skip to content

Commit

Permalink
Merge pull request #1 from hsndmr/add-retrieve-method-to-payment
Browse files Browse the repository at this point in the history
Add retrieve method to payment
  • Loading branch information
hsndmr committed Jan 4, 2024
2 parents b858983 + 94d0b5d commit 443093e
Show file tree
Hide file tree
Showing 7 changed files with 113 additions and 7 deletions.
5 changes: 3 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

24 changes: 21 additions & 3 deletions packages/iyzipay-node/src/lib/models/payment.spec.ts
Original file line number Diff line number Diff line change
@@ -1,16 +1,23 @@
import { Options } from '../options';
import { CreatePaymentRequest } from '../requests';
import { CreatePaymentRequest, RetrievePaymentRequest } from '../requests';
import {
IyzipayHttpClientMock,
assertResource,
} from '../iyzipay-http-client.mock';
import { Payment } from './payment';
import { IyzipayHttpClient } from '../iyzipay-http-client';

describe('Payment', () => {
let options: Options;
let client: IyzipayHttpClient;

beforeEach(() => {
options = new Options();
client = new IyzipayHttpClientMock(options);
});

it('should create payment', async () => {
// Arrange
const options = new Options();
const client = new IyzipayHttpClientMock(options);

const request = new CreatePaymentRequest();

Expand All @@ -20,4 +27,15 @@ describe('Payment', () => {
// Assert
assertResource(payment);
});

it('should retrieve payment', async () => {
// Arrange
const request = new RetrievePaymentRequest();

// Act
const payment = await Payment.retrieve(request, client);

// Assert
assertResource(payment);
});
});
17 changes: 16 additions & 1 deletion packages/iyzipay-node/src/lib/models/payment.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { IyzipayHttpClient } from '../iyzipay-http-client';
import { CreatePaymentRequest } from '../requests';
import { CreatePaymentRequest, RetrievePaymentRequest } from '../requests';
import { PaymentMapper } from './mappers/payment-mapper';
import { PaymentResource } from './resources/payment-resource';

Expand All @@ -18,4 +18,19 @@ export class Payment extends PaymentResource {

return new PaymentMapper(response.data).mapPayment(new Payment());
}

public static async retrieve(
request: RetrievePaymentRequest,
client: IyzipayHttpClient
): Promise<Payment> {
const response = await client.post(
'/payment/detail',
request.getRequestData(),
{
headers: client.getHttpHeaders(request),
}
);

return new PaymentMapper(response.data).mapPayment(new Payment());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -176,7 +176,7 @@ export class CreatePaymentRequest extends Request {
}

public getRequestData(): FlexibleData {
return RequestDataBuilder.create()
return RequestDataBuilder.fromObject(super.getRequestData())
.add('locale', this.getLocale())
.add('conversationId', this.getConversationId())
.addPrice('price', this.price)
Expand Down
1 change: 1 addition & 0 deletions packages/iyzipay-node/src/lib/requests/index.ts
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
export * from './create-payment-request';
export * from './retrieve-payment-request';
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
import { Locale } from '../models';
import { RetrievePaymentRequest } from './retrieve-payment-request';

describe('RetrievePaymentRequest', () => {
let request: RetrievePaymentRequest;

beforeEach(() => {
request = new RetrievePaymentRequest();
request.setLocale(Locale.TR);
request.setConversationId('123456789');
request.setPaymentId(1);
request.setPaymentConversationId('123456789');
});

it('should get request data', () => {
// Arrange & Act & Assert
const expectedJsonObject = {
locale: request.getLocale(),
conversationId: request.getConversationId(),
paymentId: request.getPaymentId(),
paymentConversationId: request.getPaymentConversationId(),
};

expect(request.getRequestData()).toEqual(expectedJsonObject);
});

it('should generate PKI request string', () => {
const str = `[locale=tr,conversationId=123456789,paymentId=1,paymentConversationId=123456789]`;

expect(request.toPKIRequestString()).toEqual(str);
});
});
39 changes: 39 additions & 0 deletions packages/iyzipay-node/src/lib/requests/retrieve-payment-request.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
import { Request } from '../request';
import { RequestDataBuilder } from '../request-data-builder';
import { RequestStringBuilder } from '../request-string-builder';

export class RetrievePaymentRequest extends Request {
private paymentId;
private paymentConversationId;

public getPaymentId() {
return this.paymentId;
}

public setPaymentId(paymentId) {
this.paymentId = paymentId;
}

public getPaymentConversationId() {
return this.paymentConversationId;
}

public setPaymentConversationId(paymentConversationId) {
this.paymentConversationId = paymentConversationId;
}

public getRequestData() {
return RequestDataBuilder.fromObject(super.getRequestData())
.add('paymentId', this.getPaymentId())
.add('paymentConversationId', this.getPaymentConversationId())
.get();
}

public toPKIRequestString() {
return RequestStringBuilder.create()
.appendSuper(super.toPKIRequestString())
.append('paymentId', this.getPaymentId())
.append('paymentConversationId', this.getPaymentConversationId())
.getRequestString();
}
}

0 comments on commit 443093e

Please sign in to comment.