Club Delphi  
    FTP   CCD     Buscar   Trucos   Trabajo   Foros

Retroceder   Foros Club Delphi > Principal > Delphi para la web
Registrarse FAQ Miembros Calendario Guía de estilo Temas de Hoy

Grupo de Teaming del ClubDelphi

 
 
Herramientas Buscar en Tema Desplegado
  #1  
Antiguo 23-11-2014
Avatar de oesqueda
oesqueda oesqueda is offline
Miembro
 
Registrado: dic 2007
Ubicación: Guadalajara, Mexico
Posts: 66
Poder: 17
oesqueda Va por buen camino
Exclamation PayPal usando REST

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
  //get an access token
  slParameters.Add('grant_type=client_credentials');
  json := http.Post('https://api.sandbox.paypal.com/v1/oauth2/token', slParameters);
  //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;
    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
//create a payment
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));
  //PayPalObj.AddPair(TJSONPair.Create('redirect_urls', RedirectObj));

  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;
  //http.Request.CustomHeaders.FoldLines := False;  //have tried this with no success
  Authorization := Format('Bearer %s', [AccessToken]);
  http.Request.CustomHeaders.AddValue('Authorization', Authorization);  //token obtained from first request

  Memo1.Lines.Add(' ');
  Memo1.Lines.Add(http.Request.CustomHeaders.Text);
  ssJson := TStringStream.Create(PayPalObj.ToString, TEncoding.ASCII);
  try
    { AQUI SE OBTIENE EL VALOR EN BLANCO }
    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;
Archivos Adjuntos
Tipo de Archivo: rar PayPalREST.rar (59,3 KB, 40 visitas)
__________________
OEsqueda
Responder Con Cita
 



Normas de Publicación
no Puedes crear nuevos temas
no Puedes responder a temas
no Puedes adjuntar archivos
no Puedes editar tus mensajes

El código vB está habilitado
Las caritas están habilitado
Código [IMG] está habilitado
Código HTML está deshabilitado
Saltar a Foro

Temas Similares
Tema Autor Foro Respuestas Último mensaje
Rest roman La Taberna 11 30-07-2014 16:52:00
Datasnap Rest Server dison Desarrollo en Delphi para Android 3 16-05-2014 09:48:44
REST, Marshaling y \ iuqrul Providers 1 08-11-2013 10:51:33
Donaciones Paypal, sugerencia. REHome La Taberna 12 07-11-2010 16:54:05
¿quien usa paypal en México.? JXJ La Taberna 4 18-02-2010 22:59:33


La franja horaria es GMT +2. Ahora son las 00:22:26.


Powered by vBulletin® Version 3.6.8
Copyright ©2000 - 2024, Jelsoft Enterprises Ltd.
Traducción al castellano por el equipo de moderadores del Club Delphi
Copyright 1996-2007 Club Delphi