1+ package net .authorize .sample .RecurringBilling ;
2+
3+ import java .math .BigDecimal ;
4+
5+ import javax .xml .datatype .DatatypeFactory ;
6+ import javax .xml .datatype .XMLGregorianCalendar ;
7+
8+ import net .authorize .Environment ;
9+ import net .authorize .api .contract .v1 .ANetApiResponse ;
10+ import net .authorize .api .contract .v1 .ARBCreateSubscriptionRequest ;
11+ import net .authorize .api .contract .v1 .ARBCreateSubscriptionResponse ;
12+ import net .authorize .api .contract .v1 .ARBSubscriptionType ;
13+ import net .authorize .api .contract .v1 .ARBSubscriptionUnitEnum ;
14+ import net .authorize .api .contract .v1 .CustomerProfileIdType ;
15+ import net .authorize .api .contract .v1 .MerchantAuthenticationType ;
16+ import net .authorize .api .contract .v1 .MessageTypeEnum ;
17+ import net .authorize .api .contract .v1 .PaymentScheduleType ;
18+ import net .authorize .api .controller .ARBCreateSubscriptionController ;
19+ import net .authorize .api .controller .base .ApiOperationBase ;
20+
21+ public class CreateSubscriptionFromCustomerProfile {
22+
23+ public static ANetApiResponse run (String apiLoginId , String transactionKey , short intervalLength , Double amount ,
24+ String profileId , String paymentProfileId , String customerAddressId ) {
25+ //Common code to set for all requests
26+ ApiOperationBase .setEnvironment (Environment .SANDBOX );
27+ MerchantAuthenticationType merchantAuthenticationType = new MerchantAuthenticationType () ;
28+ merchantAuthenticationType .setName (apiLoginId );
29+ merchantAuthenticationType .setTransactionKey (transactionKey );
30+ ApiOperationBase .setMerchantAuthentication (merchantAuthenticationType );
31+
32+ // Set up payment schedule
33+ PaymentScheduleType schedule = new PaymentScheduleType ();
34+ PaymentScheduleType .Interval interval = new PaymentScheduleType .Interval ();
35+ interval .setLength (intervalLength );
36+ interval .setUnit (ARBSubscriptionUnitEnum .DAYS );
37+ schedule .setInterval (interval );
38+
39+ try {
40+ XMLGregorianCalendar startDate = DatatypeFactory .newInstance ().newXMLGregorianCalendar ();
41+ startDate .setDay (30 );
42+ startDate .setMonth (8 );
43+ startDate .setYear (2020 );
44+ schedule .setStartDate (startDate ); //2020-08-30
45+ }
46+ catch (Exception e ) { }
47+
48+ schedule .setTotalOccurrences ((short )12 );
49+ schedule .setTrialOccurrences ((short )1 );
50+
51+ CustomerProfileIdType profile = new CustomerProfileIdType ();
52+ profile .setCustomerProfileId (profileId );
53+ profile .setCustomerPaymentProfileId (paymentProfileId );
54+ profile .setCustomerAddressId (customerAddressId );
55+
56+ ARBSubscriptionType arbSubscriptionType = new ARBSubscriptionType ();
57+ arbSubscriptionType .setPaymentSchedule (schedule );
58+ arbSubscriptionType .setAmount (new BigDecimal ("10.29" ));
59+ arbSubscriptionType .setTrialAmount (new BigDecimal ("0.00" ));
60+ arbSubscriptionType .setProfile (profile );
61+
62+ // Make the API Request
63+ ARBCreateSubscriptionRequest apiRequest = new ARBCreateSubscriptionRequest ();
64+ apiRequest .setSubscription (arbSubscriptionType );
65+ ARBCreateSubscriptionController controller = new ARBCreateSubscriptionController (apiRequest );
66+ controller .execute ();
67+ ARBCreateSubscriptionResponse response = controller .getApiResponse ();
68+ if (response !=null ) {
69+
70+ if (response .getMessages ().getResultCode () == MessageTypeEnum .OK ) {
71+
72+ System .out .println (response .getSubscriptionId ());
73+ System .out .println (response .getMessages ().getMessage ().get (0 ).getCode ());
74+ System .out .println (response .getMessages ().getMessage ().get (0 ).getText ());
75+ }
76+ else
77+ {
78+ System .out .println ("Failed to create Subscription: " + response .getMessages ().getResultCode ());
79+ System .out .println (response .getMessages ().getMessage ().get (0 ).getText ());
80+ }
81+ }
82+
83+ return response ;
84+ }
85+ }
0 commit comments