简体   繁体   中英

php class function call single quote issue

I am wiring up the Authorize.net API for a web site and I have created a cart system where all is well so far. My issue is with the way I am using a parameter in one of the class function calls. It will only work if I use single quotes surrounding my string. I cannot use a variable at all it throws an error which is the reason I am here asking this question. For example:

$lineItem->setDescription('SOME TEXT GOES HERE'); // works perfectly

$foo = 'SOME TEXT GOES HERE';
$lineItem->setDescription($foo); // fails

$lineItem->setDescription("$foo"); // fails

// I tried all kinds of ways like:
$foo = "SOME TEXT GOES HERE"; // double quotes
$lineItem->setDescription($foo); // fails

// I tried this too and this works but In my case I am within a loop
define("FOO", $foo);
$lineItem->setDescription(FOO); // works but I need to loop all the items

// I tried this
$foo = json_encode($foo);
$lineItem->setDescription($foo); // fails?

// It seems like this would work but I know this is not realistic.
$lineItem->setDescription('$foo'); // this is just for illustrative purposes

So what am I doing wrong? If I could print a variable in single quotes it would seem to work but PHP doesn't work like that. Can anyone give me another way to get this to work?

Thanks in advance.

Let me show the full function so everyone can see whats happening...

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

function AuthorizeNet(){    

require 'assets/authorizenet/vendor/autoload.php';

    define("AUTHORIZENET_LOG_FILE", "phplog");

// Common setup for API credentials
$merchantAuthentication = new AnetAPI\MerchantAuthenticationType();
$merchantAuthentication->setName($GLOBALS['authorizenet_api']);
$merchantAuthentication->setTransactionKey($GLOBALS['authorizenet_tx']);

// Create the payment data for a credit card
$creditCard = new AnetAPI\CreditCardType();
$creditCard->setCardNumber("4111111111111111");
$creditCard->setExpirationDate("2038-12");
$paymentOne = new AnetAPI\PaymentType();
$paymentOne->setCreditCard($creditCard);

// Create the Bill To info
$billto = new AnetAPI\CustomerAddressType();
$billto->setFirstName("Ellen");
$billto->setLastName("Johnson");
$billto->setCompany("Souveniropolis");
$billto->setAddress("14 Main Street");
$billto->setCity("Pecan Springs");
$billto->setState("TX");
$billto->setZip("44628");
$billto->setCountry("USA");
$billto->setPhoneNumber("310-867-5309");    

$total = 0;
$tax=0;
$shipping=0;
$items_subtotal = 0;
$discount_total = 0;
$lineItem_Array = array();
$i=0;

// Create the items
foreach($_SESSION['ms_cart'] as $cart_item){

    $desc = array();

    if(!empty($cart_item["size"])){
        $desc[] = "Size: " . $cart_item["size"];
    }

    if(!empty($cart_item["color"])){
        $desc[] = "Color: " . $cart_item["color"];
    }

    $description = implode(", ", $desc);


    if(!empty($cart_item["discount_total"])){
        $discount_total = $discount_total+round($cart_item["discount_total"],2);
    }

    $name = $cart_item["name"];
    $desc = $description;
    $quantity = $cart_item["qty"];
    $price = $cart_item["price"];
    $sku = $cart_item["uiid"];
    $items_subtotal = ($price*$quantity)+$items_subtotal;


    $lineItem = new AnetAPI\LineItemType();
    $lineItem->setItemId($sku);
    $lineItem->setName($name); // <-- this is my issue
    $lineItem->setDescription($desc);  // <-- also here
    $lineItem->setQuantity(1);
    $lineItem->setUnitPrice(1);
    $lineItem->setTaxable(0); // 1 Yes 0 for no
    $lineItem_Array[$i] = $lineItem;

    $i++;   
}

$subtotal = round($items_subtotal, 2);

if(!empty($_SESSION['ms_cart_tax'])){
    $tax = number_format(round($_SESSION['ms_cart_tax'], 2), 2, '.', '');
}

if(!empty($_SESSION['ms_ship_rate'])){
    $shipping = round($_SESSION['ms_ship_rate'], 2);
}

$total = ($subtotal+$tax+$shipping)-$discount_total;
$total = round($total,2);

// Create a transaction
$transactionRequestType = new AnetAPI\TransactionRequestType();
$transactionRequestType->setTransactionType( "authCaptureTransaction"); 

$transactionRequestType->setBillTo($billto);
$transactionRequestType->setLineItems($lineItem_Array);
$transactionRequestType->setPayment($paymentOne);

$total=4; // there are 4 items in my cart loop for now I have hard coded the 4
$transactionRequestType->setAmount($total);

$request = new AnetAPI\CreateTransactionRequest();
$request->setMerchantAuthentication($merchantAuthentication);
$request->setTransactionRequest($transactionRequestType);

$controller = new AnetController\CreateTransactionController($request);

$response = $controller->executeWithApiResponse($GLOBALS['authorizenet_env']);

if ($response != null)
{
    $tresponse = $response->getTransactionResponse();

    if (($tresponse != null) && ($tresponse->getResponseCode()=="1") )   
    {
        echo "Charge Credit Card AUTH CODE : " . $tresponse->getAuthCode() . "\n";
        echo "Charge Credit Card TRANS ID  : " . $tresponse->getTransId() . "\n";
    }
    else
    {
        echo  "Charge Credit Card ERROR :  Invalid response\n";
    }
}
else
{
    echo  "Charge Credit card Null response returned";
}

}

This was not a good solution for me. I ended up removing the php-sdk and creating my own function to call Authorize.net's API. With some simple PHP JSON and cURL. I was up and running in an hour. Sometimes fussing around with API's can make you spend more time than just writing from scratch. I would like to thank everyone that took the time to take look and provide feedback.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM