Exemple #1
0
        public async void Example()
        {
#pragma warning disable 0168
            using (Client client = GetClient())
            {
                HostedCheckoutSpecificInput hostedCheckoutSpecificInput = new HostedCheckoutSpecificInput();
                hostedCheckoutSpecificInput.Locale  = "en_GB";
                hostedCheckoutSpecificInput.Variant = "testVariant";

                AmountOfMoney amountOfMoney = new AmountOfMoney();
                amountOfMoney.Amount       = 2345L;
                amountOfMoney.CurrencyCode = "USD";

                Address billingAddress = new Address();
                billingAddress.CountryCode = "US";

                Customer customer = new Customer();
                customer.BillingAddress = billingAddress;

                Order order = new Order();
                order.AmountOfMoney = amountOfMoney;
                order.Customer      = customer;

                CreateHostedCheckoutRequest body = new CreateHostedCheckoutRequest();
                body.HostedCheckoutSpecificInput = hostedCheckoutSpecificInput;
                body.Order = order;

                CreateHostedCheckoutResponse response = await client.Merchant("merchantId").Hostedcheckouts().Create(body);
            }
#pragma warning restore 0168
        }
        /// <summary>
        ///
        /// </summary>
        /// <param name="customer"></param>
        /// <param name="customerPayment"></param>
        /// <param name="returnUrl">The URL that the user will be redirected to after the payment process.</param>
        /// <returns></returns>
        public async Task <string> CreateHostedCheckoutAsync(Entities.Customer customer, Entities.CustomerPayment customerPayment, string returnUrl)
        {
            var result = string.Empty;

            using (Client client = GetClient())
            {
                var ingenicoSettings      = _configuration.GetSection("Ingenico");
                var merchantId            = ingenicoSettings.GetValue <string>("MerchantID");
                var hostedCheckoutVariant = ingenicoSettings.GetValue <string>("HostedCheckoutVariant");
                var currency = string.IsNullOrEmpty(customerPayment.Currency) ? ingenicoSettings.GetValue <string>("DefaultCurrency") : customerPayment.Currency;
                var locale   = string.IsNullOrEmpty(customer.Locale) ? ingenicoSettings.GetValue <string>("DefaultLocale") : customer.Locale;

                var hostedCheckoutSpecificInput = new HostedCheckoutSpecificInput
                {
                    IsRecurring           = false,
                    Locale                = locale,
                    Variant               = hostedCheckoutVariant,
                    ReturnCancelState     = true,
                    PaymentProductFilters = new PaymentProductFiltersHostedCheckout()
                    {
                        // Only allow certain payment methods
                        RestrictTo = new PaymentProductFilter
                        {
                            Products = PaymentProductIncludeIds
                        }
                    }
                };

                var amountOfMoney = new AmountOfMoney
                {
                    Amount       = ConvertToIngenicoAmount(customerPayment.Total == 0 ? 0.01m : customerPayment.Total), // Amount cannot be zero. Amount has to be at least 1 cent.
                    CurrencyCode = currency
                };

                var order = new Order
                {
                    Customer      = CreateIngenicoCustomer(customer),
                    AmountOfMoney = amountOfMoney,
                    References    = new OrderReferences
                    {
                        // Update Order with CustomerPayment Id as reference
                        MerchantReference = customerPayment.MerchantReference.ToString()
                    }
                };

                // Create Hosted Checkout for recurring monthly direct debit via credit card or Sepa direct debit payment methods for subscription purchase

                var cardPaymentMethodSpecificInput = new CardPaymentMethodSpecificInputBase
                {
                    Tokenize  = true,
                    Recurring = new CardRecurrenceDetails
                    {
                        RecurringPaymentSequenceIndicator = "first"
                    },
                    AuthorizationMode = "SALE",
                    RequiresApproval  = false
                };

                var sepaDirectDebitPaymentMethodSpecificInput = new SepaDirectDebitPaymentMethodSpecificInputBase()
                {
                    PaymentProduct771SpecificInput = new SepaDirectDebitPaymentProduct771SpecificInputBase()
                    {
                        Mandate = new CreateMandateBase()
                        {
                            CustomerReference = order.Customer.MerchantCustomerId,
                            RecurrenceType    = "RECURRING",
                            SignatureType     = "SMS"
                        },
                    }
                };

                var body = new CreateHostedCheckoutRequest
                {
                    HostedCheckoutSpecificInput = hostedCheckoutSpecificInput,
                    Order = order,
                    CardPaymentMethodSpecificInput            = cardPaymentMethodSpecificInput,
                    SepaDirectDebitPaymentMethodSpecificInput = sepaDirectDebitPaymentMethodSpecificInput
                };

                try
                {
                    // Initial call to Ingenico API to create hosted checkout.
                    CreateHostedCheckoutResponse createHostedCheckoutResponse = await client.Merchant(merchantId).Hostedcheckouts().Create(body);

                    if (createHostedCheckoutResponse.MerchantReference == customerPayment.MerchantReference.ToString())
                    {
                        // Do initial call to Ingenico API to retreive status of payment
                        GetHostedCheckoutResponse getHostedCheckoutResponse = await client.Merchant(merchantId).Hostedcheckouts().Get(createHostedCheckoutResponse.HostedCheckoutId);

                        // Update CustomerPayment record
                        customerPayment.Status = getHostedCheckoutResponse.Status;
                        //_customerPaymentRepository.Update(customerPayment);
                    }

                    // Build up redirect URL
                    var subdomain = ingenicoSettings.GetValue <string>("Subdomain");
                    result = $"{subdomain}{createHostedCheckoutResponse.PartialRedirectUrl}";
                }
                catch (Exception ex)
                {
                    //Logger.Error("Exception at : PaymentManager.CreateHostedCheckout\n", ex);

                    // Delete Customer Payment records, since initial payment failed
                    //_customerPaymentRepository.Delete(customerPayment.Id);
                }
            }

            return(result);
        }