Comment faire des achats en ligne via l'API PayPal
Date de publication : 23/03/2011.
III. Paiement par carte
III-A. Code
III-B. Erreur de configuration
III. Paiement par carte
III-A. Code
Tout d'abord, importez les namespaces suivants:
Imports com. paypal . soap . api
Imports com. paypal . sdk . profiles
|
Dans l'exemple suivant M Jean Dupont réalise un achat de 3 articles:
Public Property panier As Panier
Protected Sub Page_Load (ByVal sender As Object, ByVal e As System. EventArgs ) Handles Me. Load
If Not Page. IsPostBack Then
LoadData ()
Response . Write (PaiementParCarteDeCredit (). PaymentTransactionDetails . PaymentInfo . Subject )
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 PaiementParCarteDeCredit () As GetTransactionDetailsResponseType
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. PayerStatus = PayPalUserStatusCodeType. unverified
payeur. PayerCountry = paysPayPal
payeur. PayerCountrySpecified = True
payeur. Address = addresse
payeur. PayerName = client
Dim typeCarteDeCredit As CreditCardTypeType
If [Enum]. IsDefined (GetType (CreditCardTypeType), " Visa " ) Then
typeCarteDeCredit = CType ([Enum]. Parse (GetType (CreditCardTypeType), " Visa " ), CreditCardTypeType)
End If
Dim carteDeCredit As New CreditCardDetailsType
carteDeCredit. CreditCardNumber = " ****************** "
carteDeCredit. CreditCardType = typeCarteDeCredit
carteDeCredit. CreditCardTypeSpecified = True
carteDeCredit. CVV2 = " *** "
carteDeCredit. ExpMonth = 12
carteDeCredit. ExpMonthSpecified = True
carteDeCredit. ExpYear = 2015
carteDeCredit. ExpYearSpecified = True
carteDeCredit. CardOwner = payeur
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 detailsRequete As New DoDirectPaymentRequestDetailsType
detailsRequete. IPAddress = HttpContext. Current . Request . UserHostAddress
detailsRequete. PaymentAction = PaymentActionCodeType. Sale
detailsRequete. CreditCard = carteDeCredit
detailsRequete. ReturnFMFDetails = True
detailsRequete. ReturnFMFDetailsSpecified = True
detailsRequete. PaymentDetails = New PaymentDetailsType
detailsRequete. PaymentDetails . ItemTotal = montantHT
detailsRequete. PaymentDetails . TaxTotal = montantTaxe
detailsRequete. PaymentDetails . OrderTotal = montantTTC
detailsRequete. PaymentDetails . ShipToAddress = addresse
detailsRequete. PaymentDetails . PaymentDetailsItem = panierPaypal. ToArray
Dim achatPaypal As New DoDirectPaymentRequestType
achatPaypal. Version = My. Settings . Version
achatPaypal. DoDirectPaymentRequestDetails = detailsRequete
Dim appel As New CallerServices
appel. APIProfile = profile
Dim reponseAchatPaypal As New DoDirectPaymentResponseType
reponseAchatPaypal = CType (appel. Call (" DoDirectPayment " , achatPaypal), DoDirectPaymentResponseType)
If Not (reponseAchatPaypal. Ack = AckCodeType. Success OrElse reponseAchatPaypal. Ack = AckCodeType. SuccessWithWarning ) Then
If reponseAchatPaypal. Errors IsNot Nothing Then
Throw New PayPalPaymentException (reponseAchatPaypal. Errors )
End If
End If
Dim infoAchatPayPal As New GetTransactionDetailsRequestType
infoAchatPayPal. TransactionID = reponseAchatPaypal. TransactionID
infoAchatPayPal. Version = My. Settings . Version
Dim reponseInfoAchatPayPal As New GetTransactionDetailsResponseType
reponseInfoAchatPayPal = CType (appel. Call (" GetTransactionDetails " , infoAchatPayPal), GetTransactionDetailsResponseType)
Return reponseInfoAchatPayPal
End Function
|
III-B. Erreur de configuration
|
Si vous avez cette erreur:
|
This transaction cannot be processed due to an invalid merchant configuration.
|
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.