async function cancelPixCharge(chargeId) { // First, check if the charge is eligible for cancellation const charge = await getChargeById(chargeId); if (charge.payment_method !== 'pix') { throw new Error('Not a Pix charge'); } if (charge.status.id !== 1) { throw new Error('Charge status must be "created" to cancel'); } // Check if 5 minutes have passed since creation const createdAt = new Date(charge.created_at); const now = new Date(); const minutesPassed = (now - createdAt) / 1000 / 60; if (minutesPassed < 5) { throw new Error('Must wait at least 5 minutes before canceling Pix'); } // Cancel the charge const result = await cancelCharge(chargeId); return result;}
Cancel Boleto Charge
Cancel a Boleto charge that hasn’t been paid:
Copy
async function cancelBoletoCharge(chargeId) { const charge = await getChargeById(chargeId); if (charge.payment_method !== 'boleto') { throw new Error('Not a Boleto charge'); } if (charge.status.id !== 1) { throw new Error('Charge status must be "created" to cancel'); } // Check if 30 minutes have passed since creation const createdAt = new Date(charge.created_at); const now = new Date(); const minutesPassed = (now - createdAt) / 1000 / 60; if (minutesPassed < 30) { throw new Error('Must wait at least 30 minutes before canceling Boleto'); } // Cancel the charge const result = await cancelCharge(chargeId); console.log('Boleto cancellation requested.'); console.log('Note: It may take at least 1 day for the status to change to "canceled"'); return result;}
Batch Cancel Charges
Cancel multiple charges at once:
Copy
async function batchCancelCharges(chargeIds) { const results = []; for (const chargeId of chargeIds) { try { const result = await cancelCharge(chargeId); results.push({ chargeId, success: true, result }); } catch (error) { results.push({ chargeId, success: false, error: error.message }); } // Add delay to avoid rate limiting await new Promise(resolve => setTimeout(resolve, 100)); } return results;}// Usageconst chargesToCancel = [32457, 32458, 32459];const results = await batchCancelCharges(chargesToCancel);console.log('Cancellation results:', results);
Auto-Cancel Expired Charges
Automatically cancel charges that have expired:
Copy
async function autoCancelExpiredCharges() { // Get all charges with status "created" const charges = await fetch( 'https://api.sandbox.wepayout.com.br/v2/payin/payments?status_id=1', { headers: { 'Authorization': 'Bearer 123', 'Accept': 'application/json' } } ).then(r => r.json()); const now = new Date(); const canceledCharges = []; for (const charge of charges.data) { const createdAt = new Date(charge.created_at); const minutesPassed = (now - createdAt) / 1000 / 60; // Check if charge is eligible for cancellation const minMinutes = charge.payment_method === 'pix' ? 5 : 30; if (minutesPassed >= minMinutes) { try { await cancelCharge(charge.id); canceledCharges.push(charge.id); } catch (error) { console.error(`Failed to cancel charge ${charge.id}:`, error); } } } return canceledCharges;}
Cancel with Validation
Cancel a charge with full validation:
Copy
async function cancelChargeWithValidation(chargeId) { // Get charge details const charge = await getChargeById(chargeId); // Validation checks const validations = { statusCheck: charge.status.id === 1, paymentMethodCheck: ['pix', 'boleto'].includes(charge.payment_method), timeCheck: false }; // Check minimum time const createdAt = new Date(charge.created_at); const now = new Date(); const minutesPassed = (now - createdAt) / 1000 / 60; if (charge.payment_method === 'pix') { validations.timeCheck = minutesPassed >= 5; } else if (charge.payment_method === 'boleto') { validations.timeCheck = minutesPassed >= 30; } // Check all validations if (!validations.statusCheck) { throw new Error('Charge status must be "created"'); } if (!validations.paymentMethodCheck) { throw new Error('Only pix and boleto charges can be canceled'); } if (!validations.timeCheck) { const minTime = charge.payment_method === 'pix' ? 5 : 30; throw new Error(`Must wait at least ${minTime} minutes before canceling`); } // All validations passed, cancel the charge const result = await cancelCharge(chargeId); return { success: true, chargeId, paymentMethod: charge.payment_method, result };}
Pix Cancellation: Pix charges are canceled instantly once the request is made.
Boleto Cancellation: Boleto charges require at least 1 day to move from drop_requested to canceled status, as the process depends on confirmation from the payment processor.
Time Requirements: Always check that the minimum time has passed before attempting to cancel: