Press n or j to go to the next uncovered block, b, p or k for the previous block.
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 | 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 7x 7x 7x 7x 7x 7x 7x 7x 5x 7x 2x 2x 2x 2x 2x 2x 3x 3x 3x 3x 3x 3x 7x 2x 2x 2x 2x 2x 7x 1x 1x 1x 1x 13x 13x 13x 13x 13x 13x 13x 13x 13x 13x 2x 2x 13x 13x 13x 13x 2x 2x 13x 13x 13x 4x 4x 13x 13x 13x 13x 13x 1x 1x 1x 1x 13x 13x 13x 13x 13x 13x 208x 208x 208x 104x 104x 9x 9x 104x 208x 208x 208x 208x 13x 13x 13x 1x 1x 1x 1x 5x 5x 5x 1x 1x 1x 1x 4x 4x 4x 4x | // Payment utilities and helpers
export interface PaymentData {
orderId: number;
amount: number;
paymentMethod: "credit_card" | "paypal" | "apple_pay";
cardToken?: string;
}
export interface PaymentResponse {
success: boolean;
message: string;
order?: {
id: number;
status: string;
};
error?: string;
}
/**
* Process payment for an order
*/
export async function processPayment(
paymentData: PaymentData
): Promise<PaymentResponse> {
try {
const response = await fetch("/api/payments", {
method: "POST",
headers: {
"Content-Type": "application/json"},
body: JSON.stringify(paymentData)});
if (!response.ok) {
const errorData = await response.json();
return {
success: false,
message: errorData.error || "Payment failed",
error: errorData.error};
}
const result = await response.json();
return {
success: result.success,
message: result.message,
order: result.order};
} catch (error) {
return {
success: false,
message: "Payment processing error",
error: error instanceof Error ? error.message : "Unknown error"};
}
}
/**
* Validate card details (basic validation)
*/
export function validateCardDetails(card: {
number: string;
exp_month: string;
exp_year: string;
cvc: string;
}): { valid: boolean; errors: string[] } {
const errors: string[] = [];
// Card number validation (Luhn algorithm)
if (!validateCardNumber(card.number)) {
errors.push("Invalid card number");
}
// Expiration validation
const expDate = new Date(parseInt(card.exp_year), parseInt(card.exp_month));
if (expDate < new Date()) {
errors.push("Card has expired");
}
// CVC validation
if (!/^\d{3,4}$/.test(card.cvc)) {
errors.push("Invalid CVC");
}
return {
valid: errors.length === 0,
errors};
}
/**
* Luhn algorithm for card number validation
*/
function validateCardNumber(num: string): boolean {
const digits = num.replace(/\D/g, "");
let sum = 0;
let isEven = false;
for (let i = digits.length - 1; i >= 0; i--) {
let digit = parseInt(digits[i], 10);
if (isEven) {
digit *= 2;
if (digit > 9) {
digit -= 9;
}
}
sum += digit;
isEven = !isEven;
}
return sum % 10 === 0;
}
/**
* Format card number for display
*/
export function formatCardNumber(number: string): string {
return number.replace(/\s/g, "").replace(/(\d{4})/g, "$1 ").trim();
}
/**
* Mask card number for display
*/
export function maskCardNumber(number: string): string {
const clean = number.replace(/\D/g, "");
return "**** **** **** " + clean.slice(-4);
}
|