Comment faire des achats en ligne via l'API PayPal
Date de publication : 23/03/2011.
IV. Paiement par PayPal
IV-A. Code
IV-B. Conclusion
IV. Paiement par PayPal
IV-A. Code
Tout d'abord, importez les namespaces suivants:
Imports com.paypal.soap.api
Imports com.paypal.sdk.profiles
Imports com.paypal.sdk.services
|
L'achat d'article via Paypal se fait en différentes étapes:
- 1e étape: SetExpressCheckout, qui renvoie un Token
- 2e étape: GetExpressCheckoutDetails, qui renvoie les informations sur l'utilisateur PayPal
- 3e étape: DoExpressCheckoutPayment, qui renvoie le résultat de la Transaction
Dans l'exemple suivant l'utilisateur Paypal réalise un achat de 3 articles:
Public Property panier As Panier
Public ReadOnly Property Action As String
Get
Return Request.QueryString("action")
End Get
End Property
Public ReadOnly Property Token As String
Get
Return Request.QueryString("token")
End Get
End Property
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
If Not Page.IsPostBack Then
LoadData()
If String.IsNullOrEmpty(Action) Then
Dim token As String = PaiementParPayPal_Etape1()
Dim payPalUrl As String = String.Format(My.Settings.PayPalIdentificationURL, token)
Response.Redirect(payPalUrl)
ElseIf Action = "return" Then
Response.Write(PaiementParPayPal_Etape3)
ElseIf Action = "cancel" Then
Response.Write("Transaction annulée")
End If
End If
End Sub
Private Sub LoadData()
Dim prd1 As Produit = New Produit(1, "Visual Studio", 1000, 10)
Dim prd2 As Produit = New Produit(2, "MSDN", 500, 5)
Dim prd3 As Produit = New Produit(3, "Formation", 100, 1)
panier = New Panier(prd1, prd2, prd3)
End Sub
Public Function PaiementParPayPal_Etape1() As String
Dim profile As IAPIProfile = ProfileFactory.createSignatureAPIProfile()
profile.APIUsername = My.Settings.Username
profile.APIPassword = My.Settings.Password
profile.APISignature = My.Settings.Signature
profile.Environment = My.Settings.Environment
Dim paysPayPal As CountryCodeType
If [Enum].IsDefined(GetType(CountryCodeType), "FR") Then
paysPayPal = CType([Enum].Parse(GetType(CountryCodeType), "FR"), CountryCodeType)
End If
Dim client As New PersonNameType
client.Salutation = "M"
client.FirstName = "Jean"
client.LastName = "Dupont"
Dim addresse As New AddressType
addresse.Name = client.Salutation + " " + client.FirstName + " " + client.LastName
addresse.Street1 = "22 rue du Paradis"
addresse.Street2 = ""
addresse.CityName = "Mérignac"
addresse.PostalCode = "33700"
addresse.CountryName = "France"
addresse.Country = paysPayPal
addresse.CountrySpecified = True
Dim payeur As New PayerInfoType
payeur.Payer = "jean.dupont@test.fr"
payeur.PayerID = ""
payeur.PayerStatus = PayPalUserStatusCodeType.unverified
payeur.PayerCountry = paysPayPal
payeur.PayerCountrySpecified = True
payeur.Address = addresse
payeur.PayerName = client
Dim codeMonnaiePayPal As CurrencyCodeType
If [Enum].IsDefined(GetType(CurrencyCodeType), "EUR") Then
codeMonnaiePayPal = CType([Enum].Parse(GetType(CurrencyCodeType), "EUR"), CurrencyCodeType)
End If
Dim montantHT As New BasicAmountType
montantHT.currencyID = codeMonnaiePayPal
montantHT.Value = panier.GetMontantHT.ToString("N")
Dim montantTaxe As New BasicAmountType
montantTaxe.currencyID = codeMonnaiePayPal
montantTaxe.Value = panier.GetMontantTaxe.ToString("N")
Dim montantTTC As New BasicAmountType
montantTTC.currencyID = codeMonnaiePayPal
montantTTC.Value = panier.GetMontantTTC.ToString("N")
Dim panierPaypal As New List(Of PaymentDetailsItemType)
For Each produit As Produit In panier.Produits
Dim quantite As Integer = panier.Produits.Where(Function(p) p.ID = produit.ID).Count
Dim produitPayPal As New PaymentDetailsItemType
produitPayPal.Number = produit.ID.ToString
produitPayPal.Name = produit.Nom
produitPayPal.Quantity = quantite.ToString
produitPayPal.Amount = New BasicAmountType
produitPayPal.Amount.currencyID = codeMonnaiePayPal
produitPayPal.Amount.Value = produit.Prix.ToString("N")
produitPayPal.Tax = New BasicAmountType
produitPayPal.Tax.currencyID = codeMonnaiePayPal
produitPayPal.Tax.Value = produit.Taxe.ToString("N")
panierPaypal.Add(produitPayPal)
Next
Dim appel As New CallerServices
appel.APIProfile = profile
Dim detailsPaiement As New PaymentDetailsType
detailsPaiement.ItemTotal = montantHT
detailsPaiement.TaxTotal = montantTaxe
detailsPaiement.OrderTotal = montantTTC
detailsPaiement.PaymentDetailsItem = panierPaypal.ToArray
Dim requestDetails As New SetExpressCheckoutRequestDetailsType
requestDetails.PaymentAction = PaymentActionCodeType.Sale
requestDetails.PaymentDetails = New PaymentDetailsType() {detailsPaiement}
requestDetails.OrderTotal = montantTTC
requestDetails.CancelURL = My.Settings.CancelURL
requestDetails.ReturnURL = My.Settings.ReturnURL
requestDetails.Address = addresse
Dim request As New SetExpressCheckoutRequestType
request.Version = My.Settings.Version
request.SetExpressCheckoutRequestDetails = requestDetails
Dim response As New SetExpressCheckoutResponseType
response = CType(appel.Call("SetExpressCheckout", request), SetExpressCheckoutResponseType)
If response.Errors IsNot Nothing Then
Throw New PayPalPaymentException(response.Errors)
End If
Return response.Token
End Function
Public Function PaiementParPayPal_Etape2() As String
Dim profile As IAPIProfile = ProfileFactory.createSignatureAPIProfile()
profile.APIUsername = My.Settings.Username
profile.APIPassword = My.Settings.Password
profile.APISignature = My.Settings.Signature
profile.Environment = My.Settings.Environment
Dim appel As New CallerServices
appel.APIProfile = profile
Dim requete As New GetExpressCheckoutDetailsRequestType
requete.Version = My.Settings.Version
requete.Token = Token
Dim reponse As New GetExpressCheckoutDetailsResponseType
reponse = CType(appel.Call("GetExpressCheckoutDetails", requete), GetExpressCheckoutDetailsResponseType)
If reponse.Errors IsNot Nothing Then
Throw New PayPalPaymentException(reponse.Errors)
End If
Return reponse.GetExpressCheckoutDetailsResponseDetails.PayerInfo.PayerID
End Function
Public Function PaiementParPayPal_Etape3() As String
Dim profile As IAPIProfile = ProfileFactory.createSignatureAPIProfile()
profile.APIUsername = My.Settings.Username
profile.APIPassword = My.Settings.Password
profile.APISignature = My.Settings.Signature
profile.Environment = My.Settings.Environment
Dim codeMonnaiePayPal As CurrencyCodeType
If [Enum].IsDefined(GetType(CurrencyCodeType), "EUR") Then
codeMonnaiePayPal = CType([Enum].Parse(GetType(CurrencyCodeType), "EUR"), CurrencyCodeType)
End If
Dim montantHT As New BasicAmountType
montantHT.currencyID = codeMonnaiePayPal
montantHT.Value = panier.GetMontantHT.ToString("N")
Dim montantTaxe As New BasicAmountType
montantTaxe.currencyID = codeMonnaiePayPal
montantTaxe.Value = panier.GetMontantTaxe.ToString("N")
Dim montantTTC As New BasicAmountType
montantTTC.currencyID = codeMonnaiePayPal
montantTTC.Value = panier.GetMontantTTC.ToString("N")
Dim panierPaypal As New List(Of PaymentDetailsItemType)
For Each produit As Produit In panier.Produits
Dim quantite As Integer = panier.Produits.Where(Function(p) p.ID = produit.ID).Count
Dim produitPayPal As New PaymentDetailsItemType
produitPayPal.Number = produit.ID.ToString
produitPayPal.Name = produit.Nom
produitPayPal.Quantity = quantite.ToString
produitPayPal.Amount = New BasicAmountType
produitPayPal.Amount.currencyID = codeMonnaiePayPal
produitPayPal.Amount.Value = produit.Prix.ToString("N")
produitPayPal.Tax = New BasicAmountType
produitPayPal.Tax.currencyID = codeMonnaiePayPal
produitPayPal.Tax.Value = produit.Taxe.ToString("N")
panierPaypal.Add(produitPayPal)
Next
Dim detailsPaiement As New PaymentDetailsType
detailsPaiement.ItemTotal = montantHT
detailsPaiement.TaxTotal = montantTaxe
detailsPaiement.OrderTotal = montantTTC
detailsPaiement.PaymentDetailsItem = panierPaypal.ToArray
Dim detailsRequete As New DoExpressCheckoutPaymentRequestDetailsType
detailsRequete.Token = Token
detailsRequete.PayerID = PaiementParPayPal_Etape2()
detailsRequete.PaymentAction = PaymentActionCodeType.Sale
detailsRequete.PaymentActionSpecified = True
detailsRequete.PaymentDetails = New PaymentDetailsType() {detailsPaiement}
Dim requete As New DoExpressCheckoutPaymentRequestType
requete.Version = My.Settings.Version
requete.DoExpressCheckoutPaymentRequestDetails = detailsRequete
Dim appel As New CallerServices
appel.APIProfile = profile
Dim reponse As New DoExpressCheckoutPaymentResponseType
reponse = CType(appel.Call("DoExpressCheckoutPayment", requete), DoExpressCheckoutPaymentResponseType)
If reponse.Errors IsNot Nothing Then
Throw New PayPalPaymentException(reponse.Errors)
End If
Return reponse.DoExpressCheckoutPaymentResponseDetails.Token
End Function
|
IV-B. Conclusion
L'article est terminé, si vous avez besoin de plus d'information, télécharger le
projet PayPal.


Copyright © 2011 Cédric Arnould.
Aucune reproduction, même partielle, ne peut être faite
de ce site ni de l'ensemble de son contenu : textes, documents, images, etc.
sans l'autorisation expresse de l'auteur. Sinon vous encourez selon la loi jusqu'à
trois ans de prison et jusqu'à 300 000 € de dommages et intérêts.