-
Notifications
You must be signed in to change notification settings - Fork 31
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #325 from Adyen/feature/AD-50
Feature/ad 50
- Loading branch information
Showing
16 changed files
with
878 additions
and
19 deletions.
There are no files selected for viewing
68 changes: 68 additions & 0 deletions
68
...don/web/src/com/adyen/v6/controllers/checkout/AdyenApplePayExpressCheckoutController.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
package com.adyen.v6.controllers.checkout; | ||
|
||
import com.adyen.model.checkout.PaymentsResponse; | ||
import com.adyen.v6.facades.AdyenExpressCheckoutFacade; | ||
import com.adyen.v6.request.ApplePayExpressCartRequest; | ||
import com.adyen.v6.request.ApplePayExpressPDPRequest; | ||
import de.hybris.platform.acceleratorstorefrontcommons.constants.WebConstants; | ||
import de.hybris.platform.acceleratorstorefrontcommons.security.GUIDCookieStrategy; | ||
import de.hybris.platform.servicelayer.session.SessionService; | ||
import org.apache.log4j.Logger; | ||
import org.springframework.beans.factory.annotation.Autowired; | ||
import org.springframework.http.HttpStatus; | ||
import org.springframework.http.ResponseEntity; | ||
import org.springframework.stereotype.Controller; | ||
import org.springframework.web.bind.annotation.ExceptionHandler; | ||
import org.springframework.web.bind.annotation.PostMapping; | ||
import org.springframework.web.bind.annotation.RequestBody; | ||
import org.springframework.web.bind.annotation.ResponseStatus; | ||
|
||
import javax.servlet.http.HttpServletRequest; | ||
import javax.servlet.http.HttpServletResponse; | ||
|
||
@Controller | ||
public class AdyenApplePayExpressCheckoutController { | ||
private static final Logger LOG = Logger.getLogger(AdyenApplePayExpressCheckoutController.class); | ||
|
||
@Autowired | ||
private AdyenExpressCheckoutFacade adyenExpressCheckoutFacade; | ||
|
||
@Autowired | ||
private SessionService sessionService; | ||
|
||
@Autowired | ||
private GUIDCookieStrategy guidCookieStrategy; | ||
|
||
@PostMapping("/expressCheckout/applePayPDP") | ||
public ResponseEntity applePayExpressPDP(final HttpServletRequest request, final HttpServletResponse response, @RequestBody ApplePayExpressPDPRequest applePayExpressPDPRequest) throws Exception { | ||
|
||
PaymentsResponse paymentsResponse = adyenExpressCheckoutFacade.expressPDPCheckout(applePayExpressPDPRequest.getAddressData(), applePayExpressPDPRequest.getProductCode(), | ||
applePayExpressPDPRequest.getAdyenApplePayMerchantIdentifier(), applePayExpressPDPRequest.getAdyenApplePayMerchantName(), | ||
applePayExpressPDPRequest.getApplePayToken(), request); | ||
|
||
guidCookieStrategy.setCookie(request, response); | ||
sessionService.setAttribute(WebConstants.ANONYMOUS_CHECKOUT, Boolean.TRUE); | ||
|
||
return new ResponseEntity<>(paymentsResponse, HttpStatus.OK); | ||
} | ||
|
||
@PostMapping("/expressCheckout/cart") | ||
public ResponseEntity cartExpressCheckout(final HttpServletRequest request, final HttpServletResponse response, @RequestBody ApplePayExpressCartRequest applePayExpressCartRequest) throws Exception { | ||
|
||
PaymentsResponse paymentsResponse = adyenExpressCheckoutFacade.expressCartCheckout(applePayExpressCartRequest.getAddressData(), | ||
applePayExpressCartRequest.getAdyenApplePayMerchantIdentifier(), applePayExpressCartRequest.getAdyenApplePayMerchantName(), | ||
applePayExpressCartRequest.getApplePayToken(), request); | ||
|
||
guidCookieStrategy.setCookie(request, response); | ||
sessionService.setAttribute(WebConstants.ANONYMOUS_CHECKOUT, Boolean.TRUE); | ||
|
||
return new ResponseEntity<>(paymentsResponse, HttpStatus.OK); | ||
} | ||
|
||
@ResponseStatus(value = HttpStatus.BAD_REQUEST) | ||
@ExceptionHandler(value = Exception.class) | ||
public String adyenComponentExceptionHandler(Exception e) { | ||
LOG.error("Exception during ApplePayExpress processing", e); | ||
return "Exception during ApplePayExpress processing"; | ||
} | ||
} |
44 changes: 44 additions & 0 deletions
44
...eckoutaddon/acceleratoraddon/web/src/com/adyen/v6/request/ApplePayExpressCartRequest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
package com.adyen.v6.request; | ||
|
||
import de.hybris.platform.commercefacades.user.data.AddressData; | ||
|
||
import java.io.Serializable; | ||
|
||
public class ApplePayExpressCartRequest implements Serializable { | ||
private AddressData addressData; | ||
private String adyenApplePayMerchantName; | ||
private String adyenApplePayMerchantIdentifier; | ||
private String applePayToken; | ||
|
||
public AddressData getAddressData() { | ||
return addressData; | ||
} | ||
|
||
public void setAddressData(AddressData addressData) { | ||
this.addressData = addressData; | ||
} | ||
|
||
public String getAdyenApplePayMerchantName() { | ||
return adyenApplePayMerchantName; | ||
} | ||
|
||
public void setAdyenApplePayMerchantName(String adyenApplePayMerchantName) { | ||
this.adyenApplePayMerchantName = adyenApplePayMerchantName; | ||
} | ||
|
||
public String getAdyenApplePayMerchantIdentifier() { | ||
return adyenApplePayMerchantIdentifier; | ||
} | ||
|
||
public void setAdyenApplePayMerchantIdentifier(String adyenApplePayMerchantIdentifier) { | ||
this.adyenApplePayMerchantIdentifier = adyenApplePayMerchantIdentifier; | ||
} | ||
|
||
public String getApplePayToken() { | ||
return applePayToken; | ||
} | ||
|
||
public void setApplePayToken(String applePayToken) { | ||
this.applePayToken = applePayToken; | ||
} | ||
} |
53 changes: 53 additions & 0 deletions
53
...heckoutaddon/acceleratoraddon/web/src/com/adyen/v6/request/ApplePayExpressPDPRequest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
package com.adyen.v6.request; | ||
|
||
import de.hybris.platform.commercefacades.user.data.AddressData; | ||
|
||
import java.io.Serializable; | ||
|
||
public class ApplePayExpressPDPRequest implements Serializable { | ||
private String productCode; | ||
private AddressData addressData; | ||
private String adyenApplePayMerchantName; | ||
private String adyenApplePayMerchantIdentifier; | ||
private String applePayToken; | ||
|
||
public String getProductCode() { | ||
return productCode; | ||
} | ||
|
||
public void setProductCode(String productCode) { | ||
this.productCode = productCode; | ||
} | ||
|
||
public AddressData getAddressData() { | ||
return addressData; | ||
} | ||
|
||
public void setAddressData(AddressData addressData) { | ||
this.addressData = addressData; | ||
} | ||
|
||
public String getAdyenApplePayMerchantName() { | ||
return adyenApplePayMerchantName; | ||
} | ||
|
||
public void setAdyenApplePayMerchantName(String adyenApplePayMerchantName) { | ||
this.adyenApplePayMerchantName = adyenApplePayMerchantName; | ||
} | ||
|
||
public String getAdyenApplePayMerchantIdentifier() { | ||
return adyenApplePayMerchantIdentifier; | ||
} | ||
|
||
public void setAdyenApplePayMerchantIdentifier(String adyenApplePayMerchantIdentifier) { | ||
this.adyenApplePayMerchantIdentifier = adyenApplePayMerchantIdentifier; | ||
} | ||
|
||
public String getApplePayToken() { | ||
return applePayToken; | ||
} | ||
|
||
public void setApplePayToken(String applePayToken) { | ||
this.applePayToken = applePayToken; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
206 changes: 206 additions & 0 deletions
206
...koutaddon/acceleratoraddon/web/webroot/_ui/responsive/common/js/adyen_express_checkout.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,206 @@ | ||
var AdyenExpressCheckoutHybris = (function() { | ||
'use strict'; | ||
|
||
var ErrorMessages = { | ||
PaymentCancelled: 'checkout.error.authorization.payment.cancelled', | ||
PaymentError: 'checkout.error.authorization.payment.error', | ||
PaymentNotAvailable: 'checkout.summary.component.notavailable', | ||
TermsNotAccepted: 'checkout.error.terms.not.accepted' | ||
}; | ||
|
||
return { | ||
|
||
adyenConfig: { | ||
merchantName: null, | ||
merchantId: null, | ||
pageType: null, | ||
productCode: null | ||
}, | ||
|
||
initiateCheckout: async function(initConfig) { | ||
const configuration = { | ||
...initConfig, | ||
analytics: { | ||
enabled: false // Set to false to not send analytics data to Adyen. | ||
}, | ||
risk: { | ||
enabled: false | ||
}, | ||
onError: (error, component) => { | ||
console.error("Checkout error occured"); | ||
}, | ||
}; | ||
|
||
return await AdyenCheckout(configuration); | ||
}, | ||
initiateApplePayExpress: async function(params, config) { | ||
var checkoutPromise = this.initiateCheckout(config); | ||
const { | ||
amount, | ||
countryCode, | ||
applePayMerchantIdentifier, | ||
applePayMerchantName, | ||
pageType, | ||
productCode | ||
} = params; | ||
|
||
const applePayNode = document.getElementById('adyen-component-button-container'); | ||
|
||
this.adyenConfig.merchantName = applePayMerchantName; | ||
this.adyenConfig.merchantId = applePayMerchantIdentifier; | ||
this.adyenConfig.pageType = pageType; | ||
this.adyenConfig.productCode = productCode; | ||
|
||
const applePayConfiguration = { | ||
//onValidateMerchant is required if you're using your own Apple Pay certificate | ||
onValidateMerchant: (resolve, reject, validationURL) => { | ||
resolve(); | ||
} | ||
}; | ||
checkoutPromise.then((checkout) => { | ||
var applePayComponent = checkout.create("applepay", { | ||
amount: { | ||
currency: amount.currency, | ||
value: amount.value | ||
}, | ||
configuration: { | ||
merchantName: applePayMerchantName, | ||
merchantId: applePayMerchantIdentifier | ||
}, | ||
// Button config | ||
buttonType: "check-out", | ||
buttonColor: "black", | ||
requiredShippingContactFields: [ | ||
"postalAddress", | ||
"name", | ||
"email" | ||
], | ||
//might be used to recalculate cart with shipping method | ||
// onShippingContactSelected: function(resolve, reject, event){ | ||
// | ||
// var shippingMethodUpdate = { | ||
// newTotal: { | ||
// amount: amount.value | ||
// } | ||
// } | ||
// resolve(shippingMethodUpdate); | ||
// }, | ||
onClick: function(resolve, reject) { | ||
resolve(); | ||
}, | ||
onSubmit: function(state, component) { | ||
// empty to block session flow, submit logic done in onAuthorized | ||
}, | ||
onAuthorized: (resolve, reject, event) => { | ||
var data = this.prepareData(event); | ||
this.makePayment(data, resolve, reject); | ||
} | ||
}); | ||
applePayComponent.isAvailable() | ||
.then(function() { | ||
applePayComponent.mount(applePayNode); | ||
}) | ||
.catch(function(e) { | ||
// Apple Pay is not available | ||
console.log('Something went wrong trying to mount the Apple Pay component'); | ||
}); | ||
}) | ||
}, | ||
makePayment: function(data, resolve, reject) { | ||
$.ajax({ | ||
url: this.getUrl(), | ||
type: "POST", | ||
data: JSON.stringify(data), | ||
contentType: "application/json; charset=utf-8", | ||
success: function(response) { | ||
try { | ||
if (response.resultCode && (response.resultCode === 'Authorised' || response.resultCode === 'RedirectShopper')) { | ||
resolve(); | ||
AdyenExpressCheckoutHybris.handleResult(response, false); | ||
} else { | ||
reject(); | ||
AdyenExpressCheckoutHybris.handleResult(ErrorMessages.PaymentError, true); | ||
} | ||
} catch (e) { | ||
reject(); | ||
AdyenExpressCheckoutHybris.handleResult(ErrorMessages.PaymentError, true); | ||
} | ||
}, | ||
error: function(xmlHttpResponse, exception) { | ||
reject(); | ||
var responseMessage = xmlHttpResponse.responseJSON; | ||
if (xmlHttpResponse.status === 400) { | ||
AdyenExpressCheckoutHybris.handleResult(responseMessage, true); | ||
} else { | ||
console.log('Error on makePayment'); | ||
AdyenExpressCheckoutHybris.handleResult(ErrorMessages.PaymentError, true); | ||
} | ||
} | ||
}) | ||
}, | ||
handleResult: function(data, error) { | ||
if (error) { | ||
document.querySelector("#resultData").value = data; | ||
document.querySelector("#isResultError").value = error; | ||
} else { | ||
document.querySelector("#resultData").value = JSON.stringify(data); | ||
} | ||
document.querySelector("#handleComponentResultForm").submit(); | ||
}, | ||
prepareData: function(event) { | ||
if (this.adyenConfig.pageType === 'PDP') { | ||
return { | ||
productCode: this.adyenConfig.productCode, | ||
adyenApplePayMerchantName: this.adyenConfig.merchantName, | ||
adyenApplePayMerchantIdentifier: this.adyenConfig.merchantId, | ||
applePayToken: btoa(JSON.stringify(event.payment.token.paymentData)), | ||
addressData: { | ||
email: event.payment.shippingContact.emailAddress, | ||
firstName: event.payment.shippingContact.givenName, | ||
lastName: event.payment.shippingContact.familyName, | ||
line1: event.payment.shippingContact.addressLines[0], | ||
line2: event.payment.shippingContact.addressLines[1], | ||
postalCode: event.payment.shippingContact.postalCode, | ||
town: event.payment.shippingContact.locality, | ||
country: { | ||
isocode: event.payment.shippingContact.countryCode, | ||
name: event.payment.shippingContact.country | ||
} | ||
} | ||
} | ||
} | ||
if (this.adyenConfig.pageType === 'cart') { | ||
return { | ||
adyenApplePayMerchantName: this.adyenConfig.merchantName, | ||
adyenApplePayMerchantIdentifier: this.adyenConfig.merchantId, | ||
applePayToken: btoa(JSON.stringify(event.payment.token.paymentData)), | ||
addressData: { | ||
email: event.payment.shippingContact.emailAddress, | ||
firstName: event.payment.shippingContact.givenName, | ||
lastName: event.payment.shippingContact.familyName, | ||
line1: event.payment.shippingContact.addressLines[0], | ||
line2: event.payment.shippingContact.addressLines[1], | ||
postalCode: event.payment.shippingContact.postalCode, | ||
town: event.payment.shippingContact.locality, | ||
country: { | ||
isocode: event.payment.shippingContact.countryCode, | ||
name: event.payment.shippingContact.country | ||
} | ||
} | ||
} | ||
} | ||
console.error('unknown page type') | ||
return {}; | ||
}, | ||
getUrl: function() { | ||
if (this.adyenConfig.pageType === 'PDP') { | ||
return ACC.config.encodedContextPath + '/expressCheckout/applePayPDP' | ||
} | ||
if (this.adyenConfig.pageType === 'cart') { | ||
return ACC.config.encodedContextPath + '/expressCheckout/cart' | ||
} | ||
console.error('unknown page type') | ||
return null; | ||
} | ||
} | ||
})(); |
Oops, something went wrong.