草庐IT

PayPal Adaptive Payments ConvertCurrency API 的 PHP 示例

coder 2023-12-31 原文

我正在努力了解新的 PayPal 自适应支付 API - 特别是 ConvertCurrency 功能。有没有人在 PHP 中遇到过这个问题?

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

最佳答案

转换为 dif 非常容易。使用 Paypal 的自适应支付 SDK 的货币。假设您有必要的信息。 (API username/passwd, etc) 向API发出请求,可以按如下方式完成:

<?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>';

输出应该是这样的:

$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
                                )

                        )

                )

        )

)

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

关于PayPal Adaptive Payments ConvertCurrency API 的 PHP 示例,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1789699/

有关PayPal Adaptive Payments ConvertCurrency API 的 PHP 示例的更多相关文章

  1. postman——集合——执行集合——测试脚本——pm对象简单示例02 - 2

    //1.验证返回状态码是否是200pm.test("Statuscodeis200",function(){pm.response.to.have.status(200);});//2.验证返回body内是否含有某个值pm.test("Bodymatchesstring",function(){pm.expect(pm.response.text()).to.include("string_you_want_to_search");});//3.验证某个返回值是否是100pm.test("Yourtestname",function(){varjsonData=pm.response.json

  2. Ruby-vips 图像处理库。有什么好的使用示例吗? - 2

    我对图像处理完全陌生。我对JPEG内部是什么以及它是如何工作一无所知。我想知道,是否可以在某处找到执行以下简单操作的ruby​​代码:打开jpeg文件。遍历每个像素并将其颜色设置为fx绿色。将结果写入另一个文件。我对如何使用ruby​​-vips库实现这一点特别感兴趣https://github.com/ender672/ruby-vips我的目标-学习如何使用ruby​​-vips执行基本的图像处理操作(Gamma校正、亮度、色调……)任何指向比“helloworld”更复杂的工作示例的链接——比如ruby​​-vips的github页面上的链接,我们将不胜感激!如果有ruby​​-

  3. arrays - 如何在下面的示例中将两个值数组分组为 n 个值数组? - 2

    我已经有很多两个值数组,例如下面的例子ary=[[1,2],[2,3],[1,3],[4,5],[5,6],[4,7],[7,8],[4,8]]我想把它们分组到[1,2,3],[4,5],[5,6],[4,7,8]因为意思是1和2有关系,2和3有关系,1和3有关系,所以1,2,3都有关系我如何通过ruby​​库或任何算法来做到这一点? 最佳答案 这是基本Bron–Kerboschalgorithm的Ruby实现:classGraphdefinitialize(edges)@edges=edgesenddeffind_maximum_

  4. ruby - Google-api-ruby-client 翻译 API 示例 - 2

    很高兴看到google代码:google-api-ruby-client项目,因为这对我来说意味着Ruby人员可以使用GoogleAPI-s来完善代码。虽然我现在很困惑,因为给出的唯一示例使用Buzz,并且根据我的实验,Google翻译(v2)api的行为必须与google-api-ruby-client中的Buzz完全不同。.我对“Explorer”演示示例很感兴趣——但据我所知,它并不是一个探索器。它所做的只是调用一个Buzz服务,然后浏览它已经知道的关于Buzz服务的事情。对我来说,Explorer应该让您“发现”所公开的服务和方法/功能,而不一定已经知道它们。我很想听听使用这个

  5. ruby - 是否有 SproutCore 或 Cappuccino 的现场演示/示例应用程序 - 2

    在他们的网站上找不到任何内容。我主要只是想看看哪个值得一试(当然是RIA)。谢谢 最佳答案 SproutCoredemos 关于ruby-是否有SproutCore或Cappuccino的现场演示/示例应用程序,我们在StackOverflow上找到一个类似的问题: https://stackoverflow.com/questions/1419788/

  6. ruby-on-rails - 这个 C 和 PHP 程序员如何学习 Ruby 和 Rails? - 2

    按照目前的情况,这个问题不适合我们的问答形式。我们希望答案得到事实、引用或专业知识的支持,但这个问题可能会引发辩论、争论、投票或扩展讨论。如果您觉得这个问题可以改进并可能重新打开,visitthehelpcenter指导。关闭9年前。我来自C、php和bash背景,很容易学习,因为它们都有相同的C结构,我可以将其与我已经知道的联系起来。然后2年前我学了Python并且学得很好,Python对我来说比Ruby更容易学。然后从去年开始,我一直在尝试学习Ruby,然后是Rails,我承认,直到现在我还是学不会,讽刺的是那些打着简单易学的烙印,但是对于我这样一个老练的程序员来说,我只是无法将它

  7. ruby - 如何设置自动测试以仅重新运行失败的 rspec 示例 - 2

    我对自动测试的工作方式的印象(基于cucumbergithubwiki和其他在线内容)是它应该重新运行红色示例,直到它们通过。我的问题是它会重新运行规范文件中找到失败示例的所有示例,包括通过的示例。我不想浪费时间在修复失败示例的同时重新运行通过的示例。是否可以配置自动测试以便仅运行失败的示例? 最佳答案 您需要rspec-retrygem。以下是文档中有关如何实现它的一些示例:将它应用到覆盖整个测试套件的configureblock中...RSpec.configuredo|config|config.verbose_retry=t

  8. ruby - 是否有 Rack::Session::Cookie 用法的基本示例? - 2

    我找不到任何使用Rack::Session::Cookie的简单示例,并且希望能够将信息存储在cookie中,并在以后的请求中访问它并让它过期.这些是我能找到的唯一示例:HowdoIset/getsessionvarsinaRackapp?http://rack.rubyforge.org/doc/classes/Rack/Session/Cookie.html这是我得到的:useRack::Session::Cookie,:key=>'rack.session',:domain=>'foo.com',:path=>'/',:expire_after=>2592000,:secret=

  9. ruby-on-rails - Ruby on rails + aws cognito 示例 - 2

    我正在构建一个部署在heroku上的rails5应用程序。我想用AWScongnito来实现单点登录,但是没有足够的例子来实现。我正在使用devise进行身份验证。现在我的目标是让我的所有用户都使用AWScognito并通过我的Rails应用程序对他们进行身份验证。这是我在AWScongnitowithrails上找到的唯一资源,我正在寻找一些示例应用程序或指向工具或ruby​​API文档的链接来实现此目的。请帮助。UpdateOnbasisOfBalaAnswerrequire'aws-sdk'ENV['AWS_ACCESS_KEY_ID']='XXXXXXXXXXXXXXXXX'E

  10. ruby - 如何编写插入 rspec 示例的方法? - 2

    在一堆rspecrails单元规范中,我做了类似的事情:describeFoodo[:bar,:baz].eachdo|a|it"shouldhavemany#{a}"doFoo.shouldhave_many(a)endendend为了更简洁的代码,我宁愿这样做:describeFoodospec_has_manyFoo,:bar,:bazend那么我该如何编写像spec_has_many()这样的辅助方法来像rspec的it()方法那样插入DSL代码呢?如果它是一个普通的实例方法,我会做类似的事情:defspec_has_many(model,*args)args.eachdo|a

随机推荐