|
| 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 | +?> |
0 commit comments