public void Initialize()
 {
     base.Initialize();
     interval     = Interval.period(1, Interval.TypeUnit.MONTH);
     this.payment = _paymill.PaymentService.CreateWithTokenAsync(testToken).Result;
     this.offer1  = _paymill.OfferService.CreateAsync(this.amount, this.currency, this.interval, this.name).Result;
     this.offer2  = _paymill.OfferService.CreateAsync(this.amount * 2, this.currency, this.interval, "Updated " + this.name).Result;
 }
 static internal void ValidatesIntervalPeriod(Interval.Period interval)
 {
     if (interval.Interval < 1)
     {
         throw new ArgumentException("Interval must be greater than zero");
     }
     if (interval.Unit == null)
     {
         throw new ArgumentException("Interval unit cannot be null");
     }
 }
        /// <summary>
        /// This function creates a <see cref="Subscription" /> between a  <see cref="Client" /> and an  <see cref="Offer" />. A  <see cref="Client" /> can have several
        ///  <see cref="Subscription" />s to different <see cref="Offer" />s, but only one <see cref="Subscription" /> to the same <see cref="Offer" />. The
        ///  <see cref="Client" />s is charged for each billing interval entered.
        /// NOTE
        ///
        ///
        /// As the Subscription create method has a lot of options, we recommend you to use a Subscription.Creator.
        /// <param name="payment">
        ///          A Payment used for charging.</param>
        /// <param name="client"></param>
        /// <param name="offer">
        ///          An Offer to subscribe to. Mandatory only if amount, curreny and interval are not set</param>
        /// <param name="amount">
        ///          Amount to be charged. Mandatory if offer is null.</param>
        /// <param name="currency">
        ///          Currency in which to be charged. Mandatory if offer is null.</param>
        /// <param name="interval">
        ///          Interval of charging. Mandatory if offer is null.</param>
        /// <param name="startAt">
        ///          The date, when the subscription will start charging. If longer than 10 minutes in the future, a preauthorization
        ///          will occur automatically to verify the payment.</param>
        /// <param name="name">
        ///          A name for this subscription</param>
        /// <param name="periodOfValidity">
        ///          if set, the subscription will expire after this period.</param>
        /// <returns>the subscription.</returns>
        ///
        public async Task <Subscription> CreateAsync(Payment payment, Client client, Offer offer, int?amount, String currency, Interval.PeriodWithChargeDay interval, DateTime?startAt,
                                                     String name, Interval.Period periodOfValidity, String mandateReference)
        {
            if (offer == null && (amount == null || currency == null || interval == null))
            {
                throw new ArgumentException("Either an offer or amount, currency and interval must be set, when creating a subscription");
            }

            ValidationUtils.ValidatesPayment(payment);
            if (client != null)
            {
                ValidationUtils.ValidatesClient(client);
            }
            if (offer != null)
            {
                ValidationUtils.ValidatesOffer(offer);
            }
            if (amount != null)
            {
                ValidationUtils.ValidatesAmount(amount);
            }
            if (currency != null)
            {
                ValidationUtils.ValidatesCurrency(currency);
            }
            if (interval != null)
            {
                ValidationUtils.ValidatesIntervalPeriodWithChargeDay(interval);
            }
            if (periodOfValidity != null)
            {
                ValidationUtils.ValidatesIntervalPeriod(periodOfValidity);
            }
            return(await createAsync(null,
                                     new UrlEncoder().EncodeObject(new
            {
                payment = payment.Id,
                client = client != null ? client.Id : null,
                offer = offer != null ? offer.Id : null,
                amount = amount.HasValue ? amount.Value.ToString() : null,
                currency = currency != null ? currency : null,
                interval = interval != null ? interval.ToString() : null,
                start_at = startAt.HasValue ? DateTimeExtensions.ToUnixTimestamp(startAt.Value).ToString() : null,
                name = name != null ? name : null,
                period_of_validity = periodOfValidity != null ? periodOfValidity.ToString() : null,
                mandate_reference = mandateReference != null ? mandateReference: null
            })));
        }
        /// <summary>
        /// Creates an offer via the API.
        /// </summary>
        /// <param name="amount">Amount in cents > 0.</param>
        /// <param name="currency">ISO 4217 formatted currency code.</param>
        /// <param name="interval">Defining how often the Client should be charged.</param>
        /// <param name="name">Your name for this offer</param>
        /// <param name="trialPeriodDays">Give it a try or charge directly. Can be null</param>
        /// <returns>Object with id, which represents a PAYMILL offer</returns>
        public async Task <Offer> CreateAsync(int amount, String currency, Interval.Period interval, String name, int?trialPeriodDays)
        {
            ValidationUtils.ValidatesAmount(amount);
            ValidationUtils.ValidatesCurrency(currency);
            ValidationUtils.ValidatesIntervalPeriod(interval);
            ValidationUtils.ValidatesName(name);
            ValidationUtils.ValidatesTrialPeriodDays(trialPeriodDays);

            return(await createAsync(null,
                                     new UrlEncoder().EncodeObject(new
            {
                Amount = amount,
                Currency = currency,
                Interval = interval.ToString(),
                Name = name,
                Trial_Period_Days = trialPeriodDays
            })));
        }
 /// <summary>
 /// Change the period of validity for a subscription.
 ///  </summary>
 ///
 /// <param name="subscription">
 ///          the subscription.</param>
 /// <param name="newValidity">
 ///          the new validity.</param>
 ///  <returns> the updated subscription.</returns>
 ///
 public async Task <Subscription> LimitValidityAsync(Subscription subscription, Interval.Period newValidity)
 {
     ValidationUtils.ValidatesIntervalPeriod(newValidity);
     return(await updateParamAsync(subscription.Id, new { period_of_validity = newValidity.ToString() }));
 }
 /// <summary>
 /// This function creates a<see cref="Subscription" /> between a <see cref="Client" /> and an <see cref="Offer" />. A <see cref="Client" /> can have several
 /// <see cref="Subscription" />s to different <see cref="Offer" />s, but only one <see cref="Subscription" /> to the same <see cref="Offer" />. The
 /// <see cref="Client" />s is charged for each billing interval entered.
 /// </summary>
 ///
 /// NOTE
 /// As the Subscription create method has a lot of options, we recommend you to use a <see cref="Subscription.Creator" />.
 ///
 /// <param name="paymentId">
 ///          A Payment used for charging.</param>
 /// <param name="clientId"></param>
 /// <param name="offerId">
 ///          An link Offer to subscribe to. Mandatory only if amount, curreny and interval are not set</param>
 /// <param name="amount">
 ///          Amount to be charged. Mandatory if offer is null.</param>
 /// <param name="currency">
 ///          Currency in which to be charged. Mandatory if offer is null.</param>
 /// <param name="interval">
 ///          Interval of charging. Mandatory if offer is null.</param>
 /// <param name="startAt">
 ///          The date, when the subscription will start charging. If longer than 10 minutes in the future, a preauthorization
 ///          will occur automatically to verify the payment.</param>
 /// <param name="name">
 ///          A name for this subscription</param>
 /// <param name="periodOfValidity">
 ///          if set, the subscription will expire after this period.</param>
 /// <returns>the subscription</returns>
 ///
 public async Task <Subscription> CreateAsync(String paymentId, String clientId, String offerId, int?amount, String currency, Interval.PeriodWithChargeDay interval,
                                              DateTime?startAt, String name, Interval.Period periodOfValidity)
 {
     return(await CreateAsync(new Payment(paymentId), new Client(clientId), new Offer(offerId), amount, currency, interval, startAt, name, periodOfValidity, null));
 }
 /// <summary>
 /// Creates an offer via the API.
 /// </summary>
 /// <param name="amount">Amount in cents > 0.</param>
 /// <param name="currency">ISO 4217 formatted currency code.</param>
 /// <param name="interval">Defining how often the Client should be charged</param>
 /// <param name="name">Your name for this offer</param>
 /// <returns>Object with id, which represents a PAYMILL offer.</returns>
 public async Task <Offer> CreateAsync(int amount, String currency, Interval.Period interval, String name)
 {
     return(await CreateAsync(amount, currency, interval, name, null));
 }