Created
June 20, 2020 12:04
-
-
Save slothentic/d467d0455d8f0b846437d6f65f7958c0 to your computer and use it in GitHub Desktop.
Authnet example
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
use net\authorize\api\contract\v1 as AnetAPI; | |
use net\authorize\api\controller as AnetController; | |
$acceptJsCode = $this->request->get('code'); | |
$apiLoginId = "xxxx"; | |
$transactionKey = "xxx"; | |
$environmentHandle = \net\authorize\api\constants\ANetEnvironment::PRODUCTION; | |
$merchantAuthentication = new AnetAPI\MerchantAuthenticationType(); | |
$merchantAuthentication->setName($apiLoginId); | |
$merchantAuthentication->setTransactionKey($transactionKey); | |
// Set the transaction's refId | |
$refId = 'ref' . time(); | |
// Subscription Type Info | |
$subscription = new AnetAPI\ARBSubscriptionType(); | |
$subscription->setName("Subscription"); | |
$interval = new AnetAPI\PaymentScheduleType\IntervalAType(); | |
$interval->setLength(1); | |
$interval->setUnit("months"); | |
$paymentSchedule = new AnetAPI\PaymentScheduleType(); | |
$paymentSchedule->setInterval($interval); | |
$paymentSchedule->setStartDate(new \DateTime("now +5 minutes", new \DateTimeZone('America/Denver'))); | |
$paymentSchedule->setTotalOccurrences("9999"); | |
$subscription->setPaymentSchedule($paymentSchedule); | |
$subscription->setAmount(50.00); | |
$opaqueData = new AnetAPI\OpaqueDataType(); | |
$opaqueData->setDataDescriptor("COMMON.ACCEPT.INAPP.PAYMENT"); | |
$opaqueData->setDataValue($acceptJsCode); | |
$paymentType = new AnetAPI\PaymentType(); | |
$paymentType->setOpaqueData($opaqueData); | |
$subscription->setPayment($paymentType); | |
$order = new AnetAPI\OrderType(); | |
$order->setInvoiceNumber("1234"); | |
$order->setDescription("The order"); | |
$subscription->setOrder($order); | |
$billTo = new AnetAPI\NameAndAddressType(); | |
$billTo->setFirstName("Test"); | |
$billTo->setLastName("Subscriber"); | |
$subscription->setBillTo($billTo); | |
$customer = new AnetAPI\CustomerType(); | |
$customer->setId(1234); | |
$subscription->setCustomer($customer); | |
$request = new AnetAPI\ARBCreateSubscriptionRequest(); | |
$request->setmerchantAuthentication($merchantAuthentication); | |
$request->setRefId($refId); | |
$request->setSubscription($subscription); | |
$controller = new AnetController\ARBCreateSubscriptionController($request); | |
$response = $controller->executeWithApiResponse($environmentHandle); | |
if ($response != null && $response->getMessages()->getResultCode() == "Ok") { | |
$subscriptionId = $response->getSubscriptionId(); | |
} else { | |
$authNetErrors = $response->getMessages()->getMessage(); | |
$log = [ | |
'code' => $authNetErrors[0]->getCode(), | |
'message' => $authNetErrors[0]->getText(), | |
]; | |
// ... log error ... | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment