// Before working with this sample code, please be sure to read the accompanying Readme.txt file.
// It contains important information regarding the appropriate use of and conditions for this
// sample code. Also, please pay particular attention to the comments included in each individual
// code file, as they will assist you in the unique and correct implementation of this code on
// your specific platform.
//
// Copyright 2008 Authorize.Net Corp.


import net.authorize.arb.*;

public class CreateSubscriptionExample{
	
	public static void main(String args[]){
		
		if(args.length < 3){
			System.out.println("Authorize.Net ARB API Create Subscription Java Example");
			System.out.println("\tSyntax: CreateSubscriptionExample {api-url} {user-login} {transaction-key}");
			System.out.println("\tExample: CreateSubscriptionExample https://apitest.authorize.net/xml/v1/request.api YourUserLogin YourTranactionKey");
			System.exit(0);
		}

		java.net.URL url = null;
		try{
			if(args.length > 0) url = new java.net.URL(args[0]);
		}
		catch(java.net.MalformedURLException mue){
			System.out.println(mue);
		}
		if(url == null){
			System.out.println("Invalid url");
			System.exit(0);
		}
		
		// API constructor - url, merchant id, token key
		//
		ARBAPI api = new ARBAPI(url, args[1], args[2]);
		
	
		// Create a payment schedule
		//
		ARBPaymentSchedule new_schedule = new ARBPaymentSchedule();
		new_schedule.setIntervalLength(1);
		new_schedule.setSubscriptionUnit("months");
		new_schedule.setStartDate("2019-01-01");
		new_schedule.setTotalOccurrences(7);
		new_schedule.setTrialOccurrences(0);
	
		// Create a new credit card
		//
		CreditCard credit_card = new CreditCard();
		credit_card.setCardNumber("4111111111111111");
		credit_card.setExpirationDate("2029-07");
	
		// Create a billing info
		//
		ARBNameAndAddress billing_info = new ARBNameAndAddress();
		billing_info.setFirstName("John");
		billing_info.setLastName("Doe");
		
		// Create a customer and specify billing info
		//
		ARBCustomer customer = new ARBCustomer();
		customer.setBillTo(billing_info);
		
		// Create a subscription and specify payment, schedule and customer
		//
		ARBSubscription new_subscription = new ARBSubscription();
		new_subscription.setPayment(new ARBPayment(credit_card));
		new_subscription.setSchedule(new_schedule);
		new_subscription.setCustomer(customer);
		new_subscription.setAmount(6.00);
		new_subscription.setTrialAmount(0.00);

		// Give this subscription a name
		//
		new_subscription.setName("Demo Subscription 6");
		
		// Create a new subscription request from the subscription object
		// Returns XML document. Also holds internal pointer as current_request.
		//
		api.createSubscriptionRequest(new_subscription);
		// System.out.println(api.getCurrentRequest().dump());
		api.sendRequest();
		
		api.printMessages();
		
		api.destroy();
		
	}
}
