Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Paypal IPN Detect Refund, PHP

I'm doing some custom work on the iDev affiliate paypal IPN and I am trying to set up a catch for refunded items. I have my database work correct, but I can't seem to get the IF catch right.

Any advise on what I should change it to?

if($_REQUEST["payment_status"] == "refunded"||$testing==1) 
{
    $email = $_REQUEST["payer_email"];
    $sid = $_REQUEST["subscr_id"];
    $tid = $_REQUEST["txn_id"];

    if (!$tid)
    {
        $tid='xxx';
    }

    if ($testing==1)
    {
        echo  "testing on";
        $sid = "I-E5E34E0DTMUS"; 
    }

    $query = "SELECT * FROM idevaff_sales WHERE tid1='$tid'";
    $result = mysql_query($query);
    if (!$result)
    {
        //echo $query; exit;
        mail('***@gmail.com',"1",$query); 
    }

    $arr = mysql_fetch_array($result);
    $aid = $arr['id'];

    $query = "SELECT * FROM idevaff_affiliates WHERE tid1='$tid'";
    $result = mysql_query($query);
    if ($result)
    {
        //echo $query; 
        mail('***@gmail.com',"2","$query"); 
    }

    $arr = mysql_fetch_array($result);
    $email = $arr['email'];
    $f_name = $arr['f_name'];

    mail($email,"Affiliate Message - A refund has granted for recent affiliate commission.","Dear $f_name,  \n\n Message here about refund" );

    $query = "UPDATE idevaff_sales SET approved=3 WHERE tracking='$sid'";
    $result = mysql_query($query);
    if (!$result)
    {
        //echo $query; exit;
        mail('***@gmail.com',"3","$query"); 
    }
}

Hudson

like image 563
atwellpub Avatar asked Dec 10 '22 10:12

atwellpub


2 Answers

Here is what I ended up using successfully:

if($_REQUEST["payment_status"] == "Refunded" || $_REQUEST["payment_status"] == "Reversed"  || $testing==1)  
{
    /*do database work here*/
}
like image 119
atwellpub Avatar answered Dec 14 '22 23:12

atwellpub


I am currently negotiating the matter with PayPal. The thing is that sometimes you would get this sequence of messages:

  • Completed (sale)
  • Reversed (buyer complaint)

Sometimes you get this:

  • Completed (sale)
  • Refunded (buyer complained to you, and you refunded without "official" complaint)

Sometimes you get this:

  • Completed (sale)
  • Reversed (buyer complaint)
  • Refunded (you refunded after complaint)

So, unless PayPal agrees to implement my solution to always send Canceled_Reversal before Refunded in the latter example (which was the case a few years ago), or comes up with another solution that will provide some certainty on the matter, you need to use your own mechanisms not to double-count a refund.

If you store transactions in a DB, the following may be pretty solid, from my POV:

if (($stat == 'Refunded' || $stat == 'Reversed') && !empty($_POST['parent_txn_id'])) 
{
  $sum = $db->query('SELECT SUM(`mc_gross`) FROM `transactions` WHERE `parent_txn_id`= ?', $_POST['parent_txn_id'])->fetchColumn();
  if ($sum && $sum < 0) {
    // already had reversed/refunded call,
    // ignore this one
  }
}
like image 26
nssmart Avatar answered Dec 14 '22 22:12

nssmart