将PayPal REST客户端与Delphi XE2一起使用 [英] Using PayPal REST client with Delphi XE2

查看:71
本文介绍了将PayPal REST客户端与Delphi XE2一起使用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我尝试按照此处的说明与PayPal REST客户端进行交互:

I am trying to interface with the PayPal REST client, following the instructions here:

https://developer.paypal.com/docs /integration/direct/进行您的首次呼叫/

我可以使用具有以下代码的TIdHttp组件成功获取访问令牌:

I can successfully obtain the access token using a TIdHttp component with this code:

http.Request.ContentType := 'application/x-www-form-urlencoded';
http.Request.Accept := 'application/json';
http.Request.AcceptLanguage := 'en_US';
http.Request.BasicAuthentication := True;
http.Request.Username := 'my paypal clientid';
http.Request.Password := 'my paypal secret';

slParameters := TStringList.Create;
Response := TStringStream.Create;
try
  //get an access token
  slParameters.Add('grant_type=client_credentials');
  http.Post('https://api.sandbox.paypal.com/v1/oauth2/token', slParameters, Response);
  json := Response.DataString;
  PayPalObj := TJSONObject.ParseJSONValue(TEncoding.ASCII.GetBytes(json), 0) as TJSONObject;
  try
    jTokenValue := PayPalObj.Get('access_token').JsonValue;
    AccessToken := jTokenValue.Value;
    jTokenValue := PayPalObj.Get('token_type').JsonValue;
    TokenType := jTokenValue.Value;
  finally
    PayPalObj.Free;
  end;

  if TokenType <> 'Bearer' then
    Exit;

  if AccessToken = '' then
    Exit;

  ....

finally
  Response.Free;
  slParameters.Free;
end;

一旦我有了令牌,就应该可以创建付款.在PayPal网站上,此处提供了使用cURL的示例:

Once I have the token I should be able to create a payment. On the PayPal website an example using cURL is given here:

https://developer.paypal.com/docs/integration /web/accept-paypal-payment/

这是我尝试过的:

//create a payment
PayPalObj := TJSONObject.Create;
try
  PayPalObj.AddPair(TJSONPair.Create('intent', TJSONString.Create('sale')));

  RedirectObj := TJSONObject.Create;
  try
    RedirectObj.AddPair(TJSONPair.Create('return_url', TJSONString.Create('http://blahblah.com/return')));
    RedirectObj.AddPair(TJSONPair.Create('cancel_url', TJSONString.Create('http://blahblah.com/cancel')));
  except
    RedirectObj.Free;
    Exit;
  end;

  PayerObj := TJSONObject.Create;
  try
    PayerObj.AddPair(TJSONPair.Create('payment_method', TJSONString.Create('paypal')));
  except
    PayerObj.Free;
    Exit;
  end;

  TransactionsArray := TJSONArray.Create;
  AmountObj := TJSONObject.Create;
  TransactionObj := TJSONObject.Create;
  try
    AmountObj.AddPair('total', TJSONString.Create('7.47'));
    AmountObj.AddPair('currency', TJSONString.Create('USD'));
    TransactionObj.AddPair('amount', AmountObj);
    TransactionObj.AddPair('description', TJSONString.Create('payment description'));
    TransactionsArray.Add(TransactionObj);
  except
    TransactionsArray.Free;
    AmountObj.Free;
    TransactionObj.Free;
    Exit;
  end;

  PayPalObj.AddPair(TJSONPair.Create('redirect_urls', RedirectObj));
  PayPalObj.AddPair(TJSONPair.Create('payer', PayerObj));
  PayPalObj.AddPair(TJSONPair.Create('transactions', TransactionsArray));

  slParameters.Clear;
  Response.Clear;

  http.Request.ContentType := 'application/json';
  http.Request.CustomHeaders.Clear;
  //http.Request.CustomHeaders.FoldLines := False;  have tried this with no success
  http.Request.CustomHeaders.AddValue('Authorization', Format('Bearer %s', [AccessToken]));  //token obtained from first request

  slParameters.Add(PayPalObj.ToString);
  http.Post('https://api.sandbox.paypal.com/v1/payments/payment', slParameters, Response);
  json := Response.DataString;
  ...
finally
  PayPalObj.Free;
end;

我没有得到任何回应.我确信我已经正确构造了JSON字符串,因为我已经将其与示例之一进行了仔细比较.我还使用cURL测试了一个示例,它确实起作用.我不确定是否像我所做的那样将JSON字符串添加到字符串列表中是否正确.我也不确定是否需要在某处包括"-d" cURL参数.任何建议将不胜感激.

I'm not getting any response. I am sure I have constructed the JSON string correctly as I have carefully compared it with the sample one. I have also tested the sample one using cURL and it does work. I'm not sure if it's right to add the JSON string into a string list as I have done. I'm also not sure if I need to include the "-d" cURL parameter somewhere. Any advice would be gratefully received.

推荐答案

第二步,您不能使用TStringList发布JSON数据.仅适用于application/x-www-form-urlencoded帖子.要发布JSON,您需要改用TStream.

In the second step, you cannot use a TStringList to post the JSON data. That only works for application/x-www-form-urlencoded posts. To post JSON, you need to use a TStream instead.

此外,您无需使用TStringStream即可将响应作为字符串获得. Post()可以直接返回String.

Also, you don't need to use a TStringStream to get a response as a String. Post() can return a String directly.

尝试一下:

json := http.Post('https://api.sandbox.paypal.com/v1/oauth2/token', slParameters);
...
ssJson := TStringStream.Create(PayPalObj.ToString, TEncoding.ASCII);
try
  json := http.Post('https://api.sandbox.paypal.com/v1/payments/payment', ssJson);
finally
  ssJson.Free;
end;

这篇关于将PayPal REST客户端与Delphi XE2一起使用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆