Example #1
0
 static internal void ValidatesIntervalPeriodWithChargeDay(Interval.PeriodWithChargeDay 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
            })));
        }
 private async Task <Subscription> changeAmountAsync(Subscription subscription, int amount, int type, String currency, Interval.PeriodWithChargeDay interval)
 {
     if (currency != null)
     {
         ValidationUtils.ValidatesCurrency(currency);
     }
     if (interval != null)
     {
         ValidationUtils.ValidatesIntervalPeriodWithChargeDay(interval);
     }
     return(await updateParamAsync(subscription.Id, new {
         amount = amount,
         amount_change_type = type,
         currency = currency != null ? currency : null,
         interval = interval != null ? interval: null
     }));
 }
 /// <summary>
 /// Changes the amount of a subscription. The new amount is valid until the end of the subscription. If you want to set a
 /// temporary one-time amount use <see cref="SubscriptionService#changeAmountTemporary" />
 ///  </summary>
 ///
 /// <param name="subscription">
 ///          the subscription.</param>
 /// <param name="amount">
 ///          the new amount.</param>
 /// <param name="currency">
 ///          optionally, a new currency or null.</param>
 /// <param name="interval">
 ///          optionally, a new interval or null.</param>
 ///  <returns> the updated subscription.</returns>
 ///
 public async Task <Subscription> ChangeAmountAsync(Subscription subscription, int amount, String currency, Interval.PeriodWithChargeDay interval)
 {
     return(await changeAmountAsync(subscription, amount, 1, currency, interval));
 }
 /// <summary>
 /// Changes the amount of a subscription. The new amount is valid until the end of the subscription. If you want to set a
 /// temporary one-time amount use <see cref="SubscriptionService#changeAmountTemporary" />
 ///  </summary>
 ///
 /// <param name="subscriptionId">
 ///          the Id of the subscription.</param>
 /// <param name="amount">
 ///          the new amount.</param>
 /// <param name="currency">
 ///          optionally, a new currency or null.</param>
 /// <param name="interval">
 ///          optionally, a new interval or null.</param>
 ///  <returns> the updated subscription.</returns>
 ///
 public async Task <Subscription> ChangeAmountAsync(String subscriptionId, int amount, String currency, Interval.PeriodWithChargeDay interval)
 {
     return(await ChangeAmountAsync(new Subscription()
     {
         Id = subscriptionId
     }, amount, currency, interval));
 }
 /// <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));
 }