|
4 | 4 | use net\authorize\api\controller as AnetController; |
5 | 5 | define("AUTHORIZENET_LOG_FILE", "phplog"); |
6 | 6 | date_default_timezone_set('America/Los_Angeles'); |
7 | | - // Common Set Up for API Credentials |
8 | | - $merchantAuthentication = new AnetAPI\MerchantAuthenticationType(); |
9 | | - $merchantAuthentication->setName( "556KThWQ6vf2"); |
10 | | - $merchantAuthentication->setTransactionKey("9ac2932kQ7kN2Wzq"); |
| 7 | + |
| 8 | + function createSubscription($intervalLength) { |
11 | 9 |
|
12 | | - $refId = 'ref' . time(); |
| 10 | + // Common Set Up for API Credentials |
| 11 | + $merchantAuthentication = new AnetAPI\MerchantAuthenticationType(); |
| 12 | + $merchantAuthentication->setName( "556KThWQ6vf2"); |
| 13 | + $merchantAuthentication->setTransactionKey("9ac2932kQ7kN2Wzq"); |
13 | 14 |
|
14 | | - // Subscription Type Info |
15 | | - $subscription = new AnetAPI\ARBSubscriptionType(); |
16 | | - $subscription->setName("Sample Subscription"); |
| 15 | + $refId = 'ref' . time(); |
17 | 16 |
|
18 | | - $interval = new AnetAPI\PaymentScheduleType\IntervalAType(); |
19 | | - $interval->setLength("1"); |
20 | | - $interval->setUnit("months"); |
| 17 | + // Subscription Type Info |
| 18 | + $subscription = new AnetAPI\ARBSubscriptionType(); |
| 19 | + $subscription->setName("Sample Subscription"); |
21 | 20 |
|
22 | | - $paymentSchedule = new AnetAPI\PaymentScheduleType(); |
23 | | - $paymentSchedule->setInterval($interval); |
24 | | - $paymentSchedule->setStartDate(new DateTime('2020-08-30')); |
25 | | - $paymentSchedule->setTotalOccurrences("12"); |
26 | | - $paymentSchedule->setTrialOccurrences("1"); |
| 21 | + $interval = new AnetAPI\PaymentScheduleType\IntervalAType(); |
| 22 | + $interval->setLength($intervalLength); |
| 23 | + $interval->setUnit("days"); |
27 | 24 |
|
28 | | - $subscription->setPaymentSchedule($paymentSchedule); |
29 | | - $subscription->setAmount("10.29"); |
30 | | - $subscription->setTrialAmount("0.00"); |
31 | | - |
32 | | - $creditCard = new AnetAPI\CreditCardType(); |
33 | | - $creditCard->setCardNumber("4111111111111111"); |
34 | | - $creditCard->setExpirationDate("2020-12"); |
| 25 | + $paymentSchedule = new AnetAPI\PaymentScheduleType(); |
| 26 | + $paymentSchedule->setInterval($interval); |
| 27 | + $paymentSchedule->setStartDate(new DateTime('2020-08-30')); |
| 28 | + $paymentSchedule->setTotalOccurrences("12"); |
| 29 | + $paymentSchedule->setTrialOccurrences("1"); |
35 | 30 |
|
36 | | - $payment = new AnetAPI\PaymentType(); |
37 | | - $payment->setCreditCard($creditCard); |
| 31 | + $subscription->setPaymentSchedule($paymentSchedule); |
| 32 | + $subscription->setAmount("10.29"); |
| 33 | + $subscription->setTrialAmount("0.00"); |
| 34 | + |
| 35 | + $creditCard = new AnetAPI\CreditCardType(); |
| 36 | + $creditCard->setCardNumber("4111111111111111"); |
| 37 | + $creditCard->setExpirationDate("2020-12"); |
38 | 38 |
|
39 | | - $subscription->setPayment($payment); |
| 39 | + $payment = new AnetAPI\PaymentType(); |
| 40 | + $payment->setCreditCard($creditCard); |
40 | 41 |
|
41 | | - $billTo = new AnetAPI\NameAndAddressType(); |
42 | | - $billTo->setFirstName("John"); |
43 | | - $billTo->setLastName("Smith"); |
| 42 | + $subscription->setPayment($payment); |
44 | 43 |
|
45 | | - $subscription->setBillTo($billTo); |
| 44 | + $billTo = new AnetAPI\NameAndAddressType(); |
| 45 | + $billTo->setFirstName("John"); |
| 46 | + $billTo->setLastName("Smith"); |
46 | 47 |
|
47 | | - $request = new AnetAPI\ARBCreateSubscriptionRequest(); |
48 | | - $request->setmerchantAuthentication($merchantAuthentication); |
49 | | - $request->setRefId($refId); |
50 | | - $request->setSubscription($subscription); |
51 | | - $controller = new AnetController\ARBCreateSubscriptionController($request); |
| 48 | + $subscription->setBillTo($billTo); |
52 | 49 |
|
53 | | - $response = $controller->executeWithApiResponse( \net\authorize\api\constants\ANetEnvironment::SANDBOX); |
54 | | - |
55 | | - if (($response != null) && ($response->getMessages()->getResultCode() == "Ok") ) |
56 | | - { |
57 | | - echo "SUCCESS: Subscription ID : " . $response->getSubscriptionId() . "\n"; |
58 | | - } |
59 | | - else |
60 | | - { |
61 | | - echo "ERROR : Invalid response\n"; |
62 | | - echo "Response : " . $response->getMessages()->getMessage()[0]->getCode() . " " .$response->getMessages()->getMessage()[0]->getText() . "\n"; |
| 50 | + $request = new AnetAPI\ARBCreateSubscriptionRequest(); |
| 51 | + $request->setmerchantAuthentication($merchantAuthentication); |
| 52 | + $request->setRefId($refId); |
| 53 | + $request->setSubscription($subscription); |
| 54 | + $controller = new AnetController\ARBCreateSubscriptionController($request); |
| 55 | + |
| 56 | + $response = $controller->executeWithApiResponse( \net\authorize\api\constants\ANetEnvironment::SANDBOX); |
| 57 | + |
| 58 | + if (($response != null) && ($response->getMessages()->getResultCode() == "Ok") ) |
| 59 | + { |
| 60 | + echo "SUCCESS: Subscription ID : " . $response->getSubscriptionId() . "\n"; |
| 61 | + } |
| 62 | + else |
| 63 | + { |
| 64 | + echo "ERROR : Invalid response\n"; |
| 65 | + echo "Response : " . $response->getMessages()->getMessage()[0]->getCode() . " " .$response->getMessages()->getMessage()[0]->getText() . "\n"; |
| 66 | + } |
| 67 | + |
| 68 | + return $response; |
63 | 69 | } |
| 70 | + |
| 71 | + if(!defined(DONT_RUN_SAMPLES)) |
| 72 | + createSubscription(23); |
| 73 | + |
64 | 74 | ?> |
0 commit comments