From 6cbaf0c2fc074ee280ca2b1dbe3ca32c64e8d79b Mon Sep 17 00:00:00 2001 From: Kwok He Chu <105217051+Kwok-he-Chu@users.noreply.github.com> Date: Thu, 28 Sep 2023 13:26:26 +0200 Subject: [PATCH] Update Giving sample --- giving-example/README.md | 2 + ...pplication.java => GivingApplication.java} | 6 +-- .../adyen/giving/api/CheckoutResource.java | 34 ++++++++++++++-- .../src/main/resources/static/adyenGiving.js | 40 ++++++++----------- .../resources/static/adyenImplementation.js | 19 +-------- .../main/resources/static/css/application.css | 1 + .../src/main/resources/templates/index.html | 7 ---- .../src/main/resources/templates/preview.html | 2 +- 8 files changed, 54 insertions(+), 57 deletions(-) rename giving-example/src/main/java/com/adyen/giving/{OnlinePaymentsApplication.java => GivingApplication.java} (78%) diff --git a/giving-example/README.md b/giving-example/README.md index f7b059c..d2ea9fa 100644 --- a/giving-example/README.md +++ b/giving-example/README.md @@ -18,6 +18,8 @@ First make a test payment using one of our test card numbers, so you can see the ![Giving demo](src/main/resources/static/images/donations.gif) +> **Note**: You need to have donations [enabled in your Customer Area](https://docs.adyen.com/online-payments/donations/testing/#step-1-enable-the-donation-token-in-your-api-response) in order to receive the `DonationToken`. + ## Supported Integrations The amount of payment methods supported for donations is limited. In this demo, you will find a use case centered around the use of cards. diff --git a/giving-example/src/main/java/com/adyen/giving/OnlinePaymentsApplication.java b/giving-example/src/main/java/com/adyen/giving/GivingApplication.java similarity index 78% rename from giving-example/src/main/java/com/adyen/giving/OnlinePaymentsApplication.java rename to giving-example/src/main/java/com/adyen/giving/GivingApplication.java index 7998001..0ae0830 100644 --- a/giving-example/src/main/java/com/adyen/giving/OnlinePaymentsApplication.java +++ b/giving-example/src/main/java/com/adyen/giving/GivingApplication.java @@ -8,14 +8,14 @@ import org.springframework.boot.autoconfigure.SpringBootApplication; @SpringBootApplication -public class OnlinePaymentsApplication { - private static final Logger log = LoggerFactory.getLogger(OnlinePaymentsApplication.class); +public class GivingApplication { + private static final Logger log = LoggerFactory.getLogger(GivingApplication.class); @Autowired private ApplicationProperty applicationProperty; public static void main(String[] args) { - SpringApplication.run(OnlinePaymentsApplication.class, args); + SpringApplication.run(GivingApplication.class, args); } @PostConstruct diff --git a/giving-example/src/main/java/com/adyen/giving/api/CheckoutResource.java b/giving-example/src/main/java/com/adyen/giving/api/CheckoutResource.java index 4188fdd..e82a482 100644 --- a/giving-example/src/main/java/com/adyen/giving/api/CheckoutResource.java +++ b/giving-example/src/main/java/com/adyen/giving/api/CheckoutResource.java @@ -7,6 +7,7 @@ import com.adyen.service.checkout.PaymentsApi; import com.adyen.service.exception.ApiException; import jakarta.servlet.http.HttpServletRequest; +import jakarta.servlet.http.HttpSession; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.http.ResponseEntity; @@ -32,6 +33,10 @@ public class CheckoutResource { private final PaymentsApi paymentsApi; + private final String donationToken = "DonationToken"; + + private final String paymentOriginalPspReference = "PaymentOriginalPspReference"; + public CheckoutResource(ApplicationProperty applicationProperty) { this.applicationProperty = applicationProperty; @@ -54,15 +59,26 @@ public CheckoutResource(ApplicationProperty applicationProperty) { * @throws ApiException from Adyen API. */ @PostMapping("/donations") - public ResponseEntity donations(@RequestParam String donationToken, @RequestParam String pspReference, @RequestBody Amount body, @RequestHeader String host, HttpServletRequest request) throws IOException, ApiException { - var decodedDonationToken = URLDecoder.decode(donationToken.replace("+", "%2B"), StandardCharsets.UTF_8).replace("%2B", "+"); - + public ResponseEntity donations(@RequestBody Amount body, @RequestHeader String host, HttpServletRequest request) throws IOException, ApiException { DonationPaymentRequest donationRequest = new DonationPaymentRequest(); + HttpSession session = request.getSession(); + String pspReference = session.getAttribute(paymentOriginalPspReference).toString(); + String token = session.getAttribute(donationToken).toString(); + + if (pspReference == null) { + log.info("Could not find the PspReference in the stored session."); + return ResponseEntity.badRequest().build(); + } + + if (token == null) { + log.info("Could not find the DonationToken in the stored session."); + return ResponseEntity.badRequest().build(); + } donationRequest.amount(body); donationRequest.reference(UUID.randomUUID().toString()); donationRequest.setPaymentMethod(new CheckoutPaymentMethod(new CardDetails())); - donationRequest.setDonationToken(decodedDonationToken); + donationRequest.setDonationToken(token); donationRequest.donationOriginalPspReference(pspReference); donationRequest.setDonationAccount(this.applicationProperty.getDonationMerchantAccount()); donationRequest.returnUrl(request.getScheme() + "://" + host); @@ -133,6 +149,16 @@ public ResponseEntity payments(@RequestHeader String host, @Req log.info("REST request to make Adyen payment {}", paymentRequest); var response = paymentsApi.payments(paymentRequest); + + var session = request.getSession(); + session.setAttribute(paymentOriginalPspReference, response.getPspReference()); + + if (response.getDonationToken() == null) { + log.error("The payments endpoint did not return a donationToken, please enable this in your Customer Area. See README."); + } + else { + session.setAttribute(donationToken, response.getDonationToken()); + } return ResponseEntity.ok() .body(response); } diff --git a/giving-example/src/main/resources/static/adyenGiving.js b/giving-example/src/main/resources/static/adyenGiving.js index cf0d2f8..b06fbc2 100644 --- a/giving-example/src/main/resources/static/adyenGiving.js +++ b/giving-example/src/main/resources/static/adyenGiving.js @@ -12,9 +12,10 @@ async function callServer(url, data) { return await res.json(); } -async function handleDonation(donationToken, pspReference, amount) { +async function handleDonation(amount) { try { - const res = await callServer(`/api/donations?donationToken=${encodeURIComponent(donationToken)}&pspReference=${pspReference}`, amount); + console.log(amount); + const res = await callServer(`/api/donations`, amount); switch (res.status) { case "completed": @@ -28,18 +29,13 @@ async function handleDonation(donationToken, pspReference, amount) { console.error(error); alert("Error occurred. Look at console for details"); } - - } async function startGiving() { - - const checkout= await AdyenCheckout( - { - clientKey, - environment: "test", - } - ); + const checkout= await AdyenCheckout({ + clientKey, + environment: "test", + }); const donationConfig = { amounts: { @@ -60,25 +56,21 @@ async function startGiving() { onDonate: (state, component) => { if(state.isValid) { console.log("Initiating donation"); - let donationToken = sessionStorage.getItem("donationToken"); - let pspReference = sessionStorage.getItem("pspReference"); - - if(!donationToken || !pspReference) { - console.log("No token or pspReference found, can't donate"); - } - else{ - handleDonation(donationToken, pspReference, state.data.amount); - } + handleDonation(state.data.amount); } - }, onCancel: (result, component) => { console.log("Donation cancelled"); - document.getElementById( 'donation-container' ).style.display = 'none'; + console.log(result); + document.getElementById('donation-container').style.display = 'none'; } }; - checkout.create('donation', donationConfig).mount('#donation-container'); + try { + checkout.create('donation', donationConfig).mount('#donation-container'); + } catch (ex) { + console.warn(ex); + } } -startGiving(); +startGiving(); \ No newline at end of file diff --git a/giving-example/src/main/resources/static/adyenImplementation.js b/giving-example/src/main/resources/static/adyenImplementation.js index a179e59..33d8bb7 100644 --- a/giving-example/src/main/resources/static/adyenImplementation.js +++ b/giving-example/src/main/resources/static/adyenImplementation.js @@ -22,17 +22,6 @@ async function initCheckout() { value: 10000, currency: "EUR", }, - }, - paypal: { - amount: { - value: 10000, - currency: "USD", - }, - environment: "test", // Change this to "live" when you're ready to accept live PayPal payments - countryCode: "US", // Only needed for test. This will be automatically retrieved when you are in production. - onCancel: (data, component) => { - component.setStatus('ready'); - }, } }, onSubmit: (state, component) => { @@ -58,12 +47,6 @@ async function initCheckout() { async function handleSubmission(state, component, url) { try { const res = await callServer(url, state.data); - if(res.donationToken){ - // Depending on how you are handling the donation, you may want to store the token and pspReference, reuse the checkout instance, or store the data in the backend session. - console.log("Caching donationToken and pspReference"); - sessionStorage.setItem("donationToken", res.donationToken); - sessionStorage.setItem("pspReference", res.pspReference); - } handleServerResponse(res, component); } catch (error) { console.error(error); @@ -107,4 +90,4 @@ function handleServerResponse(res, component) { } } -initCheckout(); +initCheckout(); \ No newline at end of file diff --git a/giving-example/src/main/resources/static/css/application.css b/giving-example/src/main/resources/static/css/application.css index 398ce90..a82d945 100644 --- a/giving-example/src/main/resources/static/css/application.css +++ b/giving-example/src/main/resources/static/css/application.css @@ -16,6 +16,7 @@ body { a, u { text-decoration: none; + color: #0abf53 } a:hover { diff --git a/giving-example/src/main/resources/templates/index.html b/giving-example/src/main/resources/templates/index.html index c089770..7cfe4cd 100644 --- a/giving-example/src/main/resources/templates/index.html +++ b/giving-example/src/main/resources/templates/index.html @@ -30,13 +30,6 @@

Adyen Giving - Donations demo

- - - - - - - diff --git a/giving-example/src/main/resources/templates/preview.html b/giving-example/src/main/resources/templates/preview.html index c2c11cd..41a09e7 100644 --- a/giving-example/src/main/resources/templates/preview.html +++ b/giving-example/src/main/resources/templates/preview.html @@ -24,7 +24,7 @@

Cart