Android Code Snippet [B4X] Online Payment with PayPal

Hi all.
In Attached an example on how to use paypal for online payment in a simple way for B4X ( B4A - B4i - B4j )
In the example it is possible to enter any active paypal account, so you can also make payments on different paypal accounts.
For each transaction, it will return if the transaction was successful or failed.
If you improve the same, upload and share the same pls.

Have a nice day
Marco


 

Attachments

  • PayPalApp2019Community.zip
    33.8 KB · Views: 1,384
Last edited:

juventino883

Member
Licensed User
Longtime User
Hi @MarcoRome, I was trying this approach to use paypal and everithing is working great, but when a customer is paying through a PC (i'm using it with B4j for a webapp) the page doesn't redirect automatically to the success url, the user have to click on return to sellers website after the payment is made to be redirected to the success url, so if the user closes the browser after doing the payment and don't click on the return to seller's website button, the transaction won't be notiffied to my site, do you know if there is a way to force loading the succes url or to get a payment notification to avoid this kind of problem with paypal payments?.

thanks a lot in advance.
 

MarcoRome

Expert
Licensed User
Longtime User
Hi.
You need to set up automatic redirection from your paypal account.

1656405141858.png
 

Filippo

Expert
Licensed User
Longtime User
Hi all.
In Attached an example on how to use paypal for online payment in a simple way for B4X ( B4A - B4i - B4j )
In the example it is possible to enter any active paypal account, so you can also make payments on different paypal accounts.
For each transaction, it will return if the transaction was successful or failed.
If you improve the same, upload and share the same pls.

Have a nice day
Marco


Ciao Marco,

first thank you for sharing your solution. :)

I have successfully implemented your PayPal solution in my app.
Now I would like to give my customers also the possibility to make a cancellation.
That is, the customer gets the purchase price refunded and after that the app is no longer a full version.
Do you know how to start PayPal for such a function, or with what parameters?

Edit:
Please forget my question, I think it does not work.
 
Last edited:

MarcoRome

Expert
Licensed User
Longtime User
Ciao Marco,

first thank you for sharing your solution. :)

I have successfully implemented your PayPal solution in my app.
Now I would like to give my customers also the possibility to make a cancellation.
That is, the customer gets the purchase price refunded and after that the app is no longer a full version.
Do you know how to start PayPal for such a function, or with what parameters?

Edit:
Please forget my question, I think it does not work.
Ciao caro Filippo.

The example shown here does not use api or anything else but it is "a simple call" that you can use to make a payment via paypal and you have no other types of functionality.
If you want to add typical functionality you should use paypal API which you could always build via REST calls


or there are other systems like Sumup, woocommerce, etc

 

Filippo

Expert
Licensed User
Longtime User
Grazie Marco,

I think for the cancellation I will apply a simpler solution.
I offer the customer to contact me by email and I will refund back the purchase price.
 

Filippo

Expert
Licensed User
Longtime User
Hi,

I have executed a payment using the code. But after the payment the event "Private Sub WebView1_PageFinished (Url As String)" is not executed.
What can be the reason for this?
Is my code wrong?

B4X:
Sub parametri_pagamento (emailpaypal As String, ProductName_and_Info As String, Quantity As Int, Price As Double, currency As String, nome As String, cognome As String, indirizzo As String , cap As String, citta As String, emailcliente As String, telefonocliente As String, provincia As String )
    'Sostituisco al numero la virgola o il punto con %2e
    Dim modify_amount As String
    modify_amount = Price
    modify_amount = modify_amount.Replace(".","%2e")
    modify_amount = modify_amount.Replace(",","%2e")
    
'    Log(modify_amount)
    
    WebView1.LoadUrl($"https://www.paypal.com/cgi-bin/webscr?cmd=_xclick
    &business=${emailpaypal}
    &item_name=${ProductName_and_Info}
    &item_number=${Quantity}
    &amount=${modify_amount}
    &currency_code=${currency}
    &Ic=DE
    &first_name=${nome}
    &last_name=${cognome}
    &address1=${indirizzo}
    &city=${citta}
    &zip=${cap}
    &email=${emailcliente}
    &state=${provincia}
    &night_phone_b=${telefonocliente}
    &cancel_return=1
    &return=2"$)

End Sub

'The transaction was successful or not
'Result
'2 = Bezahlung erfolgreich
'1 = Bezahlung nicht erfolgreich
'0 = Bezahlung abgebrochen
Private Sub WebView1_PageFinished (Url As String)
    ProgressDialogHide

    'Log(Url)

    If Url = "2" Then
        PayPalResult = 2
        ToolBar1.SubTitel = "back to the main page"
    else if Url = "1" Then
        PayPalResult = 1
    else if Url = "0" Then
        PayPalResult = 0
    End If
    
    If PayPalResult > -1 Then
        Activity.Finish
    End If
End Sub
 

MarcoRome

Expert
Licensed User
Longtime User
Hi,

I have executed a payment using the code. But after the payment the event "Private Sub WebView1_PageFinished (Url As String)" is not executed.
What can be the reason for this?
Is my code wrong?

B4X:
Sub parametri_pagamento (emailpaypal As String, ProductName_and_Info As String, Quantity As Int, Price As Double, currency As String, nome As String, cognome As String, indirizzo As String , cap As String, citta As String, emailcliente As String, telefonocliente As String, provincia As String )
    'Sostituisco al numero la virgola o il punto con %2e
    Dim modify_amount As String
    modify_amount = Price
    modify_amount = modify_amount.Replace(".","%2e")
    modify_amount = modify_amount.Replace(",","%2e")
   
'    Log(modify_amount)
   
    WebView1.LoadUrl($"https://www.paypal.com/cgi-bin/webscr?cmd=_xclick
    &business=${emailpaypal}
    &item_name=${ProductName_and_Info}
    &item_number=${Quantity}
    &amount=${modify_amount}
    &currency_code=${currency}
    &Ic=DE
    &first_name=${nome}
    &last_name=${cognome}
    &address1=${indirizzo}
    &city=${citta}
    &zip=${cap}
    &email=${emailcliente}
    &state=${provincia}
    &night_phone_b=${telefonocliente}
    &cancel_return=1
    &return=2"$)

End Sub

'The transaction was successful or not
'Result
'2 = Bezahlung erfolgreich
'1 = Bezahlung nicht erfolgreich
'0 = Bezahlung abgebrochen
Private Sub WebView1_PageFinished (Url As String)
    ProgressDialogHide

    'Log(Url)

    If Url = "2" Then
        PayPalResult = 2
        ToolBar1.SubTitel = "back to the main page"
    else if Url = "1" Then
        PayPalResult = 1
    else if Url = "0" Then
        PayPalResult = 0
    End If
   
    If PayPalResult > -1 Then
        Activity.Finish
    End If
End Sub
Hi Filippo.
You set what was said in thread 9 ??
 

Filippo

Expert
Licensed User
Longtime User
Hi Filippo.
You set what was said in thread 9 ??

are you maining this?
the user have to registered in paypal (if I'm not wrong)
Yes the user is registered.

I also get the payment confirmation from PayPal.


I think this is only important if the payment is made from a website or?
1674829536526.png
 

MarcoRome

Expert
Licensed User
Longtime User
Hi,

I have executed a payment using the code. But after the payment the event "Private Sub WebView1_PageFinished (Url As String)" is not executed.
What can be the reason for this?
Is my code wrong?

B4X:
Sub parametri_pagamento (emailpaypal As String, ProductName_and_Info As String, Quantity As Int, Price As Double, currency As String, nome As String, cognome As String, indirizzo As String , cap As String, citta As String, emailcliente As String, telefonocliente As String, provincia As String )
    'Sostituisco al numero la virgola o il punto con %2e
    Dim modify_amount As String
    modify_amount = Price
    modify_amount = modify_amount.Replace(".","%2e")
    modify_amount = modify_amount.Replace(",","%2e")
   
'    Log(modify_amount)
   
    WebView1.LoadUrl($"https://www.paypal.com/cgi-bin/webscr?cmd=_xclick
    &business=${emailpaypal}
    &item_name=${ProductName_and_Info}
    &item_number=${Quantity}
    &amount=${modify_amount}
    &currency_code=${currency}
    &Ic=DE
    &first_name=${nome}
    &last_name=${cognome}
    &address1=${indirizzo}
    &city=${citta}
    &zip=${cap}
    &email=${emailcliente}
    &state=${provincia}
    &night_phone_b=${telefonocliente}
    &cancel_return=1
    &return=2"$)

End Sub

'The transaction was successful or not
'Result
'2 = Bezahlung erfolgreich
'1 = Bezahlung nicht erfolgreich
'0 = Bezahlung abgebrochen
Private Sub WebView1_PageFinished (Url As String)
    ProgressDialogHide

    'Log(Url)

    If Url = "2" Then
        PayPalResult = 2
        ToolBar1.SubTitel = "back to the main page"
    else if Url = "1" Then
        PayPalResult = 1
    else if Url = "0" Then
        PayPalResult = 0
    End If
   
    If PayPalResult > -1 Then
        Activity.Finish
    End If
End Sub

The WebView1_PageFinished (Url As String) event is fired when page URL is finished reading. In your case you load "1" or "2" which are not url's so I guess it doesn't run for that reason
 

Filippo

Expert
Licensed User
Longtime User
The WebView1_PageFinished (Url As String) event is fired when page URL is finished reading. In your case you load "1" or "2" which are not url's so I guess it doesn't run for that reason
Should I then instead of 1 or 2 , enter my website ?
 

Filippo

Expert
Licensed User
Longtime User
Now I understand how it works. :)
You have to enter a web address for purchase-successful and a web address for purchase-cancelled.

B4X:
Sub Globals
    Private url_success As String
    Private url_canceled As String
    Private PayPalResult As Int = 0
End Sub

Sub Activity_Create(FirstTime As Boolean)

    url_canceled = $"https://xxxxxxxxxx.de/PayPal_Result/PayPal_Cancel.html"$
    url_success = $"https://xxxxxxxxxx.de/PayPal_Result/PayPal_Success.html"$

    ProgressDialogShow2(Starter.language.Value("strWaitConnectPayPal"), False)
    parametri_pagamento("youremail@paypalaccount", Starter.descrizione_acquisto ,"1", Starter.prezzo_acquisto, "EUR", "First Name User","Last Name User","Address User", "Zip User", "City User", "email user","phone user ", "city/region/State user")

End Sub

Sub parametri_pagamento (emailpaypal As String, ProductName_and_Info As String, Quantity As Int, Price As Double, currency As String, nome As String, cognome As String, indirizzo As String , cap As String, citta As String, emailcliente As String, telefonocliente As String, provincia As String )
    'Sostituisco al numero la virgola o il punto con %2e
    Dim modify_amount As String
    modify_amount = Price
    modify_amount = modify_amount.Replace(".","%2e")
    modify_amount = modify_amount.Replace(",","%2e")
   
'    Log(modify_amount)
   
    WebView1.LoadUrl($"https://www.paypal.com/cgi-bin/webscr?cmd=_xclick
    &business=${emailpaypal}
    &item_name=${ProductName_and_Info}
    &item_number=${Quantity}
    &amount=${modify_amount}
    &currency_code=${currency}
    &Ic=DE
    &first_name=${nome}
    &last_name=${cognome}
    &address1=${indirizzo}
    &city=${citta}
    &zip=${cap}
    &email=${emailcliente}
    &state=${provincia}
    &night_phone_b=${telefonocliente}
    &cancel_return=${url_canceled}
    &return=${url_success}"$)

End Sub

'The transaction was successful or not
'Result
'2 = Bezahlung erfolgreich
'1 = Bezahlung nicht erfolgreich
'0 = Bezahlung abgebrochen
Private Sub WebView1_PageFinished (Url As String)
    ProgressDialogHide

    'Log(Url)

    If Url.Contains(url_success) Then
        PayPalResult = 2
        Activity.Finish
    else if Url = url_canceled Then
        PayPalResult = 1
        Activity.Finish
    End If
   
End Sub
 

khandah

Member
I tried this lib but I am always having this error : "issue":"Invalid numeric value: Leading zeroes not allowed"
Is this library still working ?
Thank you for your help.
 

MarcoRome

Expert
Licensed User
Longtime User
I tried this lib but I am always having this error : "issue":"Invalid numeric value: Leading zeroes not allowed"
Is this library still working ?
Thank you for your help.
Hi @khandah this isnt a library but simply passing parameters to a webview.
From your error it appears that you have not entered any number greater than 0 in the prezzo variable.
Try putting a value > 0 in the variable
 

Schakalaka

Active Member
Licensed User
hello, is still working this example? @MarcoRome
how to manage the action after successfull payment, for example writing some data in sql or mysql db?
in the zip file there aren' t the php pages.. I need a PP bbusiness account for use this function?
the example is a bit graphic different from the video.
Thank you
 
Last edited:

MarcoRome

Expert
Licensed User
Longtime User
hello, is still working this example? @MarcoRome
how to manage the action after successfull payment, for example writing some data in sql or mysql db?
in the zip file there aren' t the php pages.. I need a PP bbusiness account for use this function?
the example is a bit graphic different from the video.
Thank you

It should, try.
Regarding the other question, see


 
Top