function createSubscription($intervalLength, $item) { // Common Set Up for API Credentials $merchantAuthentication = new AnetAPI\MerchantAuthenticationType(); $merchantAuthentication->setName(\SampleCode\Constants::MERCHANT_LOGIN_ID); $merchantAuthentication->setTransactionKey(\SampleCode\Constants::MERCHANT_TRANSACTION_KEY); $refId = 'ref' . time(); // Subscription Type Info $subscription = new AnetAPI\ARBSubscriptionType(); $subscription->setName("Sample Subscription"); $interval = new AnetAPI\PaymentScheduleType\IntervalAType(); $interval->setLength($intervalLength); $interval->setUnit("days"); $paymentSchedule = new AnetAPI\PaymentScheduleType(); $paymentSchedule->setInterval($interval); $paymentSchedule->setStartDate(new DateTime('2020-08-30')); $paymentSchedule->setTotalOccurrences("12"); $paymentSchedule->setTrialOccurrences("1"); $subscription->setPaymentSchedule($paymentSchedule); $subscription->setAmount(rand(1, 99999) / 12.0 * 12); $subscription->setTrialAmount("0.00"); $creditCard = new AnetAPI\CreditCardType(); $creditCard->setCardNumber("4111111111111111"); $creditCard->setExpirationDate("2020-12"); $payment = new AnetAPI\PaymentType(); $payment->setCreditCard($creditCard); $subscription->setPayment($payment); $billTo = new AnetAPI\NameAndAddressType(); $billTo->setFirstName("John"); $billTo->setLastName("Smith"); $subscription->setBillTo($billTo); $request = new AnetAPI\ARBCreateSubscriptionRequest(); $request->setmerchantAuthentication($merchantAuthentication); $request->setRefId($refId); $request->setSubscription($subscription); $controller = new AnetController\ARBCreateSubscriptionController($request); $response = $controller->executeWithApiResponse(\net\authorize\api\constants\ANetEnvironment::SANDBOX); if ($response != null && $response->getMessages()->getResultCode() == "Ok") { echo "SUCCESS: Subscription ID : " . $response->getSubscriptionId() . "\n"; } else { echo "ERROR : Invalid response\n"; $errorMessages = $response->getMessages()->getMessage(); echo "Response : " . $errorMessages[0]->getCode() . " " . $errorMessages[0]->getText() . "\n"; } return $response; }
function authToken($customerId = null) { if (!isset($customerId)) { $customerId = $this->customerId(); } $options = $this->options; // Common Set Up for API Credentials $merchantAuthentication = new AnetAPI\MerchantAuthenticationType(); $merchantAuthentication->setName($options['authname']); $merchantAuthentication->setTransactionKey($options['authkey']); $refId = 'ref' . time(); $setting = new AnetAPI\SettingType(); // 2do: fix domain name and path for iframe popup $setting->setSettingName("hostedProfileIFrameCommunicatorUrl"); $setting->setSettingValue(Q_Html::themedUrl('plugins/Assets/authnet_iframe_communicator.html')); $setting->setSettingName("hostedProfilePageBorderVisible"); $setting->setSettingValue("false"); $frequest = new AnetAPI\GetHostedProfilePageRequest(); $frequest->setMerchantAuthentication($merchantAuthentication); $frequest->setCustomerProfileId($customerId); $frequest->addToHostedProfileSettings($setting); $controller = new AnetController\GetHostedProfilePageController($frequest); $fresponse = $controller->executeWithApiResponse($options['server']); if (!isset($fresponse) or $fresponse->getMessages()->getResultCode() != "Ok") { $messages = $fresponse->getMessages()->getMessage(); $message = reset($messages); throw new Assets_Exception_InvalidResponse(array('response' => $message->getCode() . ' ' . $message->getText())); } return $fresponse->getToken(); }