/*
 * Copyright 2014-2019 The Billing Project, LLC
 *
 * The Billing Project licenses this file to you under the Apache License, version 2.0
 * (the "License"); you may not use this file except in compliance with the
 * License.  You may obtain a copy of the License at:
 *
 *    http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.  See the
 * License for the specific language governing permissions and limitations
 * under the License.
 */

package org.killbill.billing.plugin.stripe;

import java.util.UUID;

import javax.inject.Named;
import javax.inject.Singleton;

import org.jooby.MediaType;
import org.jooby.Result;
import org.jooby.Results;
import org.jooby.Status;
import org.jooby.mvc.Local;
import org.jooby.mvc.POST;
import org.jooby.mvc.Path;
import org.killbill.billing.osgi.libs.killbill.OSGIKillbillClock;
import org.killbill.billing.payment.plugin.api.HostedPaymentPageFormDescriptor;
import org.killbill.billing.payment.plugin.api.PaymentPluginApiException;
import org.killbill.billing.plugin.api.PluginCallContext;
import org.killbill.billing.plugin.core.resources.PluginHealthcheck;
import org.killbill.billing.tenant.api.Tenant;
import org.killbill.billing.util.callcontext.CallContext;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.google.common.collect.ImmutableList;
import com.google.inject.Inject;

@Singleton
// Handle /plugins/killbill-stripe/checkout
@Path("/checkout")
public class StripeCheckoutServlet extends PluginHealthcheck {

    private final OSGIKillbillClock clock;
    private final StripePaymentPluginApi stripePaymentPluginApi;

    @Inject
    public StripeCheckoutServlet(final OSGIKillbillClock clock,
                                 final StripePaymentPluginApi stripePaymentPluginApi) {
        this.clock = clock;
        this.stripePaymentPluginApi = stripePaymentPluginApi;
    }

    @POST
    public Result createSession(@Named("kbAccountId") final UUID kbAccountId,
                                @Local @Named("killbill_tenant") final Tenant tenant) throws JsonProcessingException, PaymentPluginApiException {
        final CallContext context = new PluginCallContext(StripeActivator.PLUGIN_NAME, clock.getClock().getUTCNow(), kbAccountId, tenant.getId());
        final HostedPaymentPageFormDescriptor hostedPaymentPageFormDescriptor = stripePaymentPluginApi.buildFormDescriptor(kbAccountId,
                                                                                                                           ImmutableList.of(),
                                                                                                                           ImmutableList.of(),
                                                                                                                           context);
        return Results.with(hostedPaymentPageFormDescriptor, Status.CREATED)
                      .type(MediaType.json);
    }
}

@@ -691,16 +691,43 @@
 tripe;%0A%0A
+import java.util.Optional;%0A
 import j
@@ -1049,24 +1049,80 @@
 lbillClock;%0A
+import org.killbill.billing.payment.api.PluginProperty;%0A
 import org.k
@@ -2225,32 +2225,120 @@
 ID kbAccountId,%0A
+                                @Named(%22successUrl%22) final Optional%3CString%3E successUrl,%0A
                 
@@ -2614,16 +2614,185 @@
 tId());%0A
+        final ImmutableList%3CPluginProperty%3E customFields = ImmutableList.of(new PluginProperty(%22success_url%22, successUrl.orElse(%22https://example.com/success%22), false));%0A
         
@@ -3042,34 +3042,28 @@
         
-ImmutableList.of()
+customFields
 ,%0A      

make success url configurable