This seem to be a bug in the way Mage_Paypal handle invoice number.
If a paypal payment fail when a customer is trying to checkout and they try to checkout again whether it is a week or months later, once the quote session is still available magento will issue the same order increment id/invoice number to paypal
See /app/code/core/Mage/Paypal/Model/Express/Checkout.php
public function start($returnUrl, $cancelUrl, $button = null)
{
......
$this->_api->setAmount($this->_quote->getBaseGrandTotal())
->setCurrencyCode($this->_quote->getBaseCurrencyCode())
->setInvNum($this->_quote->getReservedOrderId())
Paypal invoice number is set using $this->_quote->getReservedOrderId()
See app/code/core/Mage/Sales/Model/Quote.php
public function reserveOrderId()
{
if (!$this->getReservedOrderId()) {
$this->setReservedOrderId($this->_getResource()->getReservedOrderId($this));
} else {
//checking if reserved order id was already used for some order
//if yes reserving new one if not using old one
if ($this->_getResource()->isOrderIncrementIdUsed($this->getReservedOrderId())) {
$this->setReservedOrderId($this->_getResource()->getReservedOrderId($this));
}
}
return $this;
}
Since the order increment id/was never converted to an order $this->_getResource()->isOrderIncrementIdUsed($this->getReservedOrderId()
will return false and the order increment number/invoice will stay unchanged.
Also take a look at Magento PayPal Duplicate Invoice Error