Using PayPal REST client with Delphi XE2

2019-08-04 04:21发布

问题:

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

https://developer.paypal.com/docs/integration/direct/make-your-first-call/

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;

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/

This is what I have tried:

//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;

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.

回答1:

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.

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

Try this:

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;