1

I have created ARB subscription, but for that we need to pass customer credit card informaton and its expiration date, Can we create subscription by customer id ? We don't want to store any card information of the customer, but we have customer id, Is it possible or do any guys have any other idea ? Any help will be really appreciated

<?php
error_reporting(E_ALL);
require 'vendor/autoload.php';
//require_once 'constants/SampleCodeConstants.php';

use net\authorize\api\contract\v1 as AnetAPI;
use net\authorize\api\controller as AnetController;

date_default_timezone_set('America/Los_Angeles');

define("AUTHORIZENET_LOG_FILE", "phplog");

function createSubscription($intervalLength) {
    /* Create a merchantAuthenticationType object with authentication details
      retrieved from the constants file */
    $merchantAuthentication = new AnetAPI\MerchantAuthenticationType();
    $merchantAuthentication->setName('****');
    $merchantAuthentication->setTransactionKey('*****');

    // Set the transaction's refId
    $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("2038-12");

    /*echo "<pre>";
    print_r($creditCard);
    die;*/

    $payment = new AnetAPI\PaymentType();
    $payment->setCreditCard($creditCard);
    $subscription->setPayment($payment);

    $order = new AnetAPI\OrderType();
    $order->setInvoiceNumber("1234354");
    $order->setDescription("Description of the subscription");
    $subscription->setOrder($order);

    $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";
    }


    echo "<pre>";
    print_r($response);
    die;

    return $response;
}

if (!defined('DONT_RUN_SAMPLES'))
    createSubscription(23);
?>
John Conde
  • 217,595
  • 99
  • 455
  • 496
Nikul Panchal
  • 1,542
  • 3
  • 35
  • 58

1 Answers1

1

Yes, you can create a subscription from a customerProfileId as long as you also have the customerPaymentProfileId as well.

Here is sample XML:

<?xml version="1.0" encoding="utf-8"?>
<ARBCreateSubscriptionRequest xmlns="AnetApi/xml/v1/schema/AnetApiSchema.xsd">
    <merchantAuthentication>
        <name>API_USERNAME</name>
        <transactionKey>API_TRANSACTION_KEY</transactionKey>
    </merchantAuthentication>
    <refId>Sample</refId>
    <subscription>
        <name>Sample subscription</name>
        <paymentSchedule>
            <interval>
                <length>1</length>
                <unit>months</unit>
            </interval>
            <startDate>2020-08-30</startDate>
            <totalOccurrences>12</totalOccurrences>
            <trialOccurrences>1</trialOccurrences>
        </paymentSchedule>
        <amount>10.00</amount>
        <trialAmount>0.00</trialAmount>
        <profile>
            <customerProfileId>12345678</customerProfileId>
            <customerPaymentProfileId>987654342</customerPaymentProfileId>
        </profile>
    </subscription>
</ARBCreateSubscriptionRequest>

Or if you use the JSON APIs:

{
    "ARBCreateSubscriptionRequest": {
        "merchantAuthentication": {
            "name": "API_USERNAME",
            "transactionKey": "API_TRANSACTION_KEY"
        },
        "refId": "123456",
        "subscription": {
            "name": "Sample subscription",
            "paymentSchedule": {
                "interval": {
                    "length": "1",
                    "unit": "months"
                },
                "startDate": "2020-08-30",
                "totalOccurrences": "12",
                "trialOccurrences": "1"
            },
            "amount": "10.29",
            "trialAmount": "0.00",
            "profile": {
                "customerProfileId": "12345678",
                "customerPaymentProfileId": "987654342"
            }
        }
    }
}

Here's how you do it using their SDK:

Remove:

$creditCard = new AnetAPI\CreditCardType();
$creditCard->setCardNumber("4111111111111111");
$creditCard->setExpirationDate("2038-12");

/*echo "<pre>";
print_r($creditCard);
die;*/

$payment = new AnetAPI\PaymentType();
$payment->setCreditCard($creditCard);
$subscription->setPayment($payment);    

and:

$billTo = new AnetAPI\NameAndAddressType();
$billTo->setFirstName("John");
$billTo->setLastName("Smith");

$subscription->setBillTo($billTo);

Replace with:

$profile = new AnetAPI\CustomerProfileIdType();
$profile->setCustomerProfileId($customerProfileId);
$profile->setCustomerPaymentProfileId($customerPaymentProfileId);
$subscription->setProfile($profile);    
John Conde
  • 217,595
  • 99
  • 455
  • 496