package com.angkorteam.ecommerce.controller;

import com.angkorteam.ecommerce.mobile.delivery.Payment;
import com.angkorteam.ecommerce.model.EcommercePayment;
import com.angkorteam.framework.spring.JdbcTemplate;
import com.angkorteam.framework.spring.NamedParameterJdbcTemplate;
import com.angkorteam.platform.Platform;
import com.braintreegateway.BraintreeGateway;
import com.google.common.base.Strings;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;

/**
 * Created by socheatkhauv on 9/2/17.
 */
@Controller
@RequestMapping("/payment")
public class PaymentController {

    @RequestMapping(path = "/paypal/initiate/{id}", method = RequestMethod.POST)
    public ResponseEntity<?> paypalInitiate(HttpServletRequest request, Long paymentId) throws ServletException {
        JdbcTemplate jdbcTemplate = Platform.getBean(JdbcTemplate.class);
        NamedParameterJdbcTemplate named = Platform.getBean(NamedParameterJdbcTemplate.class);
        EcommercePayment ecommercePayment = jdbcTemplate.queryForObject("select * from ecommerce_payment where ecommerce_payment_id = ?", EcommercePayment.class, paymentId);
        if (ecommercePayment == null || !Payment.TYPE_PAYPAL.equals(ecommercePayment.getType())) {
            throw new ServletException("payment is not available");
        }
        if (Strings.isNullOrEmpty(ecommercePayment.getServerParam1())) {
            throw new ServletException("payment is not available");
        }
        BraintreeGateway gateway = new BraintreeGateway(ecommercePayment.getServerParam1());
        String token = gateway.clientToken().generate();
        return ResponseEntity.ok(token);
    }
}

@@ -398,16 +398,73 @@
 trings;%0A
+import org.slf4j.Logger;%0Aimport org.slf4j.LoggerFactory;%0A
 import o
@@ -503,16 +503,16 @@
 Entity;%0A
-
 import o
@@ -885,16 +885,108 @@
 ller %7B%0A%0A
+    private static final Logger LOGGER = LoggerFactory.getLogger(PaymentController.class);%0A%0A
     @Req
@@ -1172,16 +1172,65 @@
 ption %7B%0A
+        LOGGER.info(%22%7B%7D%22, getClass().getName());%0A
         
@@ -1564,16 +1564,116 @@
 entId);%0A
+        LOGGER.info(%22payment type %7B%7D%22, ecommercePayment == null ? %22%22 : ecommercePayment.getType());%0A
         
@@ -1833,32 +1833,117 @@
 le%22);%0A        %7D%0A
+        LOGGER.info(%22payment server param1 %7B%7D%22, ecommercePayment.getServerParam1());%0A
         if (Stri
@@ -2069,32 +2069,50 @@
 le%22);%0A        %7D%0A
+        try %7B%0A    
         Braintre
@@ -2196,16 +2196,20 @@
         
+    
 String t
@@ -2257,16 +2257,20 @@
         
+  
+  
 return R
@@ -2294,16 +2294,112 @@
 token);%0A
+        %7D catch (Throwable e) %7B%0A            e.printStackTrace();%0A            throw e;%0A        %7D%0A
     %7D%0A%7D%0A

add log