Code Monkey home page Code Monkey logo

broadleafcommerce / blc-sample-payment-gateway Goto Github PK

View Code? Open in Web Editor NEW
1.0 27.0 15.0 335 KB

Broadleaf Null Payment Gateway Integration - a sample payment gateway integration that does not integrate with an actual payment gateway. Its sole purpose is to demonstrate how the framework may integrate with a real gateway and demonstrate a sample implementation for many of the gateway interfaces.

License: Apache License 2.0

Java 100.00%

blc-sample-payment-gateway's Introduction

blc-sample-payment-gateway

Broadleaf Sample Payment Gateway Integration - a sample payment gateway integration that does not integrate with an actual payment gateway. Its sole purpose is to demonstrate how the framework may integrate with a real gateway and demonstrate a sample implementation for many of the gateway interfaces.

Sample Nonce Payment Submission

In order to set up the payment submission using a payment nonce and ajax request, you will need to include a CheckoutEndpoint like the following:

@RestController
@RequestMapping(value = "/cart/checkout",
    produces = {MediaType.APPLICATION_JSON_VALUE, MediaType.APPLICATION_XML_VALUE})
public class SamplePaymentNonceCheckoutEndpoint extends com.broadleafcommerce.rest.api.endpoint.checkout.CheckoutEndpoint {
    
    @RequestMapping(method = RequestMethod.POST, value = "/complete")
    public String performCheckout(HttpServletRequest request,
                                        @RequestParam("cartId") Long cartId,
                                        @RequestParam("payment_method_nonce") String nonce) {
        Order cart = orderService.findOrderById(cartId);

        if (cart != null) {
            try {
                //Create a new PAYMENT_NONCE Order Payment
                OrderPayment paymentNonce = orderPaymentService.create();
                paymentNonce.setType(PaymentType.CREDIT_CARD);
                paymentNonce.setPaymentGatewayType(SamplePaymentGatewayType.NULL_GATEWAY);
                paymentNonce.setAmount(cart.getTotalAfterAppliedPayments());
                paymentNonce.setOrder(cart);

                //Populate Billing Address per UI requirements
                //For this example, we'll copy the address from the temporary Credit Card's Billing address and archive the payment,
                // (since Heat Clinic's checkout template saves and validates the address in a previous section).
                OrderPayment tempPayment = null;
                for (OrderPayment payment : cart.getPayments()) {
                    if (PaymentGatewayType.TEMPORARY.equals(payment.getGatewayType()) &&
                            PaymentType.CREDIT_CARD.equals(payment.getType())) {
                        tempPayment = payment;
                        break;
                    }
                }
                if (tempPayment != null){
                    paymentNonce.setBillingAddress(addressService.copyAddress(tempPayment.getBillingAddress()));
                    orderService.removePaymentFromOrder(cart, tempPayment);
                }

                // Create the UNCONFIRMED transaction for the payment
                PaymentTransaction transaction = orderPaymentService.createTransaction();
                transaction.setAmount(cart.getTotalAfterAppliedPayments());
                transaction.setRawResponse("Sample Payment Nonce");
                transaction.setSuccess(true);
                transaction.setType(PaymentTransactionType.UNCONFIRMED);
                transaction.getAdditionalFields().put(SamplePaymentGatewayConstants.PAYMENT_METHOD_NONCE, nonce);

                transaction.setOrderPayment(paymentNonce);
                paymentNonce.addTransaction(transaction);
                orderService.addPaymentToOrder(cart, paymentNonce, null);

                cart = orderService.save(cart, true);

                return paymentGatewayCheckoutService.initiateCheckout(cart.getId());
            } catch (Exception e) {
                throw BroadleafWebServicesException.build(HttpStatus.SC_INTERNAL_SERVER_ERROR)
                        .addMessage(BroadleafWebServicesException.CHECKOUT_PROCESSING_ERROR);
            }
        }

        throw BroadleafWebServicesException.build(HttpStatus.SC_NOT_FOUND)
                .addMessage(BroadleafWebServicesException.CART_NOT_FOUND);
    }
}

Here is an example of a checkout request using the superagent javascript library:

import request from 'core/util/superagent'

const SamplePaymentService = {
    
    tokenizeCard: (paymentForm) => {
        // This is where you would tokenize the card
        const nonce = `${paymentForm.creditCardNumber}|${paymentForm.creditCardName}|${paymentForm.expirationDate}|${paymentForm.cvv}`
        return Promise.resolve(nonce)
    },

    performCheckout: (order, payment_method_nonce) => {
        return new Promise((resolve, reject) => {
            request.post('/api/cart/checkout/complete')
                .query({ cartId: order.id, payment_method_nonce })
                .end((err, response) => {
                    if (err) {
                        reject(err)
                    }

                    if (response.body) {
                        resolve(response.body)
                    }
                })
        })
    }
}

export default SamplePaymentService

blc-sample-payment-gateway's People

Contributors

broadleaf-infrastructure avatar ckittrell avatar danielcolgrove avatar elbertbautista avatar jacieck avatar jerry77oz avatar jfleschler avatar nappypirate avatar oleksiimiroshnyk avatar phillipuniverse avatar riteshadhikari17 avatar troian88 avatar

Stargazers

 avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.