/// <summary>
        /// Initializes a new instance of the <see cref="AuthorizePayment"/> class.
        /// </summary>
        /// <param name="paymentGateway">The payment gateway to use for authorization.</param>
        /// <param name="amountToCharge">The amount to charge.</param>
        public AuthorizePayment(IPaymentGateway paymentGateway, decimal amountToCharge)
        {
            paymentGateway.AssertNotNull(nameof(paymentGateway));
            amountToCharge.AssertPositive(nameof(amountToCharge));

            this.Amount         = amountToCharge;
            this.PaymentGateway = paymentGateway;
        }
        /// <summary>
        /// Initializes a new instance of the <see cref="TransactionResultLineItem"/> class.
        /// </summary>
        /// <param name="subscriptionId">The subscription ID related to the transaction line item.</param>
        /// <param name="partnerOfferId">The partner offer ID related to the transaction line item.</param>
        /// <param name="quantity">The quantity purchased.</param>
        /// <param name="seatPrice">The price charged per seat.</param>
        /// <param name="amountCharged">The total amount charged.</param>
        public TransactionResultLineItem(string subscriptionId, string partnerOfferId, int quantity, decimal seatPrice, decimal amountCharged)
        {
            // we don't validate amount charged since a transaction may result in a negative amount
            subscriptionId.AssertNotEmpty(nameof(subscriptionId));
            partnerOfferId.AssertNotEmpty(nameof(partnerOfferId));
            quantity.AssertPositive(nameof(quantity));
            seatPrice.AssertPositive(nameof(seatPrice));

            this.SubscriptionId = subscriptionId;
            this.PartnerOfferId = partnerOfferId;
            this.Quantity       = quantity;
            this.SeatPrice      = seatPrice;
            this.AmountCharged  = amountCharged;
        }
Esempio n. 3
0
        /// <summary>
        /// Initializes a new instance of the <see cref="CustomerPurchaseEntity"/> class.
        /// </summary>
        /// <param name="purchaseType">The purchase type.</param>
        /// <param name="id">Purchase ID.</param>
        /// <param name="customerId">The ID  of the customer who made the purchase.</param>
        /// <param name="subscriptionId">The subscription ID which the purchase applied to.</param>
        /// <param name="seatsBought">The number of seats bought in the purchase.</param>
        /// <param name="seatPrice">The seat price charged for the purchase.</param>
        /// <param name="transactionDate">The transaction date of the purchase.</param>
        public CustomerPurchaseEntity(CommerceOperationType purchaseType, string id, string customerId, string subscriptionId, int seatsBought, decimal seatPrice, DateTime transactionDate)
        {
            id.AssertNotEmpty(nameof(id));
            customerId.AssertNotEmpty(nameof(customerId));
            subscriptionId.AssertNotEmpty(nameof(subscriptionId));
            seatsBought.AssertPositive(nameof(seatsBought));
            seatPrice.AssertPositive(nameof(seatPrice));

            this.PurchaseType    = purchaseType;
            this.Id              = id;
            this.CustomerId      = customerId;
            this.SubscriptionId  = subscriptionId;
            this.SeatsBought     = seatsBought;
            this.SeatPrice       = seatPrice;
            this.TransactionDate = transactionDate;
        }
        /// <summary>
        /// Authorizes a payment with PayPal. Ensures the payment is valid.
        /// </summary>
        /// <param name="amount">The amount to charge.</param>
        /// <returns>An authorization code.</returns>
        public async Task <string> AuthorizeAsync(decimal amount)
        {
            amount.AssertPositive(nameof(amount));

            CreditCard customerCard = new CreditCard();

            customerCard.first_name   = this.creditCard.CardHolderFirstName;
            customerCard.last_name    = this.creditCard.CardHolderLastName;
            customerCard.expire_month = this.creditCard.CreditCardExpiryMonth;
            customerCard.expire_year  = this.creditCard.CreditCardExpiryYear;
            customerCard.number       = this.creditCard.CreditCardNumber;
            customerCard.type         = this.creditCard.CreditCardType;
            customerCard.cvv2         = this.creditCard.CreditCardCvn;

            //// PayPal expects the following in the amount (string)input during authorization...
            //// There are a few currencies where decimals are not supported. For future consideration.
            //// Currency amount must be
            ////      non - negative number,
            ////      may optionally contain exactly 2 decimal places separated by '.',
            ////      optional thousands separator ',',
            ////      limited to 7 digits before the decimal point],
            ////      Conversion needs to be done since Commerce layer treats it as a decimal while PayPal expects a string.
            ////      "12.0"(ie - without the additional zero...expected would be "12.00"). https://msdn.microsoft.com/en-us/library/dwhawy9k(v=vs.110).aspx#FFormatString

            string currency = this.ApplicationDomain.PortalLocalization.CurrencyCode;
            string orderAmt = amount.ToString("F", CultureInfo.InvariantCulture);

            // Create Payment Object.
            var payment = new Payment()
            {
                // Intent is to Authroize this Payment.
                intent = "authorize",                           // required
                // Payer Object (Required)
                // A resource representing a Payer that funds a payment. Use the List of `FundingInstrument` and the Payment Method as 'credit_card'
                payer = new Payer()
                {
                    payment_method = "credit_card",              // required
                    // funding instruments object [].
                    // The Payment creation API requires a list of
                    // FundingInstrument; add the created `FundingInstrument` to a List
                    funding_instruments = new List <FundingInstrument>()
                    {
                        // A resource representing a Payeer's funding instrument. and the `CreditCardDetails`
                        new FundingInstrument()
                        {
                            // A resource representing a credit card that can be used to fund a payment.
                            credit_card = customerCard
                        }
                    },
                },

                // transactions object []. (Required)
                // The Payment creation API requires a list of transactions; add the created `Transaction` to a List
                transactions = new List <Transaction>()
                {
                    // A transaction defines the contract of a payment - what is the payment for and who is fulfilling it. Transaction is created with a `Payee` and `Amount` types
                    new Transaction()
                    {
                        // Let's you specify a payment amount.
                        amount = new Amount()
                        {
                            currency = currency,    // Required
                            total    = orderAmt     // Required
                        },
                        description = this.paymentDescription
                    }
                }
            };

            // Create a payment by posting to the APIService using a valid APIContext
            // Retrieve an Authorization Id by making a Payment with intent as 'authorize' and parsing through the Payment object
            Payment createdPayment = null;

            try
            {
                createdPayment = payment.Create(await this.GetAPIContext());
            }
            catch (PayPalException ex)
            {
                this.ParsePayPalException(ex);
            }

            return(await Task.FromResult(createdPayment.transactions[0].related_resources[0].authorization.id));
        }