コード例 #1
0
        public static string GetNonceForNewPaymentMethod(BraintreeGateway gateway, Params @params, bool isCreditCard)
        {
            var clientToken = GenerateDecodedClientToken(gateway);
            var authorizationFingerprint = extractParamFromJson("authorizationFingerprint", clientToken);

            var paymentMethodType       = isCreditCard ? "credit_card" : "paypal_account";
            var paymentMethodTypePlural = paymentMethodType + "s";
            var builder = new RequestBuilder();

            builder.
            AddTopLevelElement("authorization_fingerprint", authorizationFingerprint).
            AddTopLevelElement("shared_customer_identifier", "test-identifier").
            AddTopLevelElement("shared_customer_identifier_type", "testing");
            foreach (var param in @params)
            {
                builder.AddTopLevelElement(string.Format("{0}[{1}]", paymentMethodType, param.Key), param.Value.ToString());
            }

            var response = new BraintreeTestHttpService().Post(
                gateway.MerchantId,
                "v1/payment_methods/" + paymentMethodTypePlural,
                builder.ToQueryString());

            return(extractParamFromJson("nonce", response));
        }
コード例 #2
0
        public void Generate_GatewayRespectsFailOnDuplicatePaymentMethod()
        {
            BraintreeGateway gateway = new BraintreeGateway
            {
                Environment = Environment.DEVELOPMENT,
                MerchantId  = "integration_merchant_id",
                PublicKey   = "integration_public_key",
                PrivateKey  = "integration_private_key"
            };
            Result <Customer> result = gateway.Customer.Create(new CustomerRequest());

            Assert.IsTrue(result.IsSuccess());
            string customerId = result.Target.Id;

            var request = new CreditCardRequest
            {
                CustomerId     = customerId,
                Number         = "4111111111111111",
                ExpirationDate = "05/2099"
            };

            Result <CreditCard> creditCardResult = gateway.CreditCard.Create(request);

            Assert.IsTrue(creditCardResult.IsSuccess());

            var clientToken = TestHelper.GenerateDecodedClientToken(gateway,
                                                                    new ClientTokenRequest
            {
                CustomerId = customerId,
                Options    = new ClientTokenOptionsRequest
                {
                    FailOnDuplicatePaymentMethod = true
                }
            }
                                                                    );
            var authorizationFingerprint = TestHelper.extractParamFromJson("authorizationFingerprint", clientToken);

            RequestBuilder builder = new RequestBuilder("");

            builder.AddTopLevelElement("authorization_fingerprint", authorizationFingerprint).
            AddTopLevelElement("shared_customer_identifier_type", "testing").
            AddTopLevelElement("shared_customer_identifier", "test-identifier").
            AddTopLevelElement("credit_card[number]", "4111111111111111").
            AddTopLevelElement("credit_card[expiration_month]", "11").
            AddTopLevelElement("credit_card[expiration_year]", "2099");

            HttpWebResponse response = new BraintreeTestHttpService().Post(gateway.MerchantId, "v1/payment_methods/credit_cards.json", builder.ToQueryString());

            response = new BraintreeTestHttpService().Post(gateway.MerchantId, "v1/payment_methods/credit_cards.json", builder.ToQueryString());

            Assert.AreEqual(422, (int)response.StatusCode);
            response.Close();

            Customer customer = gateway.Customer.Find(customerId);

            Assert.AreEqual(1, customer.CreditCards.Length);
        }
コード例 #3
0
        public static string GetNonceForPayPalAccount(BraintreeGateway gateway, Params paypalAccountDetails)
        {
            var clientToken = GenerateDecodedClientToken(gateway);
            var authorizationFingerprint = extractParamFromJson("authorizationFingerprint", clientToken);
            var builder = new RequestBuilder();

            builder.AddTopLevelElement("authorization_fingerprint", authorizationFingerprint);
            foreach (var param in paypalAccountDetails)
            {
                builder.AddTopLevelElement(string.Format("paypal_account[{0}]", param.Key), param.Value.ToString());
            }

            var response = new BraintreeTestHttpService().Post(gateway.MerchantId, "v1/payment_methods/paypal_accounts", builder.ToQueryString());

            return(extractParamFromJson("nonce", response));
        }
コード例 #4
0
        public static string GetNonceForNewCreditCard(BraintreeGateway gateway, Params creditCardDetails, string customerId = null)
        {
            var clientToken = TestHelper.GenerateDecodedClientToken(
                gateway,
                customerId == null ? null : new ClientTokenRequest {
                CustomerId = customerId
            });

            var authorizationFingerprint = extractParamFromJson("authorizationFingerprint", clientToken);

            var builder = new RequestBuilder();

            builder.
            AddTopLevelElement("authorization_fingerprint", authorizationFingerprint).
            AddTopLevelElement("shared_customer_identifier", "test-identifier").
            AddTopLevelElement("shared_customer_identifier_type", "testing");

            foreach (var param in creditCardDetails)
            {
                var nested = param.Value as Params;
                if (null != nested)
                {
                    foreach (var nestedParam in nested)
                    {
                        builder.AddTopLevelElement(string.Format("credit_card[{0}][{1}]", param.Key, nestedParam.Key), nestedParam.Value.ToString());
                    }
                }
                else
                {
                    builder.AddTopLevelElement(string.Format("credit_card[{0}]", param.Key), param.Value.ToString());
                }
            }

            var response = new BraintreeTestHttpService().Post(
                gateway.MerchantId,
                "v1/payment_methods/credit_cards",
                builder.ToQueryString());

            return(extractParamFromJson("nonce", response));
        }
コード例 #5
0
        public static string GenerateFuturePaymentPayPalNonce(BraintreeGateway gateway)
        {
            var            clientToken = TestHelper.GenerateDecodedClientToken(gateway);
            var            authorizationFingerprint = extractParamFromJson("authorizationFingerprint", clientToken);
            RequestBuilder builder = new RequestBuilder("");

            builder.AddTopLevelElement("authorization_fingerprint", authorizationFingerprint).
            AddTopLevelElement("shared_customer_identifier_type", "testing").
            AddTopLevelElement("shared_customer_identifier", "test-identifier").
            AddTopLevelElement("paypal_account[consent_code]", "consent").
            AddTopLevelElement("paypal_account[correlation_id]", System.Guid.NewGuid().ToString()).
            AddTopLevelElement("paypal_account[options][validate]", "false");

            HttpWebResponse response     = new BraintreeTestHttpService().Post(gateway.MerchantId, "v1/payment_methods/paypal_accounts", builder.ToQueryString());
            StreamReader    reader       = new StreamReader(response.GetResponseStream(), Encoding.UTF8);
            string          responseBody = reader.ReadToEnd();

            Regex regex = new Regex("nonce\":\"(?<nonce>[a-f0-9\\-]+)\"");
            Match match = regex.Match(responseBody);

            return(match.Groups["nonce"].Value);
        }
コード例 #6
0
        public static string GenerateUnlockedNonce(BraintreeGateway gateway, string creditCardNumber, string customerId)
        {
            var clientToken = "";

            if (customerId == null)
            {
                clientToken = TestHelper.GenerateDecodedClientToken(gateway);
            }
            else
            {
                clientToken = TestHelper.GenerateDecodedClientToken(gateway, new ClientTokenRequest
                {
                    CustomerId = customerId
                }
                                                                    );
            }
            var            authorizationFingerprint = extractParamFromJson("authorizationFingerprint", clientToken);
            RequestBuilder builder = new RequestBuilder("");

            builder.AddTopLevelElement("authorization_fingerprint", authorizationFingerprint).
            AddTopLevelElement("shared_customer_identifier_type", "testing").
            AddTopLevelElement("shared_customer_identifier", "test-identifier").
            AddTopLevelElement("credit_card[number]", creditCardNumber).
            AddTopLevelElement("share", "true").
            AddTopLevelElement("credit_card[expiration_month]", "11").
            AddTopLevelElement("credit_card[expiration_year]", "2099");

            HttpWebResponse response     = new BraintreeTestHttpService().Post(gateway.MerchantId, "v1/payment_methods/credit_cards.json", builder.ToQueryString());
            StreamReader    reader       = new StreamReader(response.GetResponseStream(), Encoding.UTF8);
            string          responseBody = reader.ReadToEnd();

            Regex regex = new Regex("nonce\":\"(?<nonce>[a-f0-9\\-]+)\"");
            Match match = regex.Match(responseBody);

            return(match.Groups["nonce"].Value);
        }
コード例 #7
0
        public void Generate_GeneratedFingerprintIsAcceptedByGateway()
        {
            BraintreeGateway gateway = new BraintreeGateway
            {
                Environment = Environment.DEVELOPMENT,
                MerchantId  = "integration_merchant_id",
                PublicKey   = "integration_public_key",
                PrivateKey  = "integration_private_key"
            };
            var clientToken = TestHelper.GenerateDecodedClientToken(gateway);
            var authorizationFingerprint = TestHelper.extractParamFromJson("authorizationFingerprint", clientToken);

            var encodedFingerprint = HttpUtility.UrlEncode(authorizationFingerprint, Encoding.UTF8);
            var url = "v1/payment_methods.json";

            url += "?authorizationFingerprint=" + encodedFingerprint;
            url += "&sharedCustomerIdentifierType=testing";
            url += "&sharedCustomerIdentifier=test-identifier";

            HttpWebResponse response = new BraintreeTestHttpService().Get(gateway.MerchantId, url);

            Assert.AreEqual(HttpStatusCode.OK, response.StatusCode);
            response.Close();
        }
コード例 #8
0
        public void Generate_GeneratedFingerprintIsAcceptedByGateway()
        {
            var gateway = new BraintreeGateway();
            var clientToken = TestHelper.GenerateDecodedClientToken(gateway);
            var authorizationFingerprint = TestHelper.extractParamFromJson("authorizationFingerprint", clientToken);

            var encodedFingerprint = HttpUtility.UrlEncode(authorizationFingerprint, Encoding.UTF8);
            var url = "v1/payment_methods.json";
            url += "?authorizationFingerprint=" + encodedFingerprint;
            url += "&sharedCustomerIdentifierType=testing";
            url += "&sharedCustomerIdentifier=test-identifier";

            HttpWebResponse response = new BraintreeTestHttpService().Get(gateway.MerchantId, url);
            Assert.AreEqual(HttpStatusCode.OK, response.StatusCode);
            response.Close();
        }
コード例 #9
0
        public void Generate_GatewayRespectsMakeDefault()
        {
            var gateway = new BraintreeGateway();
            Result<Customer> result = gateway.Customer.Create(new CustomerRequest());
            Assert.IsTrue(result.IsSuccess());
            string customerId = result.Target.Id;

            var request = new CreditCardRequest
            {
                CustomerId = customerId,
                Number = "5555555555554444",
                ExpirationDate = "05/2099"
            };
            Result<CreditCard> creditCardResult = gateway.CreditCard.Create(request);
            Assert.IsTrue(creditCardResult.IsSuccess());

            var clientToken = TestHelper.GenerateDecodedClientToken(gateway,
                new ClientTokenRequest
                {
                    CustomerId = customerId,
                    Options = new ClientTokenOptionsRequest
                    {
                        MakeDefault = true
                    }
                }
            );
            var authorizationFingerprint = TestHelper.extractParamFromJson("authorizationFingerprint", clientToken);

            var builder = new RequestBuilder("");
            builder.AddTopLevelElement("authorization_fingerprint", authorizationFingerprint).
                AddTopLevelElement("shared_customer_identifier_type", "testing").
                AddTopLevelElement("shared_customer_identifier", "test-identifier").
                AddTopLevelElement("credit_card[number]", "4111111111111111").
                AddTopLevelElement("credit_card[expiration_month]", "11").
                AddTopLevelElement("credit_card[expiration_year]", "2099");

            HttpWebResponse response = new BraintreeTestHttpService().Post(gateway.MerchantId, "v1/payment_methods/credit_cards.json", builder.ToQueryString());
            Assert.AreEqual(HttpStatusCode.Created, response.StatusCode);
            response.Close();

            Customer customer = gateway.Customer.Find(customerId);
            Assert.AreEqual(2, customer.CreditCards.Length);
            foreach (CreditCard creditCard in customer.CreditCards)
            {
                if (creditCard.LastFour == "1111") {
                    Assert.IsTrue(creditCard.IsDefault.Value);
                }
            }
        }
コード例 #10
0
        public void Generate_GatewayRespectsVerifyCard()
        {
            var gateway = new BraintreeGateway();
            Result<Customer> result = gateway.Customer.Create(new CustomerRequest());
            Assert.IsTrue(result.IsSuccess());

            string customerId = result.Target.Id;
            var clientToken = TestHelper.GenerateDecodedClientToken(gateway,
                new ClientTokenRequest
                {
                    CustomerId = customerId,
                    Options = new ClientTokenOptionsRequest
                    {
                        VerifyCard = true
                    }
                }
            );
            var authorizationFingerprint = TestHelper.extractParamFromJson("authorizationFingerprint", clientToken);

            var builder = new RequestBuilder("");
            builder.AddTopLevelElement("authorization_fingerprint", authorizationFingerprint).
                AddTopLevelElement("shared_customer_identifier_type", "testing").
                AddTopLevelElement("shared_customer_identifier", "test-identifier").
                AddTopLevelElement("credit_card[number]", "4000111111111115").
                AddTopLevelElement("credit_card[expiration_month]", "11").
                AddTopLevelElement("credit_card[expiration_year]", "2099");

            HttpWebResponse response = new BraintreeTestHttpService().Post(gateway.MerchantId, "v1/payment_methods/credit_cards.json", builder.ToQueryString());
            Assert.AreEqual(422, (int)response.StatusCode);
            response.Close();

            Customer customer = gateway.Customer.Find(customerId);
            Assert.AreEqual(0, customer.CreditCards.Length);
        }
コード例 #11
0
    public static string GenerateFuturePaymentPayPalNonce(BraintreeGateway gateway)
    {
        var clientToken = TestHelper.GenerateDecodedClientToken(gateway);
        var authorizationFingerprint  = extractParamFromJson("authorizationFingerprint", clientToken);
        RequestBuilder builder = new RequestBuilder("");
        builder.AddTopLevelElement("authorization_fingerprint", authorizationFingerprint).
            AddTopLevelElement("shared_customer_identifier_type", "testing").
            AddTopLevelElement("shared_customer_identifier", "test-identifier").
            AddTopLevelElement("paypal_account[consent_code]", "consent").
            AddTopLevelElement("paypal_account[correlation_id]", System.Guid.NewGuid().ToString()).
            AddTopLevelElement("paypal_account[options][validate]", "false");

        HttpWebResponse response = new BraintreeTestHttpService().Post(gateway.MerchantId, "v1/payment_methods/paypal_accounts", builder.ToQueryString());
        StreamReader reader = new StreamReader(response.GetResponseStream(), Encoding.UTF8);
        string responseBody = reader.ReadToEnd();

        Regex regex = new Regex("nonce\":\"(?<nonce>[a-f0-9\\-]+)\"");
        Match match = regex.Match(responseBody);
        return match.Groups["nonce"].Value;
    }
コード例 #12
0
    public static string GenerateUnlockedNonce(BraintreeGateway gateway, string creditCardNumber, string customerId)
    {
      var clientToken = "";
      if (customerId ==  null) {
        clientToken = TestHelper.GenerateDecodedClientToken(gateway);
      } else {
        clientToken = TestHelper.GenerateDecodedClientToken(gateway, new ClientTokenRequest
          {
            CustomerId = customerId
          }
        );
      }
      var authorizationFingerprint  = extractParamFromJson("authorizationFingerprint", clientToken);
      RequestBuilder builder = new RequestBuilder("");
      builder.AddTopLevelElement("authorization_fingerprint", authorizationFingerprint).
        AddTopLevelElement("shared_customer_identifier_type", "testing").
        AddTopLevelElement("shared_customer_identifier", "test-identifier").
        AddTopLevelElement("credit_card[number]", creditCardNumber).
        AddTopLevelElement("share", "true").
        AddTopLevelElement("credit_card[expiration_month]", "11").
        AddTopLevelElement("credit_card[expiration_year]", "2099");

      HttpWebResponse response = new BraintreeTestHttpService().Post(gateway.MerchantId, "v1/payment_methods/credit_cards.json", builder.ToQueryString());
      StreamReader reader = new StreamReader(response.GetResponseStream(), Encoding.UTF8);
      string responseBody = reader.ReadToEnd();

      Regex regex = new Regex("nonce\":\"(?<nonce>[a-f0-9\\-]+)\"");
      Match match = regex.Match(responseBody);
      return match.Groups["nonce"].Value;
    }
コード例 #13
0
    public static string GetNonceForNewPaymentMethod(BraintreeGateway gateway, Params @params, bool isCreditCard)
    {
        var clientToken = GenerateDecodedClientToken(gateway);
        var authorizationFingerprint = extractParamFromJson("authorizationFingerprint", clientToken);

        var paymentMethodType = isCreditCard ? "credit_card" : "paypal_account";
        var paymentMethodTypePlural = paymentMethodType + "s";
        var builder = new RequestBuilder();
        builder.
            AddTopLevelElement("authorization_fingerprint", authorizationFingerprint).
            AddTopLevelElement("shared_customer_identifier", "test-identifier").
            AddTopLevelElement("shared_customer_identifier_type", "testing");
        foreach (var param in @params)
            builder.AddTopLevelElement(string.Format("{0}[{1}]", paymentMethodType, param.Key), param.Value.ToString());

        var response = new BraintreeTestHttpService().Post(
            gateway.MerchantId,
            "v1/payment_methods/" + paymentMethodTypePlural,
            builder.ToQueryString());

        return extractParamFromJson("nonce", response);
    }
コード例 #14
0
    public static string GetNonceForNewCreditCard(BraintreeGateway gateway, Params creditCardDetails, string customerId = null)
    {
        var clientToken = TestHelper.GenerateDecodedClientToken(
            gateway,
            customerId == null ? null : new ClientTokenRequest { CustomerId = customerId });

        var authorizationFingerprint = extractParamFromJson("authorizationFingerprint", clientToken);

        var builder = new RequestBuilder();
        builder.
            AddTopLevelElement("authorization_fingerprint", authorizationFingerprint).
            AddTopLevelElement("shared_customer_identifier", "test-identifier").
            AddTopLevelElement("shared_customer_identifier_type", "testing");

        foreach (var param in creditCardDetails) {
            var nested = param.Value as Params;
            if (null != nested) {
                foreach (var nestedParam in nested) {
                    builder.AddTopLevelElement(string.Format("credit_card[{0}][{1}]", param.Key, nestedParam.Key), nestedParam.Value.ToString());
                }
            } else
                builder.AddTopLevelElement(string.Format("credit_card[{0}]", param.Key), param.Value.ToString());
        }

        var response = new BraintreeTestHttpService().Post(
            gateway.MerchantId,
            "v1/payment_methods/credit_cards",
            builder.ToQueryString());

        return extractParamFromJson("nonce", response);
    }
コード例 #15
0
    public static string GetNonceForPayPalAccount(BraintreeGateway gateway, Params paypalAccountDetails)
    {
        var clientToken = GenerateDecodedClientToken(gateway);
        var authorizationFingerprint = extractParamFromJson("authorizationFingerprint", clientToken);
        var builder = new RequestBuilder();
        builder.AddTopLevelElement("authorization_fingerprint", authorizationFingerprint);
        foreach (var param in paypalAccountDetails)
            builder.AddTopLevelElement(string.Format("paypal_account[{0}]", param.Key), param.Value.ToString());

        var response = new BraintreeTestHttpService().Post(gateway.MerchantId, "v1/payment_methods/paypal_accounts", builder.ToQueryString());
        return extractParamFromJson("nonce", response);
    }
コード例 #16
0
        public void Generate_GatewayRespectsFailOnDuplicatePaymentMethod()
        {
            BraintreeGateway gateway = new BraintreeGateway
            {
                Environment = Environment.DEVELOPMENT,
                MerchantId = "integration_merchant_id",
                PublicKey = "integration_public_key",
                PrivateKey = "integration_private_key"
            };
            Result<Customer> result = gateway.Customer.Create(new CustomerRequest());
            Assert.IsTrue(result.IsSuccess());
            string customerId = result.Target.Id;

            var request = new CreditCardRequest
            {
                CustomerId = customerId,
                Number = "4111111111111111",
                ExpirationDate = "05/2099"
            };

            Result<CreditCard> creditCardResult = gateway.CreditCard.Create(request);
            Assert.IsTrue(creditCardResult.IsSuccess());

            var clientToken = TestHelper.GenerateDecodedClientToken(gateway,
                new ClientTokenRequest
                {
                    CustomerId = customerId,
                    Options = new ClientTokenOptionsRequest
                    {
                        FailOnDuplicatePaymentMethod = true
                    }
                }
            );
            var authorizationFingerprint = TestHelper.extractParamFromJson("authorizationFingerprint", clientToken);

            RequestBuilder builder = new RequestBuilder("");
            builder.AddTopLevelElement("authorization_fingerprint", authorizationFingerprint).
                AddTopLevelElement("shared_customer_identifier_type", "testing").
                AddTopLevelElement("shared_customer_identifier", "test-identifier").
                AddTopLevelElement("credit_card[number]", "4111111111111111").
                AddTopLevelElement("credit_card[expiration_month]", "11").
                AddTopLevelElement("credit_card[expiration_year]", "2099");

            HttpWebResponse response = new BraintreeTestHttpService().Post(gateway.MerchantId, "v1/payment_methods/credit_cards.json", builder.ToQueryString());
            response = new BraintreeTestHttpService().Post(gateway.MerchantId, "v1/payment_methods/credit_cards.json", builder.ToQueryString());

            Assert.AreEqual(422, (int)response.StatusCode);
            response.Close();

            Customer customer = gateway.Customer.Find(customerId);
            Assert.AreEqual(1, customer.CreditCards.Length);
        }