All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.braintreegateway.PaymentMethodGateway Maven / Gradle / Ivy

The newest version!
package com.braintreegateway;

import com.braintreegateway.exceptions.NotFoundException;
import com.braintreegateway.util.Http;
import com.braintreegateway.util.NodeWrapper;
import com.braintreegateway.util.PaymentMethodParser;

public class PaymentMethodGateway {
    private Http http;
    private Configuration configuration;

    public PaymentMethodGateway(Http http, Configuration configuration) {
        this.http = http;
        this.configuration = configuration;
    }

    public Result create(PaymentMethodRequest request) {
        NodeWrapper response = http.post(configuration.getMerchantPath() + "/payment_methods", request);
        return parseResponse(response);
    }

    public Result update(String token, PaymentMethodRequest request) {
        NodeWrapper response = http.put(configuration.getMerchantPath() + "/payment_methods/any/" + token, request);
        return parseResponse(response);
    }

    public Result delete(String token) {
        http.delete(configuration.getMerchantPath() + "/payment_methods/any/" + token);
        return new Result();
    }

    public Result delete(String token, PaymentMethodDeleteRequest request) {
        http.delete(configuration.getMerchantPath() + "/payment_methods/any/" + token + "?" + request.toQueryString());
        return new Result();
    }

    public PaymentMethod find(String token) {
        if(token == null || token.trim().equals("")) {
            throw new NotFoundException();
        }

        NodeWrapper response = http.get(configuration.getMerchantPath() + "/payment_methods/any/" + token);

        return parseResponse(response).getTarget();
    }

    public Result grant(String token) {
        String request = new RequestBuilder("payment-method").addElement("shared-payment-method-token", token).toXML();
        NodeWrapper response = http.post(configuration.getMerchantPath() + "/payment_methods/grant", request);
        return new Result(response, PaymentMethodNonce.class);
    }

    public Result grant(String token, PaymentMethodGrantRequest grantRequest) {
        String request = grantRequest.sharedPaymentMethodToken(token).toXML();
        NodeWrapper response = http.post(configuration.getMerchantPath() + "/payment_methods/grant", request);
        return new Result(response, PaymentMethodNonce.class);
    }

    public Result revoke(String token) {
        PaymentMethodGrantRevokeRequest revokeRequest = new PaymentMethodGrantRevokeRequest();
        String request = revokeRequest.sharedPaymentMethodToken(token).toXML();
        NodeWrapper response = http.post(configuration.getMerchantPath() + "/payment_methods/revoke", request);
        return parseResponse(response);
    }

    public Result parseResponse(NodeWrapper response) {
        return PaymentMethodParser.parsePaymentMethod(response);
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy