public async Task MapsPagingProperties()
        {
            // given
            var subject = new MandatesClient(_clientConfiguration);

            var firstPageRequest = new GetMandatesRequest
            {
                Limit = 1
            };

            // when
            var firstPageResult = await subject.GetPageAsync(firstPageRequest);

            var secondPageRequest = new GetMandatesRequest
            {
                After = firstPageResult.Meta.Cursors.After,
                Limit = 2
            };

            var secondPageResult = await subject.GetPageAsync(secondPageRequest);

            // then
            Assert.That(firstPageResult.Items.Count(), Is.EqualTo(firstPageRequest.Limit));
            Assert.That(firstPageResult.Meta.Limit, Is.EqualTo(firstPageRequest.Limit));
            Assert.That(firstPageResult.Meta.Cursors.Before, Is.Null);
            Assert.That(firstPageResult.Meta.Cursors.After, Is.Not.Null);

            Assert.That(secondPageResult.Items.Count(), Is.EqualTo(secondPageRequest.Limit));
            Assert.That(secondPageResult.Meta.Limit, Is.EqualTo(secondPageRequest.Limit));
            Assert.That(secondPageResult.Meta.Cursors.Before, Is.Not.Null);
            Assert.That(secondPageResult.Meta.Cursors.After, Is.Not.Null);
        }
Ejemplo n.º 2
0
        internal async Task <Mandate> CreateMandateFor(
            Creditor creditor,
            Customer customer,
            CustomerBankAccount customerBankAccount)
        {
            var request = new CreateMandateRequest
            {
                Links = new CreateMandateLinks
                {
                    Creditor            = creditor.Id,
                    CustomerBankAccount = customerBankAccount.Id
                },
                Metadata = new Dictionary <string, string>
                {
                    ["Key1"] = "Value1",
                    ["Key2"] = "Value2",
                    ["Key3"] = "Value3",
                },
                Scheme = Scheme.Bacs
            };

            var mandatesClient = new MandatesClient(_clientConfiguration);

            return((await mandatesClient.CreateAsync(request)).Item);
        }
        public async Task UpdatesMandateReplacingMetadata()
        {
            // given
            var subject = new MandatesClient(_clientConfiguration);
            var mandate = await _resourceFactory.CreateMandateFor(_creditor, _customer, _customerBankAccount);

            var request = new UpdateMandateRequest
            {
                Id       = mandate.Id,
                Metadata = new Dictionary <string, string>
                {
                    ["Key4"] = "Value4",
                    ["Key5"] = "Value5",
                    ["Key6"] = "Value6",
                },
            };

            // when
            var result = await subject.UpdateAsync(request);

            var actual = result.Item;

            // then
            Assert.That(actual, Is.Not.Null);
            Assert.That(actual.Id, Is.Not.Null);
            Assert.That(actual.Metadata, Is.EqualTo(request.Metadata));
        }
        public async Task CreatesSubscriptionForMerchant()
        {
            // given
            var accessToken    = Environment.GetEnvironmentVariable("GoCardlessMerchantAccessToken");
            var configuration  = ClientConfiguration.ForSandbox(accessToken);
            var mandatesClient = new MandatesClient(configuration);
            var mandate        = (await mandatesClient.GetPageAsync()).Items.First();

            var request = new CreateSubscriptionRequest
            {
                Amount       = 123,
                AppFee       = 12,
                Count        = 5,
                Currency     = "GBP",
                Interval     = 1,
                IntervalUnit = IntervalUnit.Weekly,
                Links        = new SubscriptionLinks
                {
                    Mandate = mandate.Id
                },
                Metadata = new Dictionary <string, string>
                {
                    ["Key1"] = "Value1",
                    ["Key2"] = "Value2",
                    ["Key3"] = "Value3",
                },
                Name      = "Test subscription",
                StartDate = DateTime.Now.AddMonths(1)
            };

            var subject = new SubscriptionsClient(configuration);

            // when
            var result = await subject.CreateAsync(request);

            // then
            Assert.That(result.Item.Id, Is.Not.Empty);
            Assert.That(result.Item.Amount, Is.EqualTo(request.Amount));
            Assert.That(result.Item.AppFee, Is.EqualTo(request.AppFee));
            Assert.That(result.Item.CreatedAt, Is.Not.Null.And.Not.EqualTo(default(DateTimeOffset)));
            Assert.That(result.Item.Currency, Is.EqualTo(request.Currency));
            Assert.That(result.Item.DayOfMonth, Is.EqualTo(request.DayOfMonth));
            Assert.That(result.Item.Interval, Is.EqualTo(request.Interval));
            Assert.That(result.Item.IntervalUnit, Is.EqualTo(request.IntervalUnit));
            Assert.That(result.Item.Links, Is.Not.Null);
            Assert.That(result.Item.Links.Mandate, Is.EqualTo(request.Links.Mandate));
            Assert.That(result.Item.Metadata, Is.EqualTo(request.Metadata));
            Assert.That(result.Item.Month, Is.EqualTo(request.Month));
            Assert.That(result.Item.Name, Is.EqualTo(request.Name));
            Assert.That(result.Item.PaymentReference, Is.EqualTo(request.PaymentReference));
            Assert.That(result.Item.StartDate.Date, Is.EqualTo(request.StartDate.Value.Date));
            Assert.That(result.Item.Status, Is.EqualTo(SubscriptionStatus.Active));
            Assert.That(result.Item.UpcomingPayments.Count(), Is.EqualTo(request.Count));
        }
Ejemplo n.º 5
0
        public async Task CallsGetMandatesEndpoint()
        {
            // given
            var subject = new MandatesClient(_clientConfiguration);

            // when
            await subject.GetPageAsync();

            // then
            _httpTest
            .ShouldHaveCalled("https://api.gocardless.com/mandates")
            .WithVerb(HttpMethod.Get);
        }
Ejemplo n.º 6
0
        public void IsNullOrWhiteSpaceThrows(string id)
        {
            // given
            var subject = new MandatesClient(_clientConfiguration);

            // when
            AsyncTestDelegate test = () => subject.ForIdAsync(id);

            // then
            var ex = Assert.ThrowsAsync <ArgumentException>(test);

            Assert.That(ex.Message, Is.Not.Null);
            Assert.That(ex.ParamName, Is.EqualTo(nameof(id)));
        }
Ejemplo n.º 7
0
        public async Task CallsIndividualMandatesEndpoint()
        {
            // given
            var subject = new MandatesClient(_clientConfiguration);
            var id      = "MD12345678";

            // when
            await subject.ForIdAsync(id);

            // then
            _httpTest
            .ShouldHaveCalled("https://api.gocardless.com/mandates/MD12345678")
            .WithVerb(HttpMethod.Get);
        }
Ejemplo n.º 8
0
        public void CreateMandateRequestIsNullThrows()
        {
            // given
            var subject = new MandatesClient(_clientConfiguration);

            CreateMandateRequest request = null;

            // when
            AsyncTestDelegate test = () => subject.CreateAsync(request);

            // then
            var ex = Assert.ThrowsAsync <ArgumentNullException>(test);

            Assert.That(ex.ParamName, Is.EqualTo(nameof(request)));
        }
Ejemplo n.º 9
0
        public async Task CallsCancelMandateEndpoint()
        {
            // given
            var subject = new MandatesClient(_clientConfiguration);

            var request = new CancelMandateRequest
            {
                Id = "MD12345678"
            };

            // when
            await subject.CancelAsync(request);

            // then
            _httpTest
            .ShouldHaveCalled("https://api.gocardless.com/mandates/MD12345678/actions/cancel")
            .WithVerb(HttpMethod.Post);
        }
Ejemplo n.º 10
0
        public void CancelMandateRequestIdIsNullOrWhiteSpaceThrows(string id)
        {
            // given
            var subject = new MandatesClient(_clientConfiguration);

            var request = new CancelMandateRequest
            {
                Id = id
            };

            // when
            AsyncTestDelegate test = () => subject.CancelAsync(request);

            // then
            var ex = Assert.ThrowsAsync <ArgumentException>(test);

            Assert.That(ex.ParamName, Is.EqualTo(nameof(request.Id)));
        }
Ejemplo n.º 11
0
        public async Task CallsCreateMandateEndpoint()
        {
            // given
            var subject = new MandatesClient(_clientConfiguration);

            var request = new CreateMandateRequest
            {
                IdempotencyKey = Guid.NewGuid().ToString()
            };

            // when
            await subject.CreateAsync(request);

            // then
            _httpTest
            .ShouldHaveCalled("https://api.gocardless.com/mandates")
            .WithHeader("Idempotency-Key")
            .WithVerb(HttpMethod.Post);
        }
Ejemplo n.º 12
0
        public async Task CallsGetMandatesEndpointUsingRequest()
        {
            // given
            var subject = new MandatesClient(_clientConfiguration);

            var request = new GetMandatesRequest
            {
                Before = "before test",
                After  = "after test",
                Limit  = 5
            };

            // when
            await subject.GetPageAsync(request);

            // then
            _httpTest
            .ShouldHaveCalled("https://api.gocardless.com/mandates?before=before%20test&after=after%20test&limit=5")
            .WithVerb(HttpMethod.Get);
        }
        public async Task UpdatesMandatePreservingMetadata()
        {
            // given
            var subject = new MandatesClient(_clientConfiguration);
            var mandate = await _resourceFactory.CreateMandateFor(_creditor, _customer, _customerBankAccount);

            var request = new UpdateMandateRequest
            {
                Id = mandate.Id
            };

            // when
            var result = await subject.UpdateAsync(request);

            var actual = result.Item;

            // then
            Assert.That(actual, Is.Not.Null);
            Assert.That(actual.Id, Is.Not.Null);
            Assert.That(actual.Metadata, Is.EqualTo(mandate.Metadata));
        }
        public async Task ReturnsMandates()
        {
            // given
            var subject = new MandatesClient(_clientConfiguration);

            // when
            var result = (await subject.GetPageAsync()).Items.ToList();

            // then
            Assert.That(result.Any(), Is.True);
            Assert.That(result[0], Is.Not.Null);
            Assert.That(result[0].Id, Is.Not.Null);
            Assert.That(result[0].CreatedAt, Is.Not.Null.And.Not.EqualTo(default(DateTimeOffset)));
            Assert.That(result[0].Links.Creditor, Is.Not.Null);
            Assert.That(result[0].Links.CustomerBankAccount, Is.Not.Null);
            Assert.That(result[0].Metadata, Is.Not.Null);
            Assert.That(result[0].NextPossibleChargeDate, Is.Not.EqualTo(default(DateTime)));
            Assert.That(result[0].Reference, Is.Not.Null);
            Assert.That(result[0].Scheme, Is.Not.Null);
            Assert.That(result[0].Status, Is.Not.Null);
        }
Ejemplo n.º 15
0
        public GoCardlessClient(ClientConfiguration configuration)
        {
            _configuration = configuration;

            BankDetailsLookups    = new BankDetailsLookupsClient(configuration);
            CreditorBankAccounts  = new CreditorBankAccountsClient(configuration);
            Creditors             = new CreditorsClient(configuration);
            CustomerBankAccounts  = new CustomerBankAccountsClient(configuration);
            CustomerNotifications = new CustomerNotificationsClient(configuration);
            Customers             = new CustomersClient(configuration);
            Events = new EventsClient(configuration);
            MandateImportEntries = new MandateImportEntriesClient(configuration);
            MandateImports       = new MandateImportsClient(configuration);
            MandatePdfs          = new MandatePdfsClient(configuration);
            Mandates             = new MandatesClient(configuration);
            Payments             = new PaymentsClient(configuration);
            PayoutItems          = new PayoutItemsClient(configuration);
            Payouts       = new PayoutsClient(configuration);
            RedirectFlows = new RedirectFlowsClient(configuration);
            Refunds       = new RefundsClient(configuration);
            Subscriptions = new SubscriptionsClient(configuration);
        }
        public async Task UpdatesSubscriptionForMerchant()
        {
            // given
            var accessToken     = Environment.GetEnvironmentVariable("GoCardlessMerchantAccessToken");
            var configuration   = ClientConfiguration.ForSandbox(accessToken);
            var resourceFactory = new ResourceFactory(configuration);

            var mandatesClient = new MandatesClient(configuration);
            var mandate        = (await mandatesClient.GetPageAsync()).Items.First();
            var subscription   = await resourceFactory.CreateSubscriptionFor(mandate, paymentReference : null);

            var request = new UpdateSubscriptionRequest
            {
                Id       = subscription.Id,
                Amount   = 456,
                AppFee   = 34,
                Metadata = new Dictionary <string, string>
                {
                    ["Key4"] = "Value4",
                    ["Key5"] = "Value5",
                    ["Key6"] = "Value6",
                },
                Name = "Updated subscription name"
            };

            var subject = new SubscriptionsClient(configuration);

            // when
            var result = await subject.UpdateAsync(request);

            // then
            Assert.That(result.Item.Id, Is.EqualTo(request.Id));
            Assert.That(result.Item.Amount, Is.EqualTo(request.Amount));
            Assert.That(result.Item.AppFee, Is.EqualTo(request.AppFee));
            Assert.That(result.Item.Metadata, Is.EqualTo(request.Metadata));
            Assert.That(result.Item.Name, Is.EqualTo(request.Name));
        }
        public async Task CreatesConflictingMandate()
        {
            // given
            var request = new CreateMandateRequest
            {
                Links = new CreateMandateLinks
                {
                    Creditor            = _creditor.Id,
                    CustomerBankAccount = _customerBankAccount.Id
                },
                Metadata = new Dictionary <string, string>
                {
                    ["Key1"] = "Value1",
                    ["Key2"] = "Value2",
                    ["Key3"] = "Value3",
                },
                Scheme = Scheme.Bacs,
            };

            var subject = new MandatesClient(_clientConfiguration);

            // when
            await subject.CreateAsync(request);

            var result = await subject.CreateAsync(request);

            // then
            Assert.That(result.Item, Is.Not.Null);
            Assert.That(result.Item.Id, Is.Not.Null);
            Assert.That(result.Item.CreatedAt, Is.Not.EqualTo(default(DateTimeOffset)));
            Assert.That(result.Item.Links.Creditor, Is.EqualTo(_creditor.Id));
            Assert.That(result.Item.Links.CustomerBankAccount, Is.EqualTo(_customerBankAccount.Id));
            Assert.That(result.Item.Metadata, Is.EqualTo(request.Metadata));
            Assert.That(result.Item.NextPossibleChargeDate, Is.Not.Null.And.Not.EqualTo(default(DateTime)));
            Assert.That(result.Item.Scheme, Is.EqualTo(request.Scheme));
            Assert.That(result.Item.Status, Is.Not.Null.And.Not.EqualTo(MandateStatus.Cancelled));
        }
        public async Task ReturnsIndividualMandate()
        {
            // given
            var subject = new MandatesClient(_clientConfiguration);
            var mandate = await _resourceFactory.CreateMandateFor(_creditor, _customer, _customerBankAccount);

            // when
            var result = await subject.ForIdAsync(mandate.Id);

            var actual = result.Item;

            // then
            Assert.That(actual, Is.Not.Null);
            Assert.That(actual.Id, Is.Not.Null.And.EqualTo(mandate.Id));
            Assert.That(actual.CreatedAt, Is.Not.EqualTo(default(DateTimeOffset)));
            Assert.That(actual.Links.Creditor, Is.Not.Null.And.EqualTo(mandate.Links.Creditor));
            Assert.That(actual.Links.Customer, Is.Not.Null.And.EqualTo(mandate.Links.Customer));
            Assert.That(actual.Links.CustomerBankAccount, Is.Not.Null.And.EqualTo(mandate.Links.CustomerBankAccount));
            Assert.That(actual.Metadata, Is.Not.Null.And.EqualTo(mandate.Metadata));
            Assert.That(actual.NextPossibleChargeDate, Is.Not.EqualTo(default(DateTimeOffset)));
            Assert.That(actual.Reference, Is.Not.Null);
            Assert.That(actual.Scheme, Is.Not.Null.And.EqualTo(mandate.Scheme));
            Assert.That(actual.Status, Is.Not.Null.And.EqualTo(mandate.Status));
        }
        public async Task PagesThroughMandates()
        {
            // given
            var subject = new MandatesClient(_clientConfiguration);
            var firstId = (await subject.GetPageAsync()).Items.First().Id;

            var initialRequest = new GetMandatesRequest
            {
                After = firstId,
                CreatedGreaterThan = new DateTimeOffset(DateTime.Now.AddDays(-1)),
                Limit = 1,
            };

            // when
            var result = await subject
                         .BuildPager()
                         .StartFrom(initialRequest)
                         .AndGetAllAfterAsync();

            // then
            Assert.That(result.Count, Is.GreaterThan(1));
            Assert.That(result[0].Id, Is.Not.Null.And.Not.EqualTo(result[1].Id));
            Assert.That(result[1].Id, Is.Not.Null.And.Not.EqualTo(result[0].Id));
        }
        public async Task CreatesAndCancelsPaymentForMerchant()
        {
            var accessToken     = Environment.GetEnvironmentVariable("GoCardlessMerchantAccessToken");
            var configuration   = ClientConfiguration.ForSandbox(accessToken);
            var resourceFactory = new ResourceFactory(configuration);

            var creditor = await resourceFactory.Creditor();

            var mandatesClient = new MandatesClient(configuration);
            var mandate        = (await mandatesClient.GetPageAsync()).Items.First();

            // given
            var createRequest = new CreatePaymentRequest
            {
                Amount      = 500,
                AppFee      = 12,
                ChargeDate  = DateTime.Now.AddMonths(1),
                Description = "Sandbox Payment",
                Currency    = "GBP",
                Links       = new CreatePaymentLinks {
                    Mandate = mandate.Id
                },
                Metadata = new Dictionary <string, string>
                {
                    ["Key1"] = "Value1",
                    ["Key2"] = "Value2",
                    ["Key3"] = "Value3",
                }
            };

            var subject = new PaymentsClient(configuration);

            // when
            var creationResult = await subject.CreateAsync(createRequest);

            var cancelRequest = new CancelPaymentRequest
            {
                Id       = creationResult.Item.Id,
                Metadata = new Dictionary <string, string>
                {
                    ["Key4"] = "Value4",
                    ["Key5"] = "Value5",
                    ["Key6"] = "Value6",
                },
            };

            var cancellationResult = await subject.CancelAsync(cancelRequest);

            // then
            Assert.That(creationResult.Item.Id, Is.Not.Null);
            Assert.That(creationResult.Item.Amount, Is.EqualTo(createRequest.Amount));
            Assert.That(creationResult.Item.AmountRefunded, Is.Not.Null);
            Assert.That(creationResult.Item.ChargeDate, Is.Not.Null.And.Not.EqualTo(default(DateTime)));
            Assert.That(creationResult.Item.CreatedAt, Is.Not.Null.And.Not.EqualTo(default(DateTimeOffset)));
            Assert.That(creationResult.Item.Currency, Is.EqualTo(createRequest.Currency));
            Assert.That(creationResult.Item.Description, Is.EqualTo(createRequest.Description));
            Assert.That(creationResult.Item.Links.Creditor, Is.EqualTo(creditor.Id));
            Assert.That(creationResult.Item.Links.Mandate, Is.EqualTo(mandate.Id));
            Assert.That(creationResult.Item.Metadata, Is.EqualTo(createRequest.Metadata));
            Assert.That(creationResult.Item.Reference, Is.EqualTo(createRequest.Reference));
            Assert.That(creationResult.Item.Status, Is.Not.Null.And.Not.EqualTo(PaymentStatus.Cancelled));

            Assert.That(cancellationResult.Item.Status, Is.EqualTo(PaymentStatus.Cancelled));
        }
        public async Task CreatesCancelsAndReinstatesMandate()
        {
            // given
            var createRequest = new CreateMandateRequest
            {
                Links = new CreateMandateLinks
                {
                    Creditor            = _creditor.Id,
                    CustomerBankAccount = _customerBankAccount.Id
                },
                Metadata = new Dictionary <string, string>
                {
                    ["Key1"] = "Value1",
                    ["Key2"] = "Value2",
                    ["Key3"] = "Value3",
                },
                Reference = DateTime.Now.ToString("yyyyMMddhhmmss"),
                Scheme    = Scheme.Bacs
            };

            var subject = new MandatesClient(_clientConfiguration);

            // when
            var creationResult = await subject.CreateAsync(createRequest);

            var cancelRequest = new CancelMandateRequest
            {
                Id       = creationResult.Item.Id,
                Metadata = new Dictionary <string, string>
                {
                    ["Key4"] = "Value4",
                    ["Key5"] = "Value5",
                    ["Key6"] = "Value6",
                },
            };

            var cancellationResult = await subject.CancelAsync(cancelRequest);

            var reinstateRequest = new ReinstateMandateRequest
            {
                Id       = creationResult.Item.Id,
                Metadata = new Dictionary <string, string>
                {
                    ["Key7"] = "Value7",
                    ["Key8"] = "Value8",
                    ["Key9"] = "Value9",
                },
            };

            var reinstateResult = (await subject.ReinstateAsync(reinstateRequest));

            // then
            Assert.That(creationResult.Item, Is.Not.Null);
            Assert.That(creationResult.Item.Id, Is.Not.Null);
            Assert.That(creationResult.Item.CreatedAt, Is.Not.EqualTo(default(DateTimeOffset)));
            Assert.That(creationResult.Item.Links.Creditor, Is.EqualTo(_creditor.Id));
            Assert.That(creationResult.Item.Links.CustomerBankAccount, Is.EqualTo(_customerBankAccount.Id));
            Assert.That(creationResult.Item.Metadata, Is.EqualTo(createRequest.Metadata));
            Assert.That(creationResult.Item.NextPossibleChargeDate, Is.Not.Null.And.Not.EqualTo(default(DateTime)));
            Assert.That(creationResult.Item.Reference, Is.Not.Null.And.EqualTo(createRequest.Reference));
            Assert.That(creationResult.Item.Scheme, Is.EqualTo(createRequest.Scheme));
            Assert.That(creationResult.Item.Status, Is.Not.Null.And.Not.EqualTo(MandateStatus.Cancelled));

            Assert.That(cancellationResult.Item.Status, Is.EqualTo(MandateStatus.Cancelled));

            Assert.That(reinstateResult.Item.Status, Is.Not.Null.And.Not.EqualTo(MandateStatus.Cancelled));
        }