Skip to content

Commit fb4356c

Browse files
author
akankari
committed
Adding sample codes + Tests for the Q1 API : create-subscription-from-customer-profile
Updated the sample code for create-customer-profile-from-transaction.py to incorporate the Q1 changes Reviewed by: Sunny
1 parent 295dcc4 commit fb4356c

File tree

6 files changed

+114
-5
lines changed

6 files changed

+114
-5
lines changed

CustomerProfiles/create-customer-payment-profile.py

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,9 +17,14 @@ def create_customer_payment_profile(customerProfileId):
1717
payment = apicontractsv1.paymentType()
1818
payment.creditCard = creditCard
1919

20+
billTo = apicontractsv1.customerAddressType()
21+
billTo.firstName = "John"
22+
billTo.lastName = "Snow"
23+
2024
profile = apicontractsv1.customerPaymentProfileType()
2125
profile.payment = payment
22-
26+
profile.billTo = billTo
27+
2328
createCustomerPaymentProfile = apicontractsv1.createCustomerPaymentProfileRequest()
2429
createCustomerPaymentProfile.merchantAuthentication = merchantAuth
2530
createCustomerPaymentProfile.paymentProfile = profile

CustomerProfiles/create-customer-profile-from-transaction.py

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,17 +10,27 @@ def create_customer_profile_from_transaction(transactionId):
1010
merchantAuth.name = constants.apiLoginId
1111
merchantAuth.transactionKey = constants.transactionKey
1212

13+
profile = apicontractsv1.customerProfileBaseType()
14+
profile.merchantCustomerId = "12332"
15+
profile.description = "This is a sample profile"
16+
profile.email = "[email protected]"
17+
1318
createCustomerProfileFromTransaction = apicontractsv1.createCustomerProfileFromTransactionRequest()
1419
createCustomerProfileFromTransaction.merchantAuthentication = merchantAuth
1520
createCustomerProfileFromTransaction.transId = transactionId
16-
21+
#You can either specify the customer information in form of customerProfileBaseType object
22+
createCustomerProfileFromTransaction.customer = profile
23+
# OR
24+
# You can just provide the customer Profile ID
25+
# createCustomerProfileFromTransaction.customerProfileId = "123343"
26+
1727
controller = createCustomerProfileFromTransactionController(createCustomerProfileFromTransaction)
1828
controller.execute()
1929

2030
response = controller.getresponse()
2131

2232
if (response.messages.resultCode=="Ok"):
23-
print "Successfully created a customer profile with id: %s from transaction id: %s" % {response.customerProfileId, createCustomerProfileFromTransaction.transId}
33+
print "Successfully created a customer profile with id: %s from transaction id: %s" % (response.customerProfileId, createCustomerProfileFromTransaction.transId)
2434
else:
2535
print "Failed to create customer payment profile from transaction %s" % response.messages.message[0].text
2636

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
import os, sys
2+
import imp
3+
4+
from authorizenet import apicontractsv1
5+
from authorizenet.apicontrollers import *
6+
constants = imp.load_source('modulename', 'constants.py')
7+
from decimal import *
8+
from datetime import *
9+
10+
def create_subscription_from_customer_profile(amount, days, profileId, paymentProfileId, customerAddressId):
11+
12+
# Setting the merchant details
13+
merchantAuth = apicontractsv1.merchantAuthenticationType()
14+
merchantAuth.name = constants.apiLoginId
15+
merchantAuth.transactionKey = constants.transactionKey
16+
# Setting payment schedule
17+
paymentschedule = apicontractsv1.paymentScheduleType()
18+
paymentschedule.interval = apicontractsv1.CTD_ANON()
19+
paymentschedule.interval.length = days
20+
paymentschedule.interval.unit = apicontractsv1.ARBSubscriptionUnitEnum.days
21+
paymentschedule.startDate = datetime(2020, 8, 30)
22+
paymentschedule.totalOccurrences = 12
23+
paymentschedule.trialOccurrences = 1
24+
25+
#setting the customer profile details
26+
profile = apicontractsv1.customerProfileIdType()
27+
profile.customerProfileId = profileId
28+
profile.customerPaymentProfileId = paymentProfileId
29+
profile.customerAddressId = customerAddressId
30+
31+
# Setting subscription details
32+
subscription = apicontractsv1.ARBSubscriptionType()
33+
subscription.name = "Sample Subscription"
34+
subscription.paymentSchedule = paymentschedule
35+
subscription.amount = amount
36+
subscription.trialAmount = Decimal('0.00')
37+
subscription.profile = profile
38+
39+
# Creating the request
40+
request = apicontractsv1.ARBCreateSubscriptionRequest()
41+
request.merchantAuthentication = merchantAuth
42+
request.subscription = subscription
43+
44+
# Creating and executing the controller
45+
controller = ARBCreateSubscriptionController(request)
46+
controller.execute()
47+
# Getting the response
48+
response = controller.getresponse()
49+
50+
if (response.messages.resultCode=="Ok"):
51+
print "SUCCESS:"
52+
print "Message Code : %s" % response.messages.message[0].code
53+
print "Message text : %s" % response.messages.message[0].text
54+
print "Subscription ID : %s" % response.subscriptionId
55+
else:
56+
print "ERROR:"
57+
print "Message Code : %s" % response.messages.message[0].code
58+
print "Message text : %s" % response.messages.message[0].text
59+
60+
return response
61+
62+
if(os.path.basename(__file__) == sys.argv[0].split('/')[-1]):
63+
create_subscription_from_customer_profile(constants.amount, constants.days, "247150", "215472", "189691")

constants.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,6 @@
55
customerProfileId = "123212"
66
customerPaymentProfileId = "2132322"
77
customerProfileShippingId = "123132"
8-
amount = 12.23
8+
amount = "12.23"
99
subscriptionId = "123234"
1010
days = 34

list_of_sample_codes.txt

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
SampleCode IsDependent RunApi
22
create_an_apple_pay_transaction 1 0
33
create_customer_payment_profile 1 1
4-
create_customer_profile_from_transaction 1 0
4+
create_customer_profile_from_transaction 1 1
55
create_customer_profile 1 1
66
create_customer_shipping_address 1 1
77
delete_customer_payment_profile 1 1
@@ -37,6 +37,7 @@ prior_authorization_capture 1 0
3737
void 1 0
3838
cancel_subscription 1 1
3939
create_subscription 1 1
40+
create_subscription_from_customer_profile 1 1
4041
get_list_of_subscription 1 1
4142
get_subscription_status 1 0
4243
get_subscription 1 1

test-runner.py

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -444,6 +444,36 @@ def create_subscription(self):
444444
modl = imp.load_source('modulename', 'RecurringBilling/create-subscription.py')
445445
return modl.create_subscription(self.getAmount(), self.getDay())
446446

447+
448+
def create_subscription_from_customer_profile(self):
449+
print("create_subscription_from_customer_profile")
450+
451+
#create customer profile
452+
modl = imp.load_source('modulename', 'CustomerProfiles/create-customer-profile.py')
453+
profileResponse = modl.create_customer_profile()
454+
455+
#create customer payment profile for that above profile
456+
modl = imp.load_source('modulename', 'CustomerProfiles/create-customer-payment-profile.py')
457+
paymentProfileResponse = modl.create_customer_payment_profile(profileResponse.customerProfileId)
458+
459+
#Create customer shipping address
460+
modl = imp.load_source('modulename', 'CustomerProfiles/create-customer-shipping-address.py')
461+
shippingResponse = modl.create_customer_shipping_address(profileResponse.customerProfileId)
462+
463+
#Create subscripiton from customer profile
464+
modl = imp.load_source('modulename', 'RecurringBilling/create-subscription-from-customer-profile.py')
465+
response = modl.create_subscription_from_customer_profile(self.getAmount(), self.getDay(), profileResponse.customerProfileId, paymentProfileResponse.customerPaymentProfileId, shippingResponse.customerAddressId)
466+
467+
#Cancel subscription
468+
modl = imp.load_source('modulename', 'RecurringBilling/cancel-subscription.py')
469+
modl.cancel_subscription(response.subscriptionId)
470+
471+
#delete newly create customer profile
472+
modl = imp.load_source('modulename', 'CustomerProfiles/delete-customer-profile.py')
473+
modl.delete_customer_profile(profileResponse.customerProfileId)
474+
475+
return response
476+
447477
def get_list_of_subscription(self):
448478
print("get_list_of_subscription")
449479

0 commit comments

Comments
 (0)