Files
iyzico/tests/fixtures/payment.ts
Duhan BALCI c65195d26d init
2026-01-01 18:30:21 +03:00

207 lines
6.0 KiB
TypeScript

/**
* Payment test fixtures with randomized personal data
*/
import { faker } from '@faker-js/faker';
import type {
ThreeDSInitializeRequest,
Non3DPaymentRequest,
PaymentCard,
Buyer,
BillingAddress,
ShippingAddress,
BasketItem,
} from '../../src/types';
/**
* Generates a valid Turkish identity number (TC Kimlik No)
* Uses a simple algorithm that produces valid-looking numbers
*/
function generateTurkishIdentityNumber(): string {
const digits: number[] = [];
// First digit cannot be 0
digits.push(faker.number.int({ min: 1, max: 9 }));
// Generate next 8 random digits
for (let i = 1; i < 9; i++) {
digits.push(faker.number.int({ min: 0, max: 9 }));
}
// Calculate 10th digit
const oddSum = digits[0] + digits[2] + digits[4] + digits[6] + digits[8];
const evenSum = digits[1] + digits[3] + digits[5] + digits[7];
const digit10 = (oddSum * 7 - evenSum) % 10;
digits.push(digit10 < 0 ? digit10 + 10 : digit10);
// Calculate 11th digit
const allSum = digits.reduce((sum, d) => sum + d, 0);
digits.push(allSum % 10);
return digits.join('');
}
/**
* Generates a Turkish phone number in the format +905XXXXXXXXX
*/
function generateTurkishPhoneNumber(): string {
const areaCode = faker.helpers.arrayElement(['530', '531', '532', '533', '534', '535', '536', '537', '538', '539', '540', '541', '542', '543', '544', '545', '546', '547', '548', '549', '550', '551', '552', '553', '554', '555', '556', '557', '558', '559']);
const number = faker.string.numeric(7);
return `+90${areaCode}${number}`;
}
/**
* Generates a random IP address
*/
function generateIpAddress(): string {
return faker.internet.ipv4();
}
/**
* Generates a test payment card with randomized holder name
* Card number is fixed to iyzico sandbox test card
*/
export function createTestCard(): PaymentCard {
return {
cardHolderName: faker.person.fullName(),
cardNumber: '5528790000000008', // iyzico sandbox test card
expireMonth: '12',
expireYear: '2030',
cvc: '123',
};
}
/**
* Generates a test buyer with randomized personal data
*/
export function createTestBuyer(): Buyer {
const firstName = faker.person.firstName();
const lastName = faker.person.lastName();
return {
id: `BY${faker.string.alphanumeric(6).toUpperCase()}`,
name: firstName,
surname: lastName,
identityNumber: generateTurkishIdentityNumber(),
email: faker.internet.email({ firstName, lastName }).toLowerCase(),
gsmNumber: generateTurkishPhoneNumber(),
registrationAddress: `${faker.location.streetAddress()}, ${faker.location.city()}`,
city: faker.location.city(),
country: 'Turkey',
ip: generateIpAddress(),
zipCode: faker.location.zipCode('#####'),
};
}
/**
* Generates a test billing address with randomized data
*/
export function createTestBillingAddress(): BillingAddress {
return {
contactName: faker.person.fullName(),
city: faker.location.city(),
country: 'Turkey',
address: `${faker.location.streetAddress()}, ${faker.location.city()}`,
zipCode: faker.location.zipCode('#####'),
};
}
/**
* Generates a test shipping address with randomized data
*/
export function createTestShippingAddress(): ShippingAddress {
return {
contactName: faker.person.fullName(),
city: faker.location.city(),
country: 'Turkey',
address: `${faker.location.streetAddress()}, ${faker.location.city()}`,
zipCode: faker.location.zipCode('#####'),
};
}
/**
* Generates test basket items
*/
export function createTestBasketItems(): BasketItem[] {
return [
{
id: `BI${faker.string.alphanumeric(3).toUpperCase()}`,
name: faker.commerce.productName(),
category1: faker.commerce.department(),
category2: faker.commerce.productAdjective(),
itemType: 'PHYSICAL',
price: 0.3,
},
{
id: `BI${faker.string.alphanumeric(3).toUpperCase()}`,
name: faker.commerce.productName(),
category1: 'Game',
category2: 'Online Game Items',
itemType: 'VIRTUAL',
price: 0.5,
},
{
id: `BI${faker.string.alphanumeric(3).toUpperCase()}`,
name: faker.commerce.productName(),
category1: 'Electronics',
category2: faker.commerce.productAdjective(),
itemType: 'PHYSICAL',
price: 0.2,
},
];
}
/**
* Generates a test 3DS initialize request with randomized data
*/
export function createTest3DSInitializeRequest(): ThreeDSInitializeRequest {
return {
locale: 'tr',
conversationId: faker.string.uuid(),
price: 1.0,
paidPrice: 1.1,
currency: 'TRY',
installment: 1,
paymentChannel: 'WEB',
basketId: `B${faker.string.alphanumeric(5).toUpperCase()}`,
paymentGroup: 'PRODUCT',
callbackUrl: 'https://www.merchant.com/callback',
paymentCard: createTestCard(),
buyer: createTestBuyer(),
billingAddress: createTestBillingAddress(),
shippingAddress: createTestShippingAddress(),
basketItems: createTestBasketItems(),
};
}
/**
* Generates a test non-3DS payment request with randomized data
*/
export function createTestNon3DPaymentRequest(): Non3DPaymentRequest {
return {
locale: 'tr',
conversationId: faker.string.uuid(),
price: 1.0,
paidPrice: 1.1,
currency: 'TRY',
installment: 1,
paymentChannel: 'WEB',
basketId: `B${faker.string.alphanumeric(5).toUpperCase()}`,
paymentGroup: 'PRODUCT',
paymentCard: createTestCard(),
buyer: createTestBuyer(),
billingAddress: createTestBillingAddress(),
shippingAddress: createTestShippingAddress(),
basketItems: createTestBasketItems(),
};
}
// Legacy exports for backward compatibility (static versions that generate once)
export const testCard = createTestCard();
export const testBuyer = createTestBuyer();
export const testBillingAddress = createTestBillingAddress();
export const testShippingAddress = createTestShippingAddress();
export const testBasketItems = createTestBasketItems();
export const test3DSInitializeRequest = createTest3DSInitializeRequest();
export const testNon3DPaymentRequest = createTestNon3DPaymentRequest();