【问题标题】:PayPal's CallPaymentDetails functionPayPal Call Payment Details 功能
【发布时间】:2013-09-30 17:59:39
【问题描述】:

我下载了 paypalplatform.php,不记得从哪里下载的,但它给了我一个不错的小功能,可以让我检查付款状态:

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

这会返回大量有用的数据,例如 statuspaymentInfoList.paymentInfo(0).transactionStatus 等等。

我知道我可以写很多 if 语句来尝试解释当我调用 CallPaymentDetails 时返回给我的所有值,我觉得这很容易出错,或者可能导致我忽略场景。

所以我的问题是,是否存在一个将所有值都考虑在内的示例模板,也就是说,我不必重新发明轮子,它可以处理所有场景?

例如,目前我有:

$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 语句满足所有场景?

【问题讨论】:

    标签: php paypal paypal-adaptive-payments paypal-nvp


    【解决方案1】:

    首先,你不应该在互联网上抓取像paypalplatform.php 这样的文件。

    Paypal use Github 非常多地分享所有不同语言的所有 API。我真的建议您查看一些存储库,例如:

    最后两个对你来说很有趣。它们都为您的案例提供了有用的代码示例。

    codesamples-php

    它为您提到的函数提供了一个simple call,并描述了可以使用一个变量$response->status 找到的所有返回消息。评论揭示了这个变量可以有的所有情况:

    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

    它向a more detailed example 介绍如何使用您的功能。它从 html 表单中获取值,更容易对其进行测试。与前面的示例一样,我们可以看到 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>";
    }
    

    在这两种情况下,您只需要一个简单的开关/案例来处理响应状态。类似的东西:

    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));
    }
    

    【讨论】:

      猜你喜欢
      • 2013-06-12
      • 2021-07-18
      • 2022-08-05
      • 2019-01-15
      • 2014-11-11
      • 2013-07-17
      • 2011-06-27
      • 2016-10-08
      • 2013-06-16
      相关资源
      最近更新 更多