{"id":223,"date":"2010-02-08T12:18:32","date_gmt":"2010-02-08T16:18:32","guid":{"rendered":"http:\/\/www.johnconde.net\/blog\/?p=223"},"modified":"2018-09-18T03:48:37","modified_gmt":"2018-09-18T07:48:37","slug":"tutorial-integrate-the-authorize-net-arb-api-with-php","status":"publish","type":"post","link":"https:\/\/website-0f9bf4a4.hpx.ppi.temporary.site\/blog\/tutorial-integrate-the-authorize-net-arb-api-with-php\/","title":{"rendered":"Tutorial: Integrate the Authorize.Net ARB API with PHP"},"content":{"rendered":"<p class=\"note\">\n    This tutorial has been deprecated. You should read the new tutorial, &#8220;<a href=\"\/blog\/tutorial-integrate-authorize-net-xml-api-universal-php-class\/\" onclick=\"javascript:_gaq.push(['_trackEvent','tutorial-redirect','\/blog\/tutorial-integrate-authorize-net-xml-api-universal-php-class\/']);\">Tutorial: Integrate All Authorize.Net XML APIs With One Universal PHP Class (AIM, ARB, CIM, Transaction Details)<\/a>&#8221; which offers new code for working with Authorize.Net&#8217;s XML APIs including sample code for every API call <a href=\"https:\/\/sakerdigital.com\/2018\/09\/05\/what-is-identity-resolution-marketing\/\">here<\/a>.\n<\/p>\n<p>\n    Just like Authorize.Net&#8217;s Advanced Integration Method (AIM) <acronym title=\"Application Programming Interface\">API<\/acronym> and Customer Information Manager (CIM) API, working with their Automated Recurring Billing (ARB) API isn&#8217;t difficult if you have suitable code to work with. The sample code offered by Authorize.Net will demonstrate how to work with their API but is not production ready. As with their <a href=\"\/blog\/tutorial-integrating-the-authorizenet-aim-api-with-php\/\"><acronym title=\"Advanced Integration Method\">AIM<\/acronym> API<\/a> and <a href=\"\/blog\/tutorial-integrate-the-authorize-net-cim-api-with-php\/\"><acronym title=\"Customer Information Manager\">CIM<\/acronym> API<\/a> I have created a class that abstracts their <acronym title=\"Automated Recurring Billing\">ARB<\/acronym> API into something easier to use and maintain.\n<\/p>\n<p>\n    To follow this tutorial and integrate ARB API into your PHP website follow these steps:\n<\/p>\n<ol>\n<li>If you don&#8217;t already have one sign up for a <a href=\"http:\/\/developer.authorize.net\/testaccount\/\" rel=\"external\">Authorize.Net Developer Account<\/a>. This will allow you to test your code without having an actual Authorize.Net account or incurring any processing fees.<\/li>\n<li>Download the <a href=\"http:\/\/www.authorize.net\/support\/ARB_guide.pdf\" rel=\"external\">ARB Integration Guide<\/a> (for reference)<\/li>\n<li>Download the <a href=\"\/files\/AuthnetARB.class.phps\">PHP code for ARB<\/a><\/li>\n<\/ol>\n<p>\n    To begin you will need to include the class using PHP&#8217;s <code>require()<\/code> function:\n<\/p>\n<pre lang=\"PHP\" line=\"1\">\r\nrequire('AuthnetARB.class.php');\r\n<\/pre>\n<p>\n    As with any class\/object the first thing we need to do is instantiate it. Up to three parameters are accepted in the constructor with the first two being required. <code>myapilogin<\/code> is the API login for the account. <code>mYtRaNsaCTiOnKEy<\/code> is the transaction key generated in the Authorize.Net control panel (or assigned to you with your test account information). If you have an Authorize.Net developer account you can use it with this script by setting the third parameters to <code>TRUE<\/code> (you may omit the third parameter if you are using this with the live server):\n<\/p>\n<pre lang=\"PHP\" line=\"1\">\r\n\/\/ Set up the subscription. If you are using a live account remove the third parameter.\r\n$subscription = new AuthnetARB('myapilogin', 'mYtRaNsaCTiOnKEy', \r\n                                              AuthnetARB::USE_DEVELOPMENT_SERVER);\r\n<\/pre>\n<p>\n    Creating a subscription is very straight forward. We only need to set a few parameters before placing our API call. You can see the full list of parameters in the ARB Guide mentioned above. Naturally you will change these to be specific to your subscription:\n<\/p>\n<pre lang=\"PHP\" line=\"1\">\r\n$subscription->setParameter('amount', 29.99);\r\n$subscription->setParameter('cardNumber', '4111111111111111');\r\n$subscription->setParameter('expirationDate', '2016-12');\r\n$subscription->setParameter('firstName', 'John');\r\n$subscription->setParameter('lastName', 'Conde');\r\n$subscription->setParameter('address', '123 Main Street');\r\n$subscription->setParameter('city', 'Townsville');\r\n$subscription->setParameter('state', 'NJ');\r\n$subscription->setParameter('zip', '12345');\r\n$subscription->setParameter('email', 'fakemail@example.com');\r\n\r\n\/\/ Create the subscription\r\n$subscription->createAccount();\r\n<\/pre>\n<p>\n    After making our API call we&#8217;ll want to verify it was successful and, if so, capture the subscription ID:\n<\/p>\n<pre lang=\"PHP\" line=\"1\">\r\n\/\/ Check the results of our API call\r\nif ($subscription->isSuccessful())\r\n{\r\n    \/\/ Get the subscription ID\r\n    $subscription_id = $subscription->getSubscriberID();\r\n}\r\nelse\r\n{\r\n    \/\/ The subscription was not created!\r\n}\r\n<\/pre>\n<p>\n    Make sure you save the subscription ID as you will need to refer to it if you wish to update the subscription or delete it later.\n<\/p>\n<p>\n    By default the class assumes the subscription will be monthly. To change the frequency and length of the subscription you can use code similar to the following examples:\n<\/p>\n<pre lang=\"PHP\" line=\"1\">\r\n\/\/ Put this before $subscription->createAccount(); \r\n\/\/ if you want the subscription to be every three months.\r\n$subscription->setParameter('interval_length', 3);\r\n$subscription->setParameter('startDate', date(\"Y-m-d\", strtotime(\"+ 3 months\")));\r\n\r\n\/\/ Put this before $subscription->createAccount();\r\n\/\/ if you want the subscription to be every year.\r\n$subscription->setParameter('interval_unit', 'years');\r\n$subscription->setParameter('startDate', date(\"Y-m-d\", strtotime(\"+ 1 year\")));\r\n<\/pre>\n<p>\n    <code>interval_length<\/code> is how many units (i.e. weeks, months, years) you wish the subscription to wait before processing another payment. In the first example we set it to three which means the payment will process every three months. <code>interval_unit<\/code> sets the unit (i.e. weeks, months, years) you wish to use. In the second we changed it to &#8220;years&#8221; which means this subscription will be charged annually. <code>startDate<\/code> sets the date of the first subscription payment. We use PHP&#8217;s built in functions <code>strtotime()<\/code> and <code>date()<\/code> to make creating the date very easy. All we need to do is pass <code>strtotime()<\/code> the start date relative to today&#8217;s date in plain English. In our first example we want to set the first subscription payment to start in three months so we pass &#8220;+ 3 months&#8221; to <code>strtotime()<\/code>. In our second example we want the subscription to start in one year so we pass &#8220;+1 year&#8221; to <code>strtotime()<\/code>.\n<\/p>\n<p>\n    By default the class assumes no trial period will occur. To create your subscription with a trial period just use the following snippet of code:\n<\/p>\n<pre lang=\"PHP\" line=\"1\">\r\n\/\/ Put this before $subscription->createAccount(); \r\n\/\/ if you want a trial period of three months for $10.00.\r\n$subscription->setParameter('trialOccurrences', 3);\r\n$subscription->setParameter('trialAmount', 10.00);\r\n<\/pre>\n<p>\n    A common question concerning subscriptions is how do you handle delayed subscriptions where no money is charged up front? How do you verify the credit card is valid before making the subscription? Naturally you want to make sure the credit card is valid before making the subscription but if you&#8217;re not actually charging the first subscription payment up front (i.e. immediately) you won&#8217;t know if the credit card is valid. Fortunately verifying the credit card is easy to do. Just use the AIM API to do an Authorization Only. This will verify that the credit card is indeed valid before you create your subscription.\n<\/p>\n<pre lang=\"PHP\" line=\"1\">\r\n$authorization = new AuthnetAIM('myapilogin', 'mYtRaNsaCTiOnKEy');\r\n$authorization->setTransaction($creditcard, $expiration, 0.00);\r\n$authorization->setTransactionType('AUTH_ONLY');\r\n$authorization->process();\r\n\r\nif ($authorization->isApproved())\r\n{\r\n    \/\/ Set up ARB subscription\r\n}\r\nelse if ($authorization->isDeclined())\r\n{\r\n    \/\/ The card is not valid\r\n}\r\n<\/pre>\n<p>\n    The AuthnetARB class takes advantage of exceptions which are new in PHP5. These are only generated when an &#8220;exceptional&#8221; event occurs. The only exceptional event that might occur when using this class is cURL may fail. This may be due to server, network, or user errors. To catch these exceptions and handle them use code similar to this:\n<\/p>\n<pre lang=\"PHP\" line=\"1\">\r\ntry\r\n{\r\n    $cim = new AuthnetARB('myapilogin', 'mYtRaNsaCTiOnKEy');\r\n    \/\/ Do more ARB stuff here\r\n}\r\ncatch (AuthnetARBException $e)\r\n{\r\n    echo 'There was an error processing the transaction. Here is the error message: ';\r\n    echo $e->__toString();\r\n}\r\n<\/pre>\n<p>\n    Now that we have an idea of how to create subscriptions using the ARB API let&#8217;s see a practical example that sets the billing cycle to be every three months and has a trial subscription for three months:\n<\/p>\n<pre lang=\"PHP\" line=\"1\">\r\n< ?php\r\n\/\/ Include AuthnetCIM class. Nothing works without it!\r\nrequire('AuthnetARB.class.php');\r\n\r\n\/\/ Use try\/catch so if an exception is thrown we can catch it \r\n\/\/ and figure out what happened\r\ntry\r\n{\r\n    \/\/ Set up the subscription. Use the developer account for testing..\r\n    $subscription = new AuthnetARB('myapilogin', 'mYtRaNsaCTiOnKEy', \r\n                                              AuthnetARB::USE_DEVELOPMENT_SERVER);\r\n\r\n    \/\/ Set subscription information\r\n    $subscription->setParameter('amount', 29.99);\r\n    $subscription->setParameter('cardNumber', '4111111111111111');\r\n    $subscription->setParameter('expirationDate', '2016-16');\r\n    $subscription->setParameter('firstName', 'John');\r\n    $subscription->setParameter('lastName', 'Conde');\r\n    $subscription->setParameter('address', '123 Main Street');\r\n    $subscription->setParameter('city', 'Townsville');\r\n    $subscription->setParameter('state', 'NJ');\r\n    $subscription->setParameter('zip', '12345');\r\n    $subscription->setParameter('email', 'fakemail@example.com');\r\n\r\n    \/\/ Set the billing cycle for every three months\r\n    $subscription->setParameter('interval_length', 3);\r\n    $subscription->setParameter('startDate', date(\"Y-m-d\", strtotime(\"+ 3 months\")));\r\n\r\n    \/\/ Set up a trial subscription for three months at a reduced price\r\n    $subscription->setParameter('trialOccurrences', 3);\r\n    $subscription->setParameter('trialAmount', 10.00);\r\n\r\n    \/\/ Create the subscription\r\n    $subscription->createAccount();\r\n\r\n    \/\/ Check the results of our API call\r\n    if ($subscription->isSuccessful())\r\n    {\r\n        \/\/ Get the subscription ID\r\n        $subscription_id = $subscription->getSubscriberID();\r\n    }\r\n    else\r\n    {\r\n        \/\/ The subscription was not created!\r\n    }\r\n}\r\ncatch (AuthnetARBException $e)\r\n{\r\n    echo $e;\r\n    echo $subscription;\r\n}\r\n?>\r\n<\/pre>\n<p>\n    This class also supports updating and deleting subscriptions. Updating a subscription might look like this:\n<\/p>\n<pre lang=\"PHP\" line=\"1\">\r\n$subscription = new AuthnetARB('myapilogin', 'mYtRaNsaCTiOnKEy');\r\n$subscription->setParameter('subscriptionId', $subscription_id);\r\n$subscription->setParameter('cardNumber', '4111111111111111');\r\n$subscription->setParameter('expirationDate', '2016-16');\r\n$subscription->setParameter('firstName', 'John');\r\n$subscription->setParameter('lastName', 'Conde');\r\n$subscription->setParameter('address', '123 Main Street');\r\n$subscription->setParameter('city', 'Townsville');\r\n$subscription->setParameter('state', 'NJ');\r\n$subscription->setParameter('zip', '12345');\r\n$subscription->setParameter('email', 'fakemail@example.com');\r\n$subscription->updateAccount();\r\n<\/pre>\n<p>\n    Deleting a subscription would look like this:\n<\/p>\n<pre lang=\"PHP\" line=\"1\">\r\n$subscription = new AuthnetARB('myapilogin', 'mYtRaNsaCTiOnKEy');\r\n$subscription->setParameter('subscriptionId', $subscription_id);\r\n$subscription->deleteAccount();\r\n<\/pre>\n<p>\n    There you have it. Working with Authorize.Net&#8217;s ARB API is pretty easy once you hide their API behind some easy to use code. Have fun creating subscriptions!<\/p>\n","protected":false},"excerpt":{"rendered":"<p>This tutorial has been deprecated. You should read the new tutorial, &#8220;Tutorial: Integrate All Authorize.Net XML APIs With One Universal PHP Class (AIM, ARB, CIM, Transaction Details)&#8221; which offers new code for working with Authorize.Net&#8217;s XML APIs including sample code &hellip; <a href=\"https:\/\/website-0f9bf4a4.hpx.ppi.temporary.site\/blog\/tutorial-integrate-the-authorize-net-arb-api-with-php\/\">Continue reading <span class=\"meta-nav\">&rarr;<\/span><\/a><\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_jetpack_newsletter_access":"","_jetpack_dont_email_post_to_subs":false,"_jetpack_newsletter_tier_id":0,"_jetpack_memberships_contains_paywalled_content":false,"_jetpack_memberships_contains_paid_content":false,"footnotes":"","jetpack_publicize_message":"","jetpack_publicize_feature_enabled":true,"jetpack_social_post_already_shared":false,"jetpack_social_options":{"image_generator_settings":{"template":"highway","default_image_id":0,"font":"","enabled":false},"version":2},"jetpack_post_was_ever_published":false},"categories":[16],"tags":[60,50],"class_list":["post-223","post","type-post","status-publish","format-standard","hentry","category-programming","tag-authorizenet","tag-php"],"jetpack_publicize_connections":[],"jetpack_featured_media_url":"","jetpack_shortlink":"https:\/\/wp.me\/pwpo4-3B","jetpack_sharing_enabled":true,"_links":{"self":[{"href":"https:\/\/website-0f9bf4a4.hpx.ppi.temporary.site\/blog\/wp-json\/wp\/v2\/posts\/223","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/website-0f9bf4a4.hpx.ppi.temporary.site\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/website-0f9bf4a4.hpx.ppi.temporary.site\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/website-0f9bf4a4.hpx.ppi.temporary.site\/blog\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/website-0f9bf4a4.hpx.ppi.temporary.site\/blog\/wp-json\/wp\/v2\/comments?post=223"}],"version-history":[{"count":9,"href":"https:\/\/website-0f9bf4a4.hpx.ppi.temporary.site\/blog\/wp-json\/wp\/v2\/posts\/223\/revisions"}],"predecessor-version":[{"id":574,"href":"https:\/\/website-0f9bf4a4.hpx.ppi.temporary.site\/blog\/wp-json\/wp\/v2\/posts\/223\/revisions\/574"}],"wp:attachment":[{"href":"https:\/\/website-0f9bf4a4.hpx.ppi.temporary.site\/blog\/wp-json\/wp\/v2\/media?parent=223"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/website-0f9bf4a4.hpx.ppi.temporary.site\/blog\/wp-json\/wp\/v2\/categories?post=223"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/website-0f9bf4a4.hpx.ppi.temporary.site\/blog\/wp-json\/wp\/v2\/tags?post=223"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}