我如何使用自适应支付" ConvertCurrency n#34; Perl中的API?

时间:2014-04-02 15:59:12

标签: perl paypal-adaptive-payments

如何使用 PayPal API(自适应付款)系统转换多种货币?这些文档只包含Ruby,iOS,PHP,Rails等内容......但不是Perl!

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

1 个答案:

答案 0 :(得分: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)

希望这会节省一些时间(因为我花了将近一天的时间来试图实现这一目标)