Skip to content

Commit

Permalink
Merge branch 'payment_method_grant_revoke'
Browse files Browse the repository at this point in the history
Merges braintree#38
Conflicts:
	src/test/java/com/braintreegateway/integrationtest/PaymentMethodIT.java
  • Loading branch information
braintreeps committed Mar 1, 2016
2 parents 4f11cdb + cfd4c3d commit f974e72
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 0 deletions.
12 changes: 12 additions & 0 deletions src/main/java/com/braintreegateway/PaymentMethodGateway.java
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,18 @@ public PaymentMethod find(String token) {
return parseResponse(response).getTarget();
}

public Result<PaymentMethodNonce> 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<PaymentMethodNonce>(response, PaymentMethodNonce.class);
}

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

public Result<? extends PaymentMethod> parseResponse(NodeWrapper response) {
if (response.getElementName() == "paypal-account") {
return new Result<PayPalAccount>(response, PayPalAccount.class);
Expand Down
4 changes: 4 additions & 0 deletions src/main/java/com/braintreegateway/util/Http.java
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,10 @@ public NodeWrapper post(String url, Request request) {
return httpRequest(RequestMethod.POST, url, request.toXML());
}

public NodeWrapper post(String url, String request) {
return httpRequest(RequestMethod.POST, url, request);
}

public NodeWrapper put(String url) {
return httpRequest(RequestMethod.PUT, url, null);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@
import com.braintreegateway.testhelpers.TestHelper;
import com.braintreegateway.exceptions.NotFoundException;
import com.braintreegateway.test.Nonce;
import com.braintreegateway.util.NodeWrapper;
import org.junit.Before;
import org.junit.Test;

import static org.junit.Assert.*;
Expand Down Expand Up @@ -1180,4 +1182,33 @@ public void updateDoesNotOverrideBillingAddressForVaultedCreditCards() {
assertFalse(foundCreditCard == null);
assertEquals(foundCreditCard.getBillingAddress().getStreetAddress(), "456 Xyz Way");
}

@Test
public void grantAndRevoke() {
BraintreeGateway partnerMerchantGateway = new BraintreeGateway(Environment.DEVELOPMENT, "integration_merchant_public_id", "oauth_app_partner_user_public_key", "oauth_app_partner_user_private_key");
Result<Customer> customerResult = partnerMerchantGateway.customer().create(new CustomerRequest());
Customer customer = customerResult.getTarget();

PaymentMethodRequest request = new PaymentMethodRequest().
paymentMethodNonce(Nonce.Transactable).
customerId(customer.getId());
Result<? extends PaymentMethod> result = partnerMerchantGateway.paymentMethod().create(request);
String paymentMethodToken = result.getTarget().getToken();
BraintreeGateway oauthGateway = new BraintreeGateway("client_id$development$integration_client_id", "client_secret$development$integration_client_secret");
String code = TestHelper.createOAuthGrant(oauthGateway, "integration_merchant_id", "grant_payment_method");

OAuthCredentialsRequest oauthRequest = new OAuthCredentialsRequest().
code(code).
scope("grant_payment_method");


Result<OAuthCredentials> accessTokenResult = oauthGateway.oauth().createTokenFromCode(oauthRequest);

BraintreeGateway accessTokenGateway = new BraintreeGateway(accessTokenResult.getTarget().getAccessToken());
Result<PaymentMethodNonce> grantResult = accessTokenGateway.paymentMethod().grant(paymentMethodToken);
assertTrue(grantResult.isSuccess());
NodeWrapper revokeResult = accessTokenGateway.paymentMethod().revoke(paymentMethodToken);
assertTrue(revokeResult.isSuccess());
}

}

0 comments on commit f974e72

Please sign in to comment.