Paypal:检查哪个用户已付款?

时间:2015-05-26 12:24:45

标签: java paypal paypal-sandbox

我正在开发基于Spring的Java应用程序,在该应用程序中我能够成功集成paypal。因此,我们有一个基于课程注册的网站,用户正在注册。现在我已经修改了支付方式,所以从前端我也会收到哪个学生正在尝试支付哪个课程。检查数据库中的课程费用,我可以设置费用,然后执行付款。

问题是,用户支付了哪些,这是我还不知道的。有没有办法,在请求中添加自定义Integer字段,Paypal的响应,这样我就可以提取该信息并执行后续步骤以确保学生在我的后端也被标记为'PAID'。请告诉我......

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;
    } 

请告诉我。非常感谢。

1 个答案:

答案 0 :(得分:0)