Hola,
estoy tratando de hacer una aplicacion para pago con PayPal (en realidad es para android e iOS pero lo que pruebo funciona en ambos mundos).
Uso la REST API de paypal
https://developer.paypal.com/webapps...oper/docs/api/
La cuestion es que si puedo obtener el TOKEN, es decir ya me valide el acceso.
Pero al hacer el pago (Payment) me devuelve la respuesta JSON en blanco, alguien puede ayudarme.
Adjunto codigo fuente.
En el codigo fuente no pongo client id y secreto, por cuestion de seguridad.
En esta lista esta el codigo fuete y el ejecutable:
www.itcmx.com/PayPalREST.rar
Por su valiosa ayuda mil gracias
Este es el codigo para el token
Código Delphi
[-]
var
slParameters:TStringList;
Response:TStringStream;
json, sTokenType:string;
PayPalObj:TJSONObject;
jTokenValue:TJSONValue;
begin
memLog.Lines.Clear;
memLog.Lines.Add('TOKEN');
btnPayment.Enabled := False;
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 := 'CLIENT ID';
http.Request.Password := 'SECRETA';
slParameters := TStringList.Create;
Response := TStringStream.Create;
try
slParameters.Add('grant_type=client_credentials');
json := http.Post('https://api.sandbox.paypal.com/v1/oauth2/token', slParameters);
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;
sTokenType := jTokenValue.Value;
Memo1.Lines.Clear;
Memo1.Lines.Text := PayPalObj.ToString;
Memo1.Lines.Add('Size: ' + IntToStr(PayPalObj.EstimatedByteSize));
finally
PayPalObj.Free;
end;
if sTokenType <> 'Bearer' then
Exit;
if AccessToken = '' then
Exit;
lblToken.Caption := Format('Token: %s - Type: %s', [AccessToken, sTokenType]);
btnPayment.Enabled := True;
finally
Response.Free;
slParameters.Free;
end;
Y aqui el del payment
Código Delphi
[-]
var
PayPalObj :TJSONObject;
PayPalObj2 :TJSONObject;
RedirectObj :TJSONObject;
PayerObj :TJSONObject;
TransactionsArray : TJSONArray;
AmountObj : TJSONObject;
TransactionObj : TJSONObject;
FundingArray : TJSONArray;
CreditCardObj : TJSONObject;
ssJson : TStringStream;
json, Authorization:string;
begin
memLog.Lines.Add('PAYMENT');
PayPalObj := TJSONObject.Create;
try
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('credit_card')));
CreditCardObj := TJSONObject.Create;
CreditCardObj.AddPair('number', '5500005555555559');
CreditCardObj.AddPair('type', 'mastercard');
CreditCardObj.AddPair('expire_month', '12');
CreditCardObj.AddPair('expire_year', '2018');
CreditCardObj.AddPair('cvv2', '111');
CreditCardObj.AddPair('first_name', 'yo');
CreditCardObj.AddPair('last_name', 'tu');
FundingArray := TJSONArray.Create;
FundingArray.Add(CreditCardObj);
PayerObj.AddPair(TJSONPair.Create('funding_instruments', FundingArray));
except
PayerObj.Free;
Exit;
end;
TransactionsArray := TJSONArray.Create;
AmountObj := TJSONObject.Create;
TransactionObj := TJSONObject.Create;
try
AmountObj.AddPair('currency', TJSONString.Create('USD'));
AmountObj.AddPair('total', TJSONString.Create('1'));
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('intent', TJSONString.Create('sale')));
PayPalObj.AddPair(TJSONPair.Create('payer', PayerObj));
PayPalObj.AddPair(TJSONPair.Create('transactions', TransactionsArray));
Memo1.Lines.Add(' ');
Memo1.Lines.Add(PayPalObj.ToString);
Memo1.Lines.Add('Size: ' + IntToStr(PayPalObj.EstimatedByteSize));
http.Request.Clear;
http.Request.ContentType := 'application/json';
http.Request.CustomHeaders.Clear;
Authorization := Format('Bearer %s', [AccessToken]);
http.Request.CustomHeaders.AddValue('Authorization', Authorization);
Memo1.Lines.Add(' ');
Memo1.Lines.Add(http.Request.CustomHeaders.Text);
ssJson := TStringStream.Create(PayPalObj.ToString, TEncoding.ASCII);
try
json := http.Post('https://api.sandbox.paypal.com/v1/payments/payment', ssJson);
lblPayment.Caption := json;
PayPalObj2 := TJSONObject.ParseJSONValue(TEncoding.ASCII.GetBytes(json), 0) as TJSONObject;
if Assigned(PayPalObj2) then
try
Memo1.Lines.Add(' ');
Memo1.Lines.Add(PayPalObj2.ToString);
finally
PayPalObj2.Free;
end;
finally
ssJson.Free;
end;
finally
PayPalObj.Free;
end;