Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Paypal JS SDK API
#1
Hi at all,
i have integrated paypal javascript api in intraweb and this is the code on OnApprove Payment

onApprove: function(data, actions) {    ' + sLineBreak +
                '  $.busyLoadSetup({ fontawesome: "fa fa-spinner fa-spin fa-5x fa-fw" }); $.busyLoadFull("show"); ' + sLineBreak +
                '  return actions.order.capture().then(function(orderData) {  ' + sLineBreak +
                '  // dettaglio di risposta ' + sLineBreak +
                '    addHiddenInput("JSONRESPONSE",JSON.stringify(orderData, null, 2));    ' + sLineBreak +
                '    ajaxCall("JsonPayPalResponse","&TipoAbbonamento='+ TipoAbbonamento.ToString +'",false); //abbonamento personale ' + sLineBreak +
                '    });                  ' + sLineBreak +
                '  },  ' + sLineBreak +


(AggiungiLog add log a table to get state of payment)
I use an hidden input to capture json and then i pass it to callback.
I use 2 callback 1 is

procedure TBSNuovoPagamentoPaypalForm.JsonPayPalResponse(EventParams: TStringList);
var
  js      : string;
  TipoAbb : string;
  dm      : TMod1;
  jsCall  : string;
begin
  TipoAbb := EventParams.Values['TipoAbbonamento'];
  //salvo il log della response
 
  js := Format('addHiddenInput("%s",%s);', ['JSONTOELAB', 'document.getElementsByName("JSONRESPONSE")[0].value']);
  jsCall := ' ajaxCall("ElaboraPagamentiDaJson","&TipoAbbonamento='+TipoAbb+'",false);';
  js := js + jsCall;
  //cambiato su un'unica chiamata
  WebApplication.CallBackResponse.AddJavaScriptToExecuteAsCDATA(js);
end;


and the other is to get the hidden input value

procedure TBSNuovoPagamentoPaypalForm.ElaboraPagamentiDaJson(EventParams: TStringList);
var
  JSonProcess              : string;
  TipoPagamento            : string;
  JSonObject              : TJSONObject;
  JSonValueCompleted      : TJSonValue;
  JSonValueId              : TJSonValue;
  Abbonamento              : string;
  dm                      : TMod1;
begin
  JSonProcess := EventParams.Values['JSONTOELAB'];
  TipoPagamento := EventParams.Values['TipoAbbonamento'];
  JSonObject := nil;
  dm := LockDataModule();
  try
    dm.AggiungiLog('Risposta json server paypal',Copy(JSonProcess,1,3000));
    JSonObject := TJSONObject.ParseJSONValue(TEncoding.UTF8.GetBytes(JSonProcess), 0) as TJSONObject;

    if JSonObject = nil then
    begin
      WebApplication.ShowMessage('Errore nella decodifica di risposta del server Paypal.com');
      dm.AggiungiLog('Errore decodifica json paypal','ElaboraPagamentiDaJson JSonObject nil');
      Exit;
    end;

    JSonValueCompleted := JSonObject.GetValue('status');
    JSonValueId := JSonObject.GetValue('id');
    if (UpperCase(JSonValueCompleted.Value) = 'COMPLETED') and (JSonValueId.Value <> '')  then
    begin
      //pagamento completato e posso procedere con generazione fattura e abilitazione utente
      IWTimer1.ExtraTagParams.AddPair('TipoAbbonamento',TipoPagamento);
      IWTimer1.ExtraTagParams.AddPair('IDPAGAMENTO',JSonValueId.Value);
      if TipoPagamento = CODABBINPS.ToString then //per abbonamento comunicazioni inps.
        IWTimer1.ExtraTagParams.AddPair('Datore',CodDatore.ToString);
      BSDialogoAttesa.ModalVisible := True;
      IWTimer1.Enabled := True;
    end;

  finally
    if Assigned(JSonObject) then
      JSonObject.Free;
    UnlockDataModule(dm);
    WebApplication.CallBackResponse.AddJavaScriptToExecute('$.busyLoadFull("hide");');
  end;
end;

with JavaScript do // creo hidden input per il ritorno del valore funzione javascript
begin
    Add('function addHiddenInput(aName,aValue) {');
    Add('  var frm = $(getSubmitForm());');
    Add('  if (!frm.length) return false;');
    Add('  var hid = frm.find("input[name=\"" + aName + "\"]");');
    Add('  if (!hid.length) {');
    Add('    hid = $("<input type=\"hidden\" name =\"" + aName + "\">");');
    Add('    frm.append(hid);');
    Add('  }');
    Add('  hid.val(aValue);');
    Add('  AddChangedControl(aName);');
    Add('}');
    Add('');
 end;

Then the timer excute code to activate the service to user with a busy modal dialog 

Sometimes callback JsonPayPalResponse does not fire. In paypal i have the payment in status complete and response ok but in the OnApprove can't fire the callback to activate the service for the user.
In sandbox and test case i can't reproduce the issue but some user can't activate. I open a ticket on paypal and the order is correctly captured with json response ok.
The calls to paypal are javascript based on documentation that you can find here https://developer.paypal.com/sdk/js/configuration/

Is the right way to process jSon that come from paypal onApprove callback?

Thank you

Andrea
Reply


Messages In This Thread
Paypal JS SDK API - by Anto90 - 05-04-2022, 03:52 PM
RE: Paypal JS SDK API - by Alexandre Machado - 05-27-2022, 03:00 AM
RE: Paypal JS SDK API - by Anto90 - 06-04-2022, 02:10 PM
RE: Paypal JS SDK API - by Anto90 - 08-03-2022, 07:19 AM
RE: Paypal JS SDK API - by Alexandre Machado - 08-04-2022, 10:38 PM

Forum Jump:


Users browsing this thread: 1 Guest(s)