我下载了paypalplatform.php,不记得从哪里来,但是它为我提供了一个不错的小功能,可让我检查付款状态:
CallPaymentDetails( $payKey, $transactionId, $trackingId );
这将返回大量的有用数据的喜欢status
和paymentInfoList.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语句满足所有场景的需求?
首先,您不应该像上那样在互联网上获取文件paypalplatform.php
。
Paypal大量使用Github来共享所有不同语言的所有API。我真的建议您看一下一些存储库,例如:
最后两个对您来说很有趣。它们都为您的案例提供了有用的代码示例。
codesamples-php
它提供了对您提到的函数的简单调用,并描述了可以使用一个变量找到的所有返回消息$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);
}
自适应付款-SDK-PHP
它提供了有关如何使用功能的更详细的示例。它从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));
}
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句