Skip to content

Commit 5da3c9e

Browse files
author
srathod
committed
- Added sample code for create-subscription-from-customer-profile.
- Updated test-runner.
1 parent a6d4510 commit 5da3c9e

File tree

4 files changed

+94
-1
lines changed

4 files changed

+94
-1
lines changed

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

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,10 +11,20 @@ function createCustomerProfileFromTransaction($transId= \SampleCode\Constants::T
1111
$merchantAuthentication = new AnetAPI\MerchantAuthenticationType();
1212
$merchantAuthentication->setName(\SampleCode\Constants::MERCHANT_LOGIN_ID);
1313
$merchantAuthentication->setTransactionKey(\SampleCode\Constants::MERCHANT_TRANSACTION_KEY);
14+
15+
$customerProfile = new AnetAPI\CustomerProfileBaseType();
16+
$customerProfile->setMerchantCustomerId("123212");
17+
$customerProfile->setEmail(rand(0,10000) . "@test" .".com");
18+
$customerProfile->setDesciption(rand(0,10000) ."sample description");
1419

1520
$request = new AnetAPI\CreateCustomerProfileFromTransactionRequest();
1621
$request->setMerchantAuthentication($merchantAuthentication);
1722
$request->setTransId($transId);
23+
// You can either specify the customer information in form of customerProfileBaseType object
24+
$request->setCustomer($customerProfile);
25+
// OR
26+
// You can just provide the customer Profile ID
27+
//$request->setCustomerProfileId("123343");
1828

1929
$controller = new AnetController\CreateCustomerProfileFromTransactionController($request);
2030

@@ -23,7 +33,7 @@ function createCustomerProfileFromTransaction($transId= \SampleCode\Constants::T
2333
if (($response != null) && ($response->getMessages()->getResultCode() == "Ok") )
2434
{
2535
echo "SUCCESS: PROFILE ID : " . $response->getCustomerProfileId() . "\n";
26-
}
36+
}
2737
else
2838
{
2939
echo "ERROR : Invalid response\n";
Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
<?php
2+
require 'vendor/autoload.php';
3+
use net\authorize\api\contract\v1 as AnetAPI;
4+
use net\authorize\api\controller as AnetController;
5+
date_default_timezone_set('America/Los_Angeles');
6+
7+
define("AUTHORIZENET_LOG_FILE", "phplog");
8+
9+
function createSubscriptionFromCustomerProfile($intervalLength, $customerProfileId, $customerPaymentProfileId, $customerAddressId) {
10+
11+
// Common Set Up for API Credentials
12+
$merchantAuthentication = new AnetAPI\MerchantAuthenticationType();
13+
$merchantAuthentication->setName(\SampleCode\Constants::MERCHANT_LOGIN_ID);
14+
$merchantAuthentication->setTransactionKey(\SampleCode\Constants::MERCHANT_TRANSACTION_KEY);
15+
16+
$refId = 'ref' . time();
17+
18+
// Subscription Type Info
19+
$subscription = new AnetAPI\ARBSubscriptionType();
20+
$subscription->setName("Sample Subscription");
21+
22+
$interval = new AnetAPI\PaymentScheduleType\IntervalAType();
23+
$interval->setLength($intervalLength);
24+
$interval->setUnit("days");
25+
26+
$paymentSchedule = new AnetAPI\PaymentScheduleType();
27+
$paymentSchedule->setInterval($interval);
28+
$paymentSchedule->setStartDate(new DateTime('2020-08-30'));
29+
$paymentSchedule->setTotalOccurrences("12");
30+
$paymentSchedule->setTrialOccurrences("1");
31+
32+
$subscription->setPaymentSchedule($paymentSchedule);
33+
$subscription->setAmount(rand(1,99999)/12.0*12);
34+
$subscription->setTrialAmount("0.00");
35+
36+
$profile = new AnetAPI\CustomerProfileIdType();
37+
$profile->setCustomerProfileId($customerProfileId);
38+
$profile->setCustomerPaymentProfileId($customerPaymentProfileId);
39+
$profile->setCustomerAddressId($customerAddressId);
40+
41+
$subscription->setProfile($profile);
42+
43+
$request = new AnetAPI\ARBCreateSubscriptionRequest();
44+
$request->setmerchantAuthentication($merchantAuthentication);
45+
$request->setRefId($refId);
46+
$request->setSubscription($subscription);
47+
$controller = new AnetController\ARBCreateSubscriptionController($request);
48+
49+
$response = $controller->executeWithApiResponse( \net\authorize\api\constants\ANetEnvironment::SANDBOX);
50+
51+
if (($response != null) && ($response->getMessages()->getResultCode() == "Ok") )
52+
{
53+
echo "SUCCESS: Subscription ID : " . $response->getSubscriptionId() . "\n";
54+
}
55+
else
56+
{
57+
echo "ERROR : Invalid response\n";
58+
echo "Response : " . $response->getMessages()->getMessage()[0]->getCode() . " " .$response->getMessages()->getMessage()[0]->getText() . "\n";
59+
}
60+
61+
return $response;
62+
}
63+
64+
if(!defined('DONT_RUN_SAMPLES'))
65+
createSubscriptionFromCustomerProfile( \SampleCode\Constants::SUBSCRIPTION_INTERVAL_DAYS, "247150", "215472", "189691");
66+
67+
?>

SampleCodeList.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
SampleCodeName,IsDependent,ShouldRun
22
ChargeCreditCard,1,1
33
CreateSubscription,1,1
4+
CreateSubscriptionFromCustomerProfile,1,1
45
GetCustomerPaymentProfileList,0,1
56
GetTransactionList,0,1
67
CreateAnApplePayTransaction,0,0

test-runner.php

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -227,6 +227,21 @@ public static function runCreateSubscription()
227227
return $response;
228228
}
229229

230+
public static function runCreateSubscriptionFromCustomerProfile()
231+
{
232+
$responseCustomerProfile = createCustomerProfile(self::getEmail());
233+
$responseCustomerPaymentProfile = createCustomerPaymentProfile($responseCustomerProfile->getCustomerProfileId(), self::getPhoneNumber());
234+
$responseCustomerShippingAddress = createCustomerShippingAddress($responseCustomerProfile->getCustomerProfileId(), self::getPhoneNumber());
235+
236+
$response = createSubscription(self::getDay(), $responseCustomerProfile->getCustomerProfileId(),
237+
$responseCustomerPaymentProfile->getCustomerPaymentProfileId(), $responseCustomerShippingAddress->getCustomerAddressId());
238+
239+
cancelSubscription($response->getSubscriptionId());
240+
deleteCustomerProfile($responseCustomerProfile->getCustomerProfileId());
241+
242+
return $response;
243+
}
244+
230245
public static function runCancelSubscription()
231246
{
232247
$response = createSubscription(self::getDay());

0 commit comments

Comments
 (0)