【问题标题】:How do I use the Adapative Payments "ConvertCurrency" API in Perl?如何在 Perl 中使用自适应支付“转换货币”API?
【发布时间】:2014-05-14 01:27:38
【问题描述】:

您如何使用 PayPal API(自适应支付) 系统从多种货币进行转换?这些文档只包含 Ruby、iOS、PHP、Rails 等的内容……但没有 Perl!

https://developer.paypal.com/docs/classic/api/adaptive-payments/ConvertCurrency_API_Operation/

【问题讨论】:

    标签: perl paypal-adaptive-payments


    【解决方案1】:

    这仅作为指导(在命令行中运行)。它将通过浏览器运行,但您需要添加一个标题(否则它会给出一个500 Internal Server Error

    perl代码如下:

    currency.cgi

    #!/usr/bin/perl
    
    use warnings;
    use strict;
    
    use HTTP::Request::Common;
    use LWP::UserAgent;
    
    my $user           = 'your PayPal API username';
    my $password       = 'your PayPal API password';
    my $signature      = 'your PayPal API signature';
    my $application_id = 'APP-80W284485P519543T'; # this is the sandbox app ID... so you would just change this to your own app-id when going live
    
    my @currencies = qw/GBP EUR CHF USD AUD/; # Enter all the currency codes you want to convert here
    
    my $url     = 'https://svcs.sandbox.paypal.com/AdaptivePayments/ConvertCurrency';  # remove the "sandbox." part of this URL, when its ready to go live...
    my $ua      = LWP::UserAgent->new();
    my $headers = HTTP::Headers->new(
            'X-PAYPAL-SECURITY-USERID'    => $user,
            'X-PAYPAL-SECURITY-PASSWORD'  => $password,
            'X-PAYPAL-SECURITY-SIGNATURE' => $signature,
            'X-PAYPAL-APPLICATION-ID'     => $application_id,
            'X-PAYPAL-DEVICE-IPADDRESS'   => $ENV{REMOTE_ADDR},
            'X-PAYPAL-REQUEST-DATA-FORMAT'   => 'JSON',
            'X-PAYPAL-RESPONSE-DATA-FORMAT'   => 'JSON'
            );
    
    
    foreach (@currencies) {
      print qq|\nGetting exchange rates for $_.... \n|;
      my ($status,$vals) = get_converted_amounts($_);
      if ($vals->{error}) {
          print qq|There was an error: $vals->{error}\n|;
          exit;
      } else {
          print qq|Got conversion rates of:\n|;
          foreach (@currencies) {
            if ($vals->{$_}) {
                print qq|\t$_ => $vals->{$_}\n|;
            }
          }
      }
    }
    
    
    sub get_converted_amounts {
    
        my ($currency_from) = @_;
    
        my @currencies_to_grab;
        foreach (@currencies) {
          next if $_ eq $currency_from; # We dont wanna bother asking it to convert from this currency, into this currency =)
          push @currencies_to_grab, $_; 
        }
    
        my $json_var = {
              requestEnvelope => { 
                detailLevel => "ReturnAll",
                errorLanguage =>  "en_US",
              },
              baseAmountList => [{ 'currency' => { 'code' => $currency_from, 'amount' => 1 } }],
              convertToCurrencyList => [{ currencyCode => \@currencies_to_grab }]
        };
    
        use JSON;
        my $new_json = JSON::to_json($json_var);
    
        my $request  = HTTP::Request->new( 'POST', $url, $headers, $new_json );
        my $response = $ua->request( $request );
    
        my $json_returned = decode_json($response->decoded_content);
    
        if ($json_returned->{error}[0]) {
          return (0, { error => "There was an error: $json_returned->{error}[0]->{message} ($json_returned->{error}[0]->{errorId}) " });
        }
    
        my $vals;
        foreach (@{$json_returned->{estimatedAmountTable}->{currencyConversionList}[0]->{currencyList}->{currency}}) {
          $vals->{$_->{code}} = $_->{amount};
        }
        return (1,$vals);
    
    }
    

    运行它:

    您只需通过 SSH/Telnet 运行它,使用:

    perl /path/to/script/currency.cgi

    您可以使用货币代码(确保只使用此处的 currencyCode 值:https://developer.paypal.com/docs/classic/api/adaptive-payments/ConvertCurrency_API_Operation/,因为这些是唯一受支持的)

    虽然这会将给定货币转换为其他相关货币(如果您想每隔几个小时运行一次脚本并存储转换率,这很好) - 调整它并不难,因此您可以这样做:

    convert(from_currency,to_currency,amount)

    希望这会为某人节省一点时间(因为我花了将近一天的时间来完成这项工作)

    【讨论】:

    • 您也可以查看metacpan.org/pod/Business::PayPal::Permissions 并为 Adaptive Payments 制作这样的 CPAN 模块;)
    • @jm666 - 我不确定你可以吗?您不需要权限 - 您只需要您的 API 用户/pass/sig/appid(与该模块要求的相同),仅此而已。
    • 我只是告诉你——而不是在这里发布一个模块——你应该制作一个 CPAN 模块——就像引用的那样......
    • 啊好吧 :) 我会在某个时候看看它。我从来没有真正擅长做模块(一旦写完,我就不知道如何编译/编写 POD)——但如果我有机会我会看看。
    猜你喜欢
    • 2014-07-08
    • 2014-07-05
    • 2020-05-20
    • 1970-01-01
    • 2012-10-13
    • 2016-08-07
    • 2011-04-29
    • 2012-03-21
    • 1970-01-01
    相关资源
    最近更新 更多