|
| 1 | +/** |
| 2 | + * Copyright (c) 2017 by CyberSource |
| 3 | + * Governing licence: https://github.com/CyberSource/cybersource-flex-samples/blob/master/LICENSE.md |
| 4 | + */ |
| 5 | +package com.cybersource.flex.application; |
| 6 | + |
| 7 | +import com.cybersource.flex.sdk.FlexService; |
| 8 | +import com.cybersource.flex.sdk.exception.FlexException; |
| 9 | +import com.cybersource.flex.sdk.model.FlexPublicKey; |
| 10 | +import java.util.Map; |
| 11 | +import javax.servlet.http.HttpSession; |
| 12 | +import org.springframework.beans.factory.annotation.Autowired; |
| 13 | +import org.springframework.stereotype.Controller; |
| 14 | +import org.springframework.ui.Model; |
| 15 | +import org.springframework.web.bind.annotation.RequestMapping; |
| 16 | +import org.springframework.web.bind.annotation.RequestMethod; |
| 17 | +import org.springframework.web.bind.annotation.RequestParam; |
| 18 | + |
| 19 | +@Controller |
| 20 | +public class CheckoutController { |
| 21 | + |
| 22 | + @Autowired |
| 23 | + private FlexService flexService; |
| 24 | + |
| 25 | + @RequestMapping("/") |
| 26 | + String redirect() { |
| 27 | + return "redirect:checkout"; |
| 28 | + } |
| 29 | + |
| 30 | + @RequestMapping("/checkout") |
| 31 | + String checkout(final HttpSession session, final Model model) throws FlexException { |
| 32 | + // retrieve one time use public RSA key from Flex to facilitate PAN encryption |
| 33 | + final FlexPublicKey key = flexService.createKey(); |
| 34 | + session.setAttribute("flexPublicKey", key); |
| 35 | + |
| 36 | + // Add JSON Web Keystore to the view model and return rendered "checkout" page |
| 37 | + model.addAttribute("jwk", key.getJwk()); |
| 38 | + return "checkout"; |
| 39 | + } |
| 40 | + |
| 41 | + @RequestMapping(value = "/receipt", method = RequestMethod.POST) |
| 42 | + String receipt(@RequestParam final Map<String, Object> postParams, final HttpSession session, final Model model) throws FlexException { |
| 43 | + // Read in the public key to be used and remove it from the session |
| 44 | + final FlexPublicKey key = (FlexPublicKey) session.getAttribute("flexPublicKey"); |
| 45 | + session.removeAttribute("flexPublicKey"); // no longer needed |
| 46 | + |
| 47 | + // verify the token signiture using SDK |
| 48 | + postParams.put("verifyResult", flexService.verify(key, postParams)); |
| 49 | + |
| 50 | + /** |
| 51 | + * |
| 52 | + * The payment may now be completed using the received & validated |
| 53 | + * token. |
| 54 | + * |
| 55 | + * For demonstration purposes, all post parameters are added to the view |
| 56 | + * model to display data received from cardholder's browser. |
| 57 | + */ |
| 58 | + model.addAttribute("postParams", postParams); |
| 59 | + return "receipt"; |
| 60 | + } |
| 61 | + |
| 62 | +} |
0 commit comments