Question

I downloaded paypalplatform.php, can't remember where from, but it gives me a nice little function which allows me to check the state of a payment:

CallPaymentDetails( $payKey, $transactionId, $trackingId );

This returns lots of useful data like status and paymentInfoList.paymentInfo(0).transactionStatus and much more.

I know I can write lots of if statements to try to account for all the values returned to me when I call CallPaymentDetails, which I feel can be very error prone, or it could result in me over looking a scenario.

So my question is, does a sample template exist which takes all values into consideration, i.e. so I don't have to re-invent the wheel, which takes care of all scenarios?

For example, at the moment I have:

$resArray = CallPaymentDetails( $payKey, $transactionId, $trackingId );

if(strtoupper($resArray["responseEnvelope.ack"]) == "SUCCESS") {

    if(strtoupper($resArray["status"]) == "CREATED") {

        // do something

    } elseif(strtoupper($resArray["status"]) == "EXPIRED") {

        // do something

    } elseif(strtoupper($resArray["status"]) == "COMPLETED") {

        // do something

        if(strtoupper($resArray["paymentInfoList.paymentInfo(0).transactionStatus"]) == "PENDING") {

            // do something

        } elseif(strtoupper($resArray["paymentInfoList.paymentInfo(0).transactionStatus"]) == "FAILED") {

            // do something

        } else {

            // what other value could be returned

        }

    } else {

        // what other value could be returned

    }

} else {

    // what other value could be returned

}

If I try to do all the variables, I could be at this forever trying to capture all scenarios, which is why I was wondering if such a template already exists which caters for all scenarios via if statements?

Was it helpful?

Solution

First of all, you shouldn't grab a file on the internet like paypalplatform.php.

Paypal use Github a lot to share all APIs for all differents languages. I really recommend you to take a look at some repository such as :

The last two are interesting for you. They both provide useful code sample for your case.

codesamples-php

It presents a simple call to the function you mention and describes all return message that can be found using one variable $response->status. Comments reveal all case this variable can have:

if ($response->responseEnvelope->ack == "Success")
{

  // The status of the payment. Possible values are:
  //
  // * CREATED - The payment request was received; funds will be
  // transferred once the payment is approved
  // * COMPLETED - The payment was successful
  // * INCOMPLETE - Some transfers succeeded and some failed for a
  // parallel payment or, for a delayed chained payment, secondary
  // receivers have not been paid
  // * ERROR - The payment failed and all attempted transfers failed
  // or all completed transfers were successfully reversed
  // * REVERSALERROR - One or more transfers failed when attempting
  // to reverse a payment
  // * PROCESSING - The payment is in progress
  // * PENDING - The payment is awaiting processing
  $logger->log("Payment Status : ".$response->status);
}

adaptivepayments-sdk-php

It presents a more detailed example on how to use your function. It takes values from an html form, easier to test it. Like the previous example, we can see same states returned by the API:

$ack = strtoupper($response->responseEnvelope->ack);
if($ack != "SUCCESS"){
  echo "<b>Error </b>";
  echo "<pre>";
  print_r($response);
  echo "</pre>";
} else {
/*
 *       The status of the payment. Possible values are:

       * CREATED - The payment request was received; funds will be
       transferred once the payment is approved
       * COMPLETED - The payment was successful
       * INCOMPLETE - Some transfers succeeded and some failed for a
       parallel payment or, for a delayed chained payment, secondary
       receivers have not been paid
       * ERROR - The payment failed and all attempted transfers failed
       or all completed transfers were successfully reversed
       * REVERSALERROR - One or more transfers failed when attempting
       to reverse a payment
       * PROCESSING - The payment is in progress
       * PENDING - The payment is awaiting processing
 */
  echo "<table>";
  echo "<tr><td>Ack :</td><td><div id='Ack'>$ack</div> </td></tr>";
  echo "<tr><td>PayKey :</td><td><div id='PayKey'>$response->payKey</div> </td></tr>";
  echo "<tr><td>Status :</td><td><div id='Status'>$response->status</div> </td></tr>";
  echo "</table>";
  echo "<pre>";
  print_r($response);
  echo "</pre>";
}

In both case, you only need a simple switch/case to handle response state. Something like that:

switch ($status)
{
  case 'CREATED':
    // handle CREATED state
    break;
  case 'COMPLETED':
    // handle COMPLETED state
    break;
  case 'INCOMPLETE':
    // handle INCOMPLETE state
    break;
  case 'ERROR':
    // handle ERROR state
    break;
  case 'REVERSALERROR':
    // handle REVERSALERROR state
    break;
  case 'PROCESSING':
    // handle PROCESSING state
    break;
  case 'PENDING':
    // handle PENDING state
    break;

  default:
    throw new Exception(sprintf("State '%s' isn't handle.", $status));
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top