AddTopLevelElement() public méthode

public AddTopLevelElement ( string name, string value ) : RequestBuilder
name string
value string
Résultat RequestBuilder
Exemple #1
0
        public override string ToQueryString()
        {
            var builder = new RequestBuilder();

            builder.AddTopLevelElement("merchant_id", MerchantId);
            builder.AddTopLevelElement("redirect_uri", RedirectUri);
            builder.AddTopLevelElement("scope", Scope);
            builder.AddTopLevelElement("state", State);
            builder.AddTopLevelElement("landing_page", LandingPage);
            builder.AddTopLevelElement("client_id", ClientId);

            foreach (var paymentMethod in PaymentMethods)
            {
                builder.AddTopLevelElement("payment_methods[]", paymentMethod);
            }

            builder.AddElement("user", User);
            builder.AddElement("business", Business);
            return(builder.ToQueryString());
        }
        public override string ToQueryString()
        {
            var builder = new RequestBuilder();
            builder.AddTopLevelElement("merchant_id", MerchantId);
            builder.AddTopLevelElement("redirect_uri", RedirectUri);
            builder.AddTopLevelElement("scope", Scope);
            builder.AddTopLevelElement("state", State);
            builder.AddTopLevelElement("client_id", ClientId);

            foreach (var paymentMethod in PaymentMethods) {
                builder.AddTopLevelElement("payment_methods[]", paymentMethod);
            }

            builder.AddElement("user", User);
            builder.AddElement("business", Business);
            return builder.ToQueryString();
        }
    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;
    }
    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;
    }
    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);
    }
    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);
    }
    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);
    }
        public void Generate_GatewayRespectsMakeDefault()
        {
            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 = "5105105105105100",
                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);

            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());
            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);
                }
            }
        }