PayPal Adaptive Payments ConvertCurrency API 的 PHP 示例

2021-12-29 00:00:00 php paypal paypal-adaptive-payments

我试图围绕新的 PayPal 自适应支付 API - 特别是 ConvertCurrency 功能.有没有人在 PHP 中遇到过这种情况?

文档:https://www.x.com/docs/DOC-1400

解决方案

转换为 diff 非常容易.货币使用 Paypal 的自适应支付 SDK.假设你有必要的信息.(API 用户名/密码等)向 API 发出请求,可以按如下方式进行:

detailLevel = 0;$requestEnvelope->errorLanguage = 'en_US';//我们的基础金额,也就是我们想要转换成的货币//其他货币类型.很简单,只要有一个公众号//道具.持有金额和当前代码.$baseAmountList = new CurrencyList();$baseAmountList->currency = array( 'amount' => 15, 'code' => 'USD' );//我们的目标货币类型.鉴于我来自墨西哥,我想//在墨西哥比索中看到它.同样,只需要提供代码//货币.在文档中,您可以访问完整的代码列表$convertToCurrencyList = new CurrencyCodeList();$convertToCurrencyList->currencyCode = 'MXN';//现在创建 ConvertCurrencyRequest 对象的一个​​实例,它是//处理这个请求所必需的.//这个对象将我们之前创建的对象作为参数//是我们的基础货币、我们的目标货币和请求.信封$ccReq = new ConvertCurrencyRequest();$ccReq-​​>baseAmountList = $baseAmountList;$ccReq-​​>convertToCurrencyList = $convertToCurrencyList;$ccReq-​​>requestEnvelope = $requestEnvelope;//最后,我们在 AdaptivePayment 对象上调用 ConvertCurrency 方法,//并将我们得到的任何结果分配给我们的变量$result = $ap->ConvertCurrency($ccReq);//鉴于我们的结果应该是一个 ConvertCurrencyResponse 对象,我们可以//查看其属性以进一步显示/处理目的$resultingCurrencyList = $result->estimatedAmountTable->currencyConversionList;$baseAmount = $resultingCurrencyList->baseAmount->amount;$baseAmountCode = $resultingCurrencyList->baseAmount->code;$convertedAmount = $resultingCurrencyList->currencyList->currency->amount;$convertedAmountCode = $resultingCurrencyList->currencyList->currency->code;回声'<br/>$' .$baseAmount .' ' .$baseAmountCode .' 是 $' .$convertedAmount .' ' .$convertedAmountCode;//这里只是为了了解我们如何从 Paypal 的 API 中获取结果echo '

';打印_r($result);echo '</pre>';

输出应该是这样的:

15 美元是 159.75 墨西哥比索ConvertCurrencyResponse 对象([响应信封] =>ResponseEnvelope 对象([时间戳] =>2010-04-20T13:27:40.278-07:00[确认] =>成功[correlationId] =>b940006680f6a[构建] =>1238639)[estimatedAmountTable] =>标准类对象([货币转换列表] =>货币转换列表对象([基本金额] =>标准类对象([代码] =>美元[金额] =>15)[货币列表] =>货币列表对象([货币] =>货币对象([代码] =>MXN[金额] =>159.75)))))

如您所见,使用 ConvertCurrency API 非常简单,只需下载 SDK 并开始使用它;)

Trying to wrap my head around the new PayPal Adaptive Payments API - specifically the ConvertCurrency functionality. Has anyone had any luck with this in PHP?

Documentation: https://www.x.com/docs/DOC-1400

解决方案

It's very easy to convert to dif. currencies using Paypal's Adaptive Payments SDK. Assuming you have the necessary info. (API username/passwd, etc) to make requests to the API, it can be done as follows:

<?php

//First we need to include the class file of the Adaptive Payments
include 'AdaptivePayments.php';

// Create an instance, you'll make all the necessary requests through this
// object, if you digged through the code, you'll notice an AdaptivePaymentsProxy class
// wich has in it all of the classes corresponding to every object mentioned on the
// documentation of the API
$ap = new AdaptivePayments();

// Our request envelope
$requestEnvelope = new RequestEnvelope();
$requestEnvelope->detailLevel = 0;
$requestEnvelope->errorLanguage = 'en_US';

// Our base amount, in other words the currency we want to convert to
// other currency type. It's very straighforward, just have a public
// prop. to hold de amount and the current code.
$baseAmountList = new CurrencyList();
$baseAmountList->currency = array( 'amount' => 15, 'code' => 'USD' );

// Our target currency type. Given that I'm from Mexico I would like to
// see it in mexican pesos. Again, just need to provide the code of the
// currency. On the docs you'll have access to the complete list of codes
$convertToCurrencyList = new CurrencyCodeList();
$convertToCurrencyList->currencyCode = 'MXN';

// Now create a instance of the ConvertCurrencyRequest object, which is
// the one necessary to handle this request.
// This object takes as parameters the ones we previously created, which
// are our base currency, our target currency, and the req. envelop
$ccReq = new ConvertCurrencyRequest();
$ccReq->baseAmountList = $baseAmountList;
$ccReq->convertToCurrencyList = $convertToCurrencyList;
$ccReq->requestEnvelope = $requestEnvelope;

// And finally we call the ConvertCurrency method on our AdaptivePayment object,
// and assign whatever result we get to our variable
$result = $ap->ConvertCurrency($ccReq);

// Given that our result should be a ConvertCurrencyResponse object, we can
// look into its properties for further display/processing purposes
$resultingCurrencyList = $result->estimatedAmountTable->currencyConversionList;
$baseAmount = $resultingCurrencyList->baseAmount->amount;
$baseAmountCode = $resultingCurrencyList->baseAmount->code;
$convertedAmount = $resultingCurrencyList->currencyList->currency->amount;
$convertedAmountCode = $resultingCurrencyList->currencyList->currency->code;

echo '<br /> $' . $baseAmount . ' ' . $baseAmountCode . ' is $' . $convertedAmount . ' ' . $convertedAmountCode;

// And here just for the sake of knowing how we get the result from Paypal's API
echo '<pre>';
print_r($result);
echo '</pre>';

The output should be something like:

$15 USD is $159.75 MXN

ConvertCurrencyResponse Object
(
    [responseEnvelope] => ResponseEnvelope Object
        (
            [timestamp] => 2010-04-20T13:27:40.278-07:00
            [ack] => Success
            [correlationId] => b940006680f6a
            [build] => 1238639
        )

    [estimatedAmountTable] => stdClass Object
        (
            [currencyConversionList] => CurrencyConversionList Object
                (
                    [baseAmount] => stdClass Object
                        (
                            [code] => USD
                            [amount] => 15
                        )

                    [currencyList] => CurrencyList Object
                        (
                            [currency] => currency Object
                                (
                                    [code] => MXN
                                    [amount] => 159.75
                                )

                        )

                )

        )

)

As you can see, it is very simple to use the ConvertCurrency API, just download the SDK and start playing with it ;)

相关文章