php类函数调用单引号问题

时间:2016-12-06 04:05:57

标签: php

我正在为一个网站连接Authorize.net API,我创建了一个购物车系统,到目前为止一切都很顺利。我的问题是我在其中一个类函数调用中使用参数的方式。只有在我的字符串周围使用单引号时它才会起作用。我根本不能使用变量它会抛出一个错误,这就是我在这里提出这个问题的原因。例如:

$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

那么我做错了什么?如果我可以在单引号中打印变量,它似乎可行,但PHP并不像那样工作。任何人都可以给我另一种方法让它工作吗?

提前致谢。

让我展示全部功能,这样每个人都可以看到发生了什么......

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

}

1 个答案:

答案 0 :(得分:0)

这对我来说不是一个好的解决方案。我最终删除了php-sdk并创建了自己的函数来调用Authorize.net的API。使用一些简单的PHP JSON和cURL。我一小时就跑起来了。有时用API来解决问题会让你花更多的时间而不仅仅是从头开始写作。我要感谢所有花时间看一看并提供反馈的人。