You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何利用PayPal IPN检测付款是否处于暂扣状态?

How to Detect Successful PayPal Payments via IPN to Avoid Key Loss & Chargeback Headaches

Hey there, I totally get why this is driving you crazy—losing your paid product keys and your money to chargebacks, plus PayPal fees? That’s a double whammy. Let’s break down exactly which IPN variables you need to watch, and how to structure your logic to stop this from happening.

Core IPN Variables to Track Payment Status

These are the non-negotiable variables you’ll check to decide whether to release your product key:

  • payment_status: This is the main indicator of where the payment stands. The only status you should trust to release a key is Completed. Any other status (like Pending, Refunded, Reversed, or Chargeback) means you hold off on sending the key.
  • pending_reason: If payment_status is Pending, this variable tells you why—common reasons include echeck (electronic check hasn’t cleared), review (PayPal is auditing the payment), or unilateral (payment from an unverified account). None of these mean you can safely release the key; wait for a follow-up IPN when the status updates to Completed.
  • txn_type: Make sure this is set to web_accept (standard checkout payment) to avoid processing other transaction types (like subscriptions or donations) by mistake.
  • mc_gross: Cross-check this value against your product’s actual price to ensure the user paid the full amount—no partial payments slipping through.
  • reason_code: If the payment later gets refunded or charged back, this variable will give you details (e.g., chargeback, refund) to trigger your key-revocation logic.

Step-by-Step Processing Logic

  1. First, Verify the IPN is Legitimate
    Never skip this! PayPal requires you to send the entire IPN message back to their verification endpoint to confirm it’s not a fake. Only process the message if you get a VERIFIED response.

  2. Branch Your Logic Based on payment_status

    • When payment_status is Completed:
      Double-check txn_type is web_accept and mc_gross matches your product price. If all checks pass, generate and send the product key to the user, and log the transaction as successful.
    • When payment_status is Pending:
      Log the transaction with the pending_reason and do not send the key. PayPal will automatically send a new IPN when the status changes (either to Completed or Failed).
    • When payment_status is Refunded, Reversed, or Chargeback:
      Trigger your key-invalidation process—mark the associated product key as unusable in your system, and if possible, notify the user (though chargeback users often don’t respond). Log this status change for your records.

Quick Code Snippet Example (PHP)

Here’s a simplified version of how you might implement this in your IPN handler:

// Assume $ipn_data contains the verified IPN response from PayPal
$payment_status = $ipn_data['payment_status'];
$txn_id = $ipn_data['txn_id'];
$product_price = 29.99; // Replace with your actual product price

switch ($payment_status) {
    case 'Completed':
        if ($ipn_data['txn_type'] === 'web_accept' && $ipn_data['mc_gross'] == $product_price) {
            // Generate and send product key
            $product_key = generate_unique_product_key();
            send_key_email($ipn_data['payer_email'], $product_key);
            // Save key and transaction to your database
            save_transaction($txn_id, $product_key, 'Completed');
        }
        break;
    case 'Pending':
        $reason = $ipn_data['pending_reason'] ?? 'Unknown';
        save_transaction($txn_id, null, "Pending: $reason");
        break;
    case 'Refunded':
    case 'Reversed':
    case 'Chargeback':
        // Invalidate the key linked to this transaction
        invalidate_product_key($txn_id);
        save_transaction($txn_id, null, $payment_status);
        break;
    default:
        save_transaction($txn_id, null, "Unprocessed status: $payment_status");
        break;
}

Critical Notes to Avoid Future Issues

  • Listen for all IPN updates: A transaction’s status can change after your first IPN (e.g., a pending payment clears, or a completed payment gets charged back). Make sure your handler is set up to process these follow-up messages.
  • Log everything: Keep detailed logs of every IPN message, status change, and key sent. This will be a lifesaver if you need to dispute a chargeback or resolve user issues.
  • Don’t rush to send keys: Even if a user claims they didn’t get the key, don’t resend it until you’ve verified the payment_status is still Completed and no refund has been initiated.

内容的提问来源于stack exchange,提问作者xored

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.20 09:20:42