2013-09-25 25 views
4

我下载paypalplatform.php,不记得从那里,但它给了我一个很好的小功能,让我检查付款的状态:PayPal的CallPaymentDetails功能

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

回答

3

首先,你不应该在互联网上抓取一个文件,如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)); 
}