以下是有关PayPal付款申请的代码。我收到401错误,但我不确定它来自何处或如何处理它。我相信它来自我的自定义标题配置。
如何解决?
FUNCTION GetAccessToken :String ;
Var
AccessToken :TJSONValue ;
BEGIN
AccessToken := PayPalTokenObj.Get('access_token').JsonValue ;
Result := AccessToken.Value ;
PayPalForm.Memo1.Lines.Add('Access Token: ' + Result) ;
END ; {GetAccessToken}
FUNCTION GetTokenType :String ;
Var
AccessTokenType :TJSONValue ;
BEGIN
AccessTokenType := PayPalTokenObj.Get('token_type').JsonValue ;
Result := AccessTokenType.Value ;
PayPalForm.Memo1.Lines.Add('Token Type: ' + Result) ;
END ; {GetTokenType}
procedure SendPayment;
const
PaymentRequest = 'https://api.sandbox.paypal.com/v1/payments/payment';
var
PayPalPaymentRequestObj, PayerObj, AmountObj, TransactionObj, RedirectObj: TJSONObject;
TransactionsArray :TJSONArray ;
InputJSON: TStringStream;
Result :String ;
begin
//Create a JSON object called PayPalPaymentRequest
PayPalPaymentRequestObj := TJSONObject.Create;
try
PayPalPaymentRequestObj.AddPair(TJSONPair.Create('intent', TJSONString.Create('sale'))) ;
RedirectObj := TJSONObject.Create;
try
PayPalPaymentRequestObj.AddPair('redirect_urls', RedirectObj);
RedirectObj.AddPair(TJSONPair.Create('return_url', TJSONString.Create('http://www.SoftwareMomentum.com')));
RedirectObj.AddPair(TJSONPair.Create('cancel_url', TJSONString.Create('http://www.Yahoo.com')));
except
RedirectObj.Free;
raise;
end;
PayerObj := TJSONObject.Create;
try
PayPalPaymentRequestObj.AddPair('payer', PayerObj);
PayerObj.AddPair(TJSONPair.Create('payment_method', TJSONString.Create('paypal')));
except
PayerObj.Free;
raise;
end;
TransactionsArray := TJSONArray.Create;
AmountObj := TJSONObject.Create;
TransactionObj := TJSONObject.Create;
try
PayPalPaymentRequestObj.AddPair('transactions', TransactionsArray) ;
AmountObj.AddPair(TJSONPair.Create('total', TJSONString.Create('7.47')));
AmountObj.AddPair(TJSONPair.Create('currency', TJSONString.Create('USD')));
TransactionObj.AddPair('amount', AmountObj);
TransactionObj.AddPair(TJSONPair.Create('description', TJSONString.Create('payment description')));
// TransactionsArray.Add(AmountObj);
TransactionsArray.Add(TransactionObj);
except
TransactionsArray.Free;
AmountObj.Free;
TransactionObj.Free;
Exit;
end;
with Http do
begin
Request.Clear;
Request.CustomHeaders.Clear ;
Request.BasicAuthentication := False;
Request.CustomHeaders.Values['Authorization'] := Format(GetTokenType + ' %s', [GetAccessToken]);
Request.ContentType := 'application/json';
Request.Accept := 'application/json' ;
Request.ContentLength := 431 ;
Request.UserAgent := 'PayPalSDK/NIW/HATEOAS' ;
end; {with}
InputJSON := TStringStream.Create(PayPalPaymentRequestObj.ToString, TEncoding.UTF8);
try
HTTP.ProtocolVersion := pv1_1 ;
HTTP.AllowCookies := True ;
HTTP.HandleRedirects := True ;
//Result := HTTP.Post(PaymentRequest, PostData);
Result := HTTP.Post(PaymentRequest, InputJSON) ;
finally
InputJSON.Free;
end ; {try-finally}
finally
PayPalPaymentRequestObj.Free;
end ; {try-finally}
end;
答案 0 :(得分:0)
您忽略了服务器的实际响应。您的代码假设响应被写入与提供请求数据相同的TStream
,但情况并非如此。
您正在调用以Post()
作为输入的TStream
版本并返回String
作为输出:
function Post(AURL: string; ASource: TStream): string; overload;
您忽略了返回的String
,而是使用原始请求数据,因此您正在体验回显。服务器没有将请求回显给您,您只是处理发送的任何内容,就好像它已被返回一样。
您需要更改此代码:
HTTP.Post(PaymentRequest, Response) ;
Result := Response.DataString ;
改为:
Result := HTTP.Post(PaymentRequest, Response);
然后,您应该将Response
变量重命名为更合适的名称。并且还清理你的内存管理,因为如果出现问题你会有一些内存泄漏。
尝试这样的事情:
procedure SendPayment;
const
PaymentRequest = 'https://api.sandbox.paypal.com/v1/payments/payment';
var
PayPalPaymentRequestObj, PayerObj, AmountObj, TransactionObj, RedirectObj: TJSONObject;
TransactionsArray: TJSONArray;
InputJSON: TStringStream;
Result: String;
begin
//Create a JSON object called PayPalPaymentRequest
PayPalPaymentRequestObj := TJSONObject.Create;
try
PayPalPaymentRequestObj.AddPair('intent', 'sale');
RedirectObj := TJSONObject.Create;
try
RedirectObj.AddPair('return_url', 'http://www.SoftwareMomentum.com');
RedirectObj.AddPair('cancel_url', 'http://www.Yahoo.com');
PayPalPaymentRequestObj.AddPair('redirect_urls', RedirectObj);
except
RedirectObj.Free;
raise;
end;
PayerObj := TJSONObject.Create;
try
PayerObj.AddPair('payment_method', 'paypal');
PayPalPaymentRequestObj.AddPair('payer', PayerObj);
except
PayerObj.Free;
raise;
end;
TransactionsArray := TJSONArray.Create;
try
TransactionObj := TJSONObject.Create;
try
AmountObj := TJSONObject.Create;
try
AmountObj.AddPair('total', '7.47');
AmountObj.AddPair('currency', 'USD');
TransactionObj.AddPair('amount', AmountObj);
except
AmountObj.Free;
raise;
end;
TransactionObj.AddPair('description', 'payment description');
TransactionsArray.Add(TransactionObj);
except
TransactionObj.Free;
raise;
end;
PayPalPaymentRequestObj.AddPair('transactions', TransactionsArray);
except
TransactionsArray.Free;
raise;
end;
with Http do
begin
ProtocolVersion := pv1_1;
AllowCookies := True;
HandleRedirects := True;
Request.Clear;
Request.BasicAuthentication := False;
Request.CustomHeaders.Values['Authorization'] := Format('%s %s', [GetTokenType, GetAccessToken]);
Request.ContentType := 'application/json';
Request.Accept := 'application/json';
Request.UserAgent := 'PayPalSDK/NIW/HATEOAS';
end;
InputJSON := TStringStream.Create(PayPalPaymentRequestObj.ToString, TEncoding.UTF8);
try
Result := HTTP.Post(PaymentRequest, InputJSON);
finally
InputJSON.Free;
end;
finally
PayPalPaymentRequestObj.Free;
end;
end;