2012-04-02 8 views
8

Ich bin auf der Suche nach einer Anwendung, wo der Benutzer die Möglichkeit haben wird, Produkte von meiner Website zu kaufen. Der Kauf umfasst physische Artikel, die sie kaufen, Versand, Steuern, etc., usw. Ich weiß, dass PayPal zahlreiche Optionen für Zahlungen zur Verfügung hat, von denen einer die Webdienste ist, der andere einfach auf seine Website umleitet. Ich möchte, dass die Benutzererfahrung so nahtlos wie möglich ist, aber ich möchte mich nicht mit der Speicherung von Kreditkarten usw. beschäftigen.Spielen! Framework Zahlungsabwicklung (eCommerce/PayPal)

Wenn ich die Transaktion auf meinem Server handhabe (mit einer sicheren Verbindung/SSL), würde ich ein Risiko eingehen, die Kreditkarteninformationen sogar zur Bearbeitung an ihren Dienst zu übermitteln?

Antwort

11

Spielen! Framework v1.2 bietet eine sehr gute Unterstützung für WebServices. Es gibt kein Paypal-Modul für Play, aber der Code ist nicht zu komplex zum Schreiben. Ich implementierte die Web-Service-Lösung für unsere Jobbörse (www.express-board.fr) und es dauerte 3 Tage.

Erstens: Der Benutzer wird immer auf die Paypal-Website umgeleitet. Es gibt keine Lösung ohne Paypal-Kunden. Wenn Sie sich für die Webservice-Lösung (developer.paypal.com) entscheiden, können Sie die Paypal-Webseite jedoch mit Ihrem Logo anpassen. Nicht die beste Lösung, aber es funktioniert.

Auf der Play-Seite, hier ist ein Beispielcode für eine Kasse Methode, die die Paypal Webdienst verwendet:

public static void checkout() { 
    Double amount = Double.parseDouble(session.get("amount")); 
    Long userId = Long.parseLong(session.get("user-id")); 
    User user = User.findById(userId); 
    if (user != null) { 
     renderArgs.put("user", user); 
    } 

    // we calculate the net and gross amount with French V.A.T (19.6%) 
    BigDecimal amountBD = new BigDecimal(amount * 1.196); 
    BigDecimal netAmountBD = new BigDecimal(amount); 
    BigDecimal taxAmountBD = amountBD.subtract(netAmountBD); 

    String netAmount = netAmountBD.setScale(2, BigDecimal.ROUND_HALF_EVEN).toString(); 
    String totalAmount = amountBD.setScale(2, BigDecimal.ROUND_HALF_EVEN).toString(); 
    String taxAmount = taxAmountBD.setScale(2, BigDecimal.ROUND_HALF_EVEN).toString(); 

    String subject="Publication d'une offre d'emploi"; 


// Paypal Web service callback executed by Play! 
// I have a paypal.api.server configuration property with prod and sandbox server 

    WS.HttpResponse res = WS.url(Play.configuration.get("paypal.api.server").toString() + 
      "&USER=%s" + 
      "&PWD=%s" + 
      "&SIGNATURE=%s" + 
      "&VERSION=%s" + 
      "&METHOD=%s" + 
      "&PAYMENTREQUEST_0_PAYMENTACTION=%s" + 
      "&LANDINGPAGE=%s" + 
      "&SOLUTIONTYPE=%s" + 
      "&EMAIL=%s" + 
      "&FIRSTNAME=%s" + 
      "&LASTNAME=%s" + 
      "&STREET=%s" + 
      "&STREET2=%s" + 
      "&CITY=%s" + 
      "&ZIP=%s" + 
      "&STATE=%s" + 
      "&PAYMENTREQUEST_0_CURRENCYCODE=%s" + 
      "&MAXAMT=%s" + 
      "&PAYMENTREQUEST_0_AMT=%s" + 
      "&PAYMENTREQUEST_0_ITEMAMT=%s" + 
      "&PAYMENTREQUEST_0_TAXAMT=%s" + 
      "&PAYMENTREQUEST_0_DESC=%s" + 
      "&L_PAYMENTREQUEST_0_NAME0=%s" + 
      "&L_PAYMENTREQUEST_0_NUMER0=%s" + 
      "&L_PAYMENTREQUEST_0_DESC0=%s" + 
      "&L_PAYMENTREQUEST_0_AMT0=%s" + 
      "&L_PAYMENTREQUEST_0_QTY0=%s" + 

      "&NOSHIPPING=%s" + 
      "&LOCALECODE=%s" + 
      "&RETURNURL=%s" + 
      "&CANCELURL=%s" + 
      "&ADDROVERRIDE=%s" + 
      "&BRANDNAME=%s" 
      , 
      //------- 
      Play.configuration.get("paypal.api.username").toString(), 
      Play.configuration.get("paypal.api.password").toString(), 
      Play.configuration.get("paypal.api.signature").toString(), 
      "69.0", // API Version 
      "SetExpressCheckout", //Method 
      "Sale", 
      "Billing", 
      "Sole", 
      user.email, 
      "" + user.fullname, 
      "" + user.fullname, 
      "" + user.postalAddress, 
      "" + user.postalAddress2, 
      "" + user.city, 
      "" + user.zip, 
      "None", 
      "EUR", 
      totalAmount, //MaxAMT 
      totalAmount, //amount.toString() 
      netAmount, //PAYMENTREQUEST_n_ITEMAMT 
      taxAmount, 
      subject, 
      //--------- 
      "Item name", 
      "1", 
      subject, 
      netAmount, 
      "1", 
      //--------- 
      "1", 
      "FR", 
      Play.configuration.get("paypal.returnUrl").toString(), 
      Play.configuration.get("paypal.cancelPay").toString(), 
      "1", //ADDROVERRIDE 
      "eXpress-Board pour Innoteria" 
    ).get(); 


    String message = res.getString(); 
    PaypalResponse paypalResponse = new PaypalResponse(message); 
    if (paypalResponse.isSuccess()) { 
     Payment payment = new Payment(); 
     payment.userId = user.id; 
     payment.totalAmount = totalAmount; 
     payment.netAmount = netAmount; 
     payment.taxAmount = taxAmount; 
     payment.token = paypalResponse.getToken(); 
     payment.correlationID = paypalResponse.getCorrelationID(); 
     payment.save(); 

     redirect(Play.configuration.get("paypal.api.http").toString() + "/cgi-bin/webscr?cmd=_express-checkout&token=" + paypalResponse.getToken()); 
    } 

    Logger.error("Error on SetExpressCheckout with: "); 
    for (String tokens : message.split("&")) { 
     try { 
      Logger.error(URLDecoder.decode(tokens, "UTF-8")); 
     } catch (UnsupportedEncodingException e) { 
     } 
    } 

    error("Error from Paypal"); 

} 

Hope it

+0

Was ** Benutzer ** ist hilft ??? Ist es eine Klasse? oder etwas anderes ?? –

+0

und wie ** WS ** Abhängigkeit in Scala ** ** build.sbt ** _ ?? –

+0

Wo finde ich die Klasse ** PaypalResponse **? –