簡體   English   中英

PHP類函數調用單引號問題

[英]php class function call single quote issue

我正在為網站連接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";
}

}

這對我來說不是一個好的解決方案。 我最終刪除了php-sdk並創建了自己的函數來調用Authorize.net的API。 使用一些簡單的PHP JSON和cURL。 我一個小時就跑了。 有時,忙於API可能會使您花費更多時間,而不僅僅是從頭開始編寫。 我感謝所有花時間看一下並提供反饋的人。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM