2015-05-26 28 views
1

我正在研究基於Spring的Java應用程序,其中我能夠成功集成貝寶。所以,我們有一個基於課程註冊的網站,用戶正在註冊。現在我已經修改了一下付款方式,所以從前端我也會收到哪個學生試圖支付哪個課程。檢查數據庫的課程成本,我可以設置成本,並執行付款。貝寶:檢查出哪個用戶已付款?

問題是,哪位用戶付了錢,這是我還不知道的。有沒有什麼辦法,在請求中添加一個自定義Integer字段,Paypal的響應,以便我可以提取該信息並執行下一步操作,以確保學生在我的後端也被標記爲「付費」。請讓我知道...

貝寶代碼:

public Payment createPayment(HttpServletRequest req, HttpServletResponse resp, String abc) { 
     System.out.println("String abc is "+abc); 
     InputStream is = TestPayment.class 
       .getResourceAsStream("/sdk_config.properties"); 
     try { 
      PayPalResource.initConfig(is); 

     } catch (PayPalRESTException e) { 
      LOGGER.fatal(e.getMessage()); 
     } 
     Payment createdPayment = null; 
     APIContext apiContext = null; 
     String accessToken = null; 
     try { 
      accessToken = GenerateAccessToken.getAccessToken(); 
      apiContext = new APIContext(accessToken); 
     } catch (PayPalRESTException e) { 
      req.setAttribute("error", e.getMessage()); 
     } 
     if (req.getParameter("PayerID") != null) { 
      Payment payment = new Payment(); 
      if (req.getParameter("guid") != null) { 
       payment.setId(map.get(req.getParameter("guid"))); 
      } 

      PaymentExecution paymentExecution = new PaymentExecution(); 
      paymentExecution.setPayerId(req.getParameter("PayerID")); 
      try { 
       createdPayment = payment.execute(apiContext, paymentExecution); 
       //ResultPrinter.addResult(req, resp, "Executed The Payment", Payment.getLastRequest(), Payment.getLastResponse(), null); 
      } catch (PayPalRESTException e) { 
       // ResultPrinter.addResult(req, resp, "Executed The Payment", Payment.getLastRequest(), null, e.getMessage()); 
      } 
     } else { 
      Details details = new Details(); 
      details.setShipping("1"); 
      details.setSubtotal("5"); 
      details.setTax("1"); 


      Amount amount = new Amount(); 
      amount.setCurrency("EUR"); 
      // Total must be equal to sum of shipping, tax and subtotal. 
      amount.setTotal("7"); 
      amount.setDetails(details); 

      // ###Transaction 
      // A transaction defines the contract of a 
      // payment - what is the payment for and who 
      // is fulfilling it. Transaction is created with 
      // a `Payee` and `Amount` types 
      Transaction transaction = new Transaction(); 
      transaction.setAmount(amount); 
      transaction 
        .setDescription("This is the payment transaction description."); 

      // ### Items 
      Item item = new Item(); 
      item.setName("Online Market").setQuantity("1").setCurrency("EUR").setPrice("5"); 
      ItemList itemList = new ItemList(); 
      List<Item> items = new ArrayList<Item>(); 
      items.add(item); 
      itemList.setItems(items); 

      transaction.setItemList(itemList); 

      // The Payment creation API requires a list of 
      // Transaction; add the created `Transaction` 
      // to a List 
      List<Transaction> transactions = new ArrayList<Transaction>(); 
      transactions.add(transaction); 

      // ###Payer 
      // A resource representing a Payer that funds a payment 
      // Payment Method 
      // as 'paypal' 
      Payer payer = new Payer(); 
      payer.setPaymentMethod("paypal"); 

      // ###Payment 
      // A Payment Resource; create one using 
      // the above types and intent as 'sale' 
      Payment payment = new Payment(); 
      payment.setIntent("sale"); 
      payment.setPayer(payer); 
      payment.setTransactions(transactions); 

      // ###Redirect URLs 
      RedirectUrls redirectUrls = new RedirectUrls(); 
      String guid = UUID.randomUUID().toString().replaceAll("-", ""); 
      redirectUrls.setCancelUrl(req.getScheme() + "://" 
        + req.getServerName() + ":" + req.getServerPort() 
        + req.getContextPath() + "/paymentcancelled"); 
      redirectUrls.setReturnUrl(req.getScheme() + "://" 
        + req.getServerName() + ":" + req.getServerPort() 
        + req.getContextPath() + "/successpayment"); 
      payment.setRedirectUrls(redirectUrls); 

      // Create a payment by posting to the APIService 
      // using a valid AccessToken 
      // The return object contains the status; 
      try { 
       createdPayment = payment.create(apiContext); 
       /* LOGGER.info("Created payment with id = " 
         + createdPayment.getId() + " and status = " 
         + createdPayment.getState());*/ 
       // ###Payment Approval Url 
       Iterator<Links> links = createdPayment.getLinks().iterator(); 
       while (links.hasNext()) { 
        Links link = links.next(); 
        if (link.getRel().equalsIgnoreCase("approval_url")) { 
         req.setAttribute("redirectURL", link.getHref()); 
        } 
       } 
      // ResultPrinter.addResult(req, resp, "Payment with PayPal", Payment.getLastRequest(), Payment.getLastResponse(), null); 
       map.put(guid, createdPayment.getId()); 
      } catch (PayPalRESTException e) { 
       // ResultPrinter.addResult(req, resp, "Payment with PayPal", Payment.getLastRequest(), null, e.getMessage()); 
      } 
     } 
     return createdPayment; 
    } 

請讓我知道。非常感謝。

+0

@kryger:這怎麼是一個很寬泛的問題,能否請您解釋一下,我只是想在這個過程中,地方設置ID這在整個交易過程中停留,並且在付款成功或失敗時返回。那有什麼廣泛的? –

回答