cancel
Showing results for 
Search instead for 
Did you mean: 

Issue with creating transaction and subscription

I am trying to create a transaction using accept js and on the backend, I have 2 curl requests. One for the transaction request and the other for the subscription request. I have a checkbox on the payment for asking if the users want to make his payment a recurring payment. The transaction request is working just fine but the subscription request is failing. I tried to comment out the transaction code and only run the recurring request but it still showing this error:

 

messages: {
    resultCode: "Error",
    message: {
        code: "E00114",
        text: "Invalid OTS Token."
    }
}

Here is all my PHP logic on the controller

<?php
    $loginId = AUTHROIZE_NET_LOGIN_ID;
    $transactionKey = AUTHROIZE_NET_TRANSACTION_KEY;

    // Posted data:
    // $request->amount = $_POST['amount'];
    // $request->first_name = $_POST['first_name'];
    // $request->last_name = $_POST['last_name'];
    // $request->dataDesc = $_POST['dataDesc'];
    // $request->dataValue = $_POST['dataValue'];

    $transRequestXmlStr = <<<XML
<?xml version="1.0" encoding="UTF-8"?>
<createTransactionRequest>
    <merchantAuthentication></merchantAuthentication>
    <transactionRequest>
        <transactionType>authCaptureTransaction</transactionType>
        <amount>assignAMOUNT</amount>
        <currencyCode>USD</currencyCode>
        <payment>
            <opaqueData>
                <dataDescriptor>assignDD</dataDescriptor>
                <dataValue>assignDV</dataValue>
            </opaqueData>
        </payment>
        <billTo>
            <firstName>firstN</firstName>
            <lastName>lastN</lastName>
        </billTo>
    </transactionRequest>
</createTransactionRequest>
XML;

    $transRequestXml = new \SimpleXMLElement($transRequestXmlStr);

    $transRequestXml->addAttribute('xmlns','AnetApi/xml/v1/schema/AnetApiSchema.xsd');

    $transRequestXml->merchantAuthentication->addChild('name',$loginId);
    $transRequestXml->merchantAuthentication->addChild('transactionKey',$transactionKey);

    $transRequestXml->transactionRequest->amount = $request->amount;
    $transRequestXml->transactionRequest->billTo->firstName = $request->first_name;
    $transRequestXml->transactionRequest->billTo->lastName = $request->last_name;
    $transRequestXml->transactionRequest->payment->opaqueData->dataDescriptor = $request->dataDesc;
    $transRequestXml->transactionRequest->payment->opaqueData->dataValue = $request->dataValue;

    $url="https://api.authorize.net/xml/v1/request.api";

    try {
        //setting the curl parameters.
        $ch = curl_init();
        if (FALSE === $ch)
            throw new Exception('failed to initialize');
        curl_setopt($ch, CURLOPT_URL, $url);
        curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: application/xml'));
        curl_setopt($ch, CURLOPT_POSTFIELDS, $transRequestXml->asXML());
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
        curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 300);
        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
        curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
        //curl_setopt($ch, CURLOPT_DNS_USE_GLOBAL_CACHE, false );
        $content = curl_exec($ch);
        if (FALSE === $content)
            throw new Exception(curl_error($ch), curl_errno($ch));
        curl_close($ch);
        
        $content = str_replace(' xmlns="AnetApi/xml/v1/schema/AnetApiSchema.xsd"', '', $content);
        $xmlResult=simplexml_load_string($content);

        $jsonResult = json_encode($xmlResult);

        $objResult = json_decode($jsonResult);

        if($objResult->transactionResponse->responseCode == "1") {
            // Success
        }
          
        // echo $jsonResult;

    } catch(Exception $e) {
        trigger_error(sprintf('Curl failed with error #%d: %s', $e->getCode(), $e->getMessage()), E_USER_ERROR);
    }

    $transRequestXmlStr2 = <<<XML
<?xml version="1.0" encoding="UTF-8"?>
<ARBCreateSubscriptionRequest>
    <merchantAuthentication></merchantAuthentication>
    <subscription>
        <name>Quick donation</name>
        <paymentSchedule>
			<interval>
				<length>1</length>
				<unit>months</unit>
			</interval>
			<startDate>2020-08-30</startDate>
			<totalOccurrences>9999</totalOccurrences>
		</paymentSchedule>
        <amount>assignAMOUNT</amount>
        <payment>
            <opaqueData>
                <dataDescriptor>assignDD</dataDescriptor>
                <dataValue>assignDV</dataValue>
            </opaqueData>
        </payment>
        <billTo>
            <firstName>firstN</firstName>
            <lastName>lastN</lastName>
        </billTo>
    </subscription>
</ARBCreateSubscriptionRequest>
XML;
          
    $transRequestXml2 = new \SimpleXMLElement($transRequestXmlStr2);

    $transRequestXml2->addAttribute('xmlns','AnetApi/xml/v1/schema/AnetApiSchema.xsd');

    $transRequestXml2->merchantAuthentication->addChild('name',$loginId);
    $transRequestXml2->merchantAuthentication->addChild('transactionKey',$transactionKey);
    
    $transRequestXml2->subscription->paymentSchedule->startDate = date('Y-m-d');
    $transRequestXml2->subscription->amount = $request->amount;
    $transRequestXml2->subscription->billTo->firstName = $request->first_name;
    $transRequestXml2->subscription->billTo->lastName = $request->last_name;
    $transRequestXml2->subscription->payment->opaqueData->dataDescriptor = $request->dataDesc;
    $transRequestXml2->subscription->payment->opaqueData->dataValue = $request->dataValue;
            
    $url2 = "https://api.authorize.net/xml/v1/request.api";

    try {
        //setting the curl parameters.
        $ch2 = curl_init();
        if (FALSE === $ch2)
            throw new Exception('failed to initialize');
        curl_setopt($ch2, CURLOPT_URL, $url2);
        curl_setopt($ch2, CURLOPT_HTTPHEADER, array('Content-Type: application/xml'));
        curl_setopt($ch2, CURLOPT_POSTFIELDS, $transRequestXml2->asXML());
        curl_setopt($ch2, CURLOPT_RETURNTRANSFER, 1);
        curl_setopt($ch2, CURLOPT_CONNECTTIMEOUT, 300);
        curl_setopt($ch2, CURLOPT_SSL_VERIFYPEER, false);
        curl_setopt($ch2, CURLOPT_SSL_VERIFYHOST, false);
        // curl_setopt($ch2, CURLOPT_DNS_USE_GLOBAL_CACHE, false );
        $content2 = curl_exec($ch2);
        if (FALSE === $content2)
            throw new Exception(curl_error($ch2), curl_errno($ch2));
        curl_close($ch2);

        $content2 = str_replace(' xmlns="AnetApi/xml/v1/schema/AnetApiSchema.xsd"', '', $content2);
        $xmlResult2 = simplexml_load_string($content2);

        $jsonResult2 = json_encode($xmlResult2);

        $objResult2 = json_decode($jsonResult2);

        // echo $jsonResult2;

    } catch(Exception $e) {
        trigger_error(sprintf('Curl failed with error #%d: %s', $e->getCode(), $e->getMessage()), E_USER_ERROR);
    }

I am very confused. I couldn't find any solution online for the issue. This that the right way to create the requests? Thank you.

the94air
Member
1 ACCEPTED SOLUTION

Accepted Solutions

Hello @the94air

 

The payment nonce returned by Accept.js can only be used once, which is why you are receiving the E000114 error when attempting to create a subscription using the nonce a second time.

 

When creating your initial transaction, set the option to create a customer profile.  Then if the transaction is successful, the response will include a customer profile id (token) which can be used to create a subscription.

 

Richard

View solution in original post

RichardH
Administrator Administrator
Administrator
3 REPLIES 3

Can you at least tell me what the "Invalid OTS Token." error means. This will be super helpful. Thanks.

the94air
Member

Hello @the94air

 

The payment nonce returned by Accept.js can only be used once, which is why you are receiving the E000114 error when attempting to create a subscription using the nonce a second time.

 

When creating your initial transaction, set the option to create a customer profile.  Then if the transaction is successful, the response will include a customer profile id (token) which can be used to create a subscription.

 

Richard

RichardH
Administrator Administrator
Administrator

Thanks, Richard. That solved my issue. I also had to add a delay before serving the subscription Curl.