Skip to content

Instantly share code, notes, and snippets.

@innerverse
Created September 21, 2011 21:49
Show Gist options
  • Save innerverse/1233423 to your computer and use it in GitHub Desktop.
Save innerverse/1233423 to your computer and use it in GitHub Desktop.
Braintree / jBilling plugin
package net.sococo.billingplugins;
import com.braintreegateway.BraintreeGateway;
import com.braintreegateway.Environment;
import com.braintreegateway.Transaction;
import com.braintreegateway.TransactionRequest;
import com.sapienter.jbilling.server.payment.PaymentDTOEx;
import com.sapienter.jbilling.server.payment.db.PaymentAuthorizationDTO;
import com.sapienter.jbilling.server.payment.db.PaymentResultDAS;
import com.sapienter.jbilling.server.pluggableTask.PaymentTask;
import com.sapienter.jbilling.server.pluggableTask.PaymentTaskWithTimeout;
import com.sapienter.jbilling.server.pluggableTask.admin.ParameterDescription;
import com.sapienter.jbilling.server.pluggableTask.admin.PluggableTaskDTO;
import com.sapienter.jbilling.server.pluggableTask.admin.PluggableTaskException;
import com.sapienter.jbilling.server.util.Constants;
import org.apache.log4j.Logger;
import java.math.BigDecimal;
/**
* Created by IntelliJ IDEA.
* User: Seth Miller
* Date: Aug 28, 2010
* Time: 9:45:20 AM
*
* Requires CC to already be in Braintree's Vault, charges using CC token
*/
public class PaymentBraintreeTask extends PaymentTaskWithTimeout
implements PaymentTask {
private static final Logger LOG = Logger.getLogger(PaymentBraintreeTask.class);
private String merchantId;
private String publicKey;
private String privateKey;
private Boolean isTest;
public static final ParameterDescription PARAMETER_MERCHANT_ID =
new ParameterDescription("merchant_id", true, ParameterDescription.Type.STR);
public static final ParameterDescription PARAMETER_PUBLIC_KEY =
new ParameterDescription("public_key", true, ParameterDescription.Type.STR);
public static final ParameterDescription PARAMETER_PRIVATE_KEY =
new ParameterDescription("private_key", true, ParameterDescription.Type.STR);
public static final ParameterDescription PARAMETER_TEST =
new ParameterDescription("test", false, ParameterDescription.Type.STR);
//initializer for pluggable params
{
descriptions.add(PARAMETER_MERCHANT_ID);
descriptions.add(PARAMETER_PUBLIC_KEY);
descriptions.add(PARAMETER_PRIVATE_KEY);
descriptions.add(PARAMETER_TEST);
}
@Override
public void initializeParamters(PluggableTaskDTO task) throws PluggableTaskException {
super.initializeParamters(task);
merchantId = ensureGetParameter(PARAMETER_MERCHANT_ID.getName());
publicKey = ensureGetParameter(PARAMETER_PUBLIC_KEY.getName());
privateKey = ensureGetParameter(PARAMETER_PRIVATE_KEY.getName());
isTest = getOptionalParameter(PARAMETER_TEST.getName(),"false").equals("true");
}
public boolean process(PaymentDTOEx paymentInfo) throws PluggableTaskException {
try {
if (BigDecimal.ZERO.compareTo(paymentInfo.getAmount()) > 0) {
LOG.debug("Braintree plugin: Negative amounts not allowed");
return false;
}
BraintreeGateway gateway = new BraintreeGateway(
isTest ? Environment.SANDBOX : Environment.PRODUCTION, merchantId, publicKey, privateKey);
TransactionRequest request = new TransactionRequest()
.amount(paymentInfo.getAmount().setScale(2, BigDecimal.ROUND_HALF_UP))
.paymentMethodToken(paymentInfo.getCreditCard().getGatewayKey())
.options()
.submitForSettlement(true)
.done();
LOG.info("Amount: " + paymentInfo.getAmount().setScale(2, BigDecimal.ROUND_HALF_UP) + " CC Token: " + paymentInfo.getCreditCard().getGatewayKey());
LOG.info("hitting: " + gateway.getConfiguration().baseMerchantURL);
com.braintreegateway.Result<Transaction> result = gateway.transaction().sale(request);
storeResults(paymentInfo, result);
return false; // chain of command ends here, as payment has been processed.
} catch(Exception e) {
return true; // continue to next possible processor
}
}
private void storeResults(PaymentDTOEx paymentInfo, com.braintreegateway.Result<Transaction> result)
throws PluggableTaskException {
LOG.info("Braintree result is: " +result.getMessage());
paymentInfo.setPaymentResult(new PaymentResultDAS().find(
result.isSuccess()
? Constants.RESULT_OK
: Constants.RESULT_FAIL));
LOG.info("Processing result is " + paymentInfo.getPaymentResult().getId()
+ ", return value of process is " + result.isSuccess());
PaymentAuthorizationDTO auth = new BraintreeAuthorizationDTO(result);
paymentInfo.setAuthorization(auth);
storeProcessedAuthorization(paymentInfo, auth);
}
public void failure(Integer userId, Integer retry) { //@deprecated method
}
public boolean preAuth(PaymentDTOEx paymentInfo) throws PluggableTaskException {
LOG.info("braintree preauth call");
return false;
}
public boolean confirmPreAuth(PaymentAuthorizationDTO auth, PaymentDTOEx paymentInfo) throws PluggableTaskException {
LOG.info("braintree confirm preauth call");
return false;
}
}
/*
* Maps a Braintree Result<Transaction> to a PaymentAuthorizationDTO
*/
class BraintreeAuthorizationDTO extends PaymentAuthorizationDTO {
private static final long serialVersionUID = 1L;
private static final String PROCESSOR = "Braintree";
public BraintreeAuthorizationDTO(com.braintreegateway.Result<Transaction> result) {
setProcessor(PROCESSOR);
if (!result.isSuccess()) {
setResponseMessage(result.getMessage());
return;
}
Transaction t = result.getTarget();
if (t == null) return;
setCode1(t.getProcessorResponseCode());
setCode2(t.getGatewayRejectionReason() == null? "" : t.getGatewayRejectionReason().toString());
setCode3(t.getCvvResponseCode());
setResponseMessage(t.getProcessorResponseText());
setApprovalCode(t.getProcessorAuthorizationCode());
setAvs(t.getAvsErrorResponseCode());
setTransactionId(t.getId());
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment