如何在Authorize.net PHP SDK中设置客户ip地址?

时间:2018-12-05 16:48:25

标签: php authorize.net

我在我们的网站上使用Authorize.net PHP SDK进行信用卡付款。目前,我正在向客户帐单发送诸如客户的名字,姓氏,地址,邮政编码,城市,州,国家和电子邮件之类的信息。 我还需要发送客户IP地址。请提出解决方案。 当前代码的一部分看起来像这样:

public function authorize_card($data) {
		// Create the payment data for a credit card
		$creditCard = new AnetAPI\CreditCardType();
		$creditCard->setCardNumber($data['card_no']);
		$creditCard->setExpirationDate($data['card_exp']);
		$creditCard->setCardCode($data['card_cvc']);
		
		$paymentCreditCard = new AnetAPI\PaymentType();
		$paymentCreditCard->setCreditCard($creditCard);
		
		//create a transaction
		$transactionRequestType = new AnetAPI\TransactionRequestType();
		$transactionRequestType->setTransactionType("authCaptureTransaction"); 
		$transactionRequestType->setAmount($data['amount']);
		$transactionRequestType->setPayment($paymentCreditCard);
		
		
		$billto = new AnetAPI\CustomerAddressType();
		$billto->setFirstName($data['bill_fname']);
		$billto->setLastName($data['bill_lname']);
		$billto->setAddress($data['bill_address']);
		$billto->setCity($data['bill_city']);
		$billto->setState($data['bill_state']);
		$billto->setZip($data['bill_zip']);
		$billto->setCountry($data['bill_country']);
		
		$bill_response = $transactionRequestType->setBillTo($billto);

		$request = new AnetAPI\CreateTransactionRequest();
		$request->setMerchantAuthentication($this->auth);
		$request->setRefId($this->refId);
		$request->setTransactionRequest($transactionRequestType);
		$controller = new AnetController\CreateTransactionController($request);
		$response = $controller->executeWithApiResponse($this->api_mode);
	
		if ($response != null) {
			$tresponse = $response->getTransactionResponse();
			
			if ($tresponse != null && ($tresponse->getResponseCode() == 1 ||      $tresponse->getResponseCode() == 253)) {
				$response_array = array();
				$response_array['auth_code'] = $tresponse->getAuthCode();
				$response_array['auth_transaction_id'] = $tresponse->getTransId();
				return $response_array;
			} else {
				$errors = $tresponse->geterrors();
				
				if (is_array($errors) && !empty($errors)) {
					return $errors[0]->geterrorText();
				} else {
					$message = $response->getMessages()->getMessage();
					return $message[0]->getText();
				}
			}
			
		} else {
			return "Charge Credit card Null response returned";
		}
}

1 个答案:

答案 0 :(得分:1)

实际上我已经弄清楚了。在autorizeNet php sdk中提供了一种设置客户ip地址的方法。 方法是setCustomerIP() 需要与TransactionRequestType类一起使用。 最终代码如下:

public function authorize_card($data) {
		// Create the payment data for a credit card
		$creditCard = new AnetAPI\CreditCardType();
		$creditCard->setCardNumber($data['card_no']);
		$creditCard->setExpirationDate($data['card_exp']);
		$creditCard->setCardCode($data['card_cvc']);
		
		$paymentCreditCard = new AnetAPI\PaymentType();
		$paymentCreditCard->setCreditCard($creditCard);
		
		//create a transaction
		$transactionRequestType = new AnetAPI\TransactionRequestType();
		$transactionRequestType->setTransactionType("authCaptureTransaction"); 
		$transactionRequestType->setAmount($data['amount']);
		$transactionRequestType->setPayment($paymentCreditCard);
    
    //Setting customer ip address
    $transactionRequestType->setCustomerIP($data['ip']);
		
		
		$billto = new AnetAPI\CustomerAddressType();
		$billto->setFirstName($data['bill_fname']);
		$billto->setLastName($data['bill_lname']);
		$billto->setAddress($data['bill_address']);
		$billto->setCity($data['bill_city']);
		$billto->setState($data['bill_state']);
		$billto->setZip($data['bill_zip']);
		$billto->setCountry($data['bill_country']);
		
		$bill_response = $transactionRequestType->setBillTo($billto);

		$request = new AnetAPI\CreateTransactionRequest();
		$request->setMerchantAuthentication($this->auth);
		$request->setRefId($this->refId);
		$request->setTransactionRequest($transactionRequestType);
		$controller = new AnetController\CreateTransactionController($request);
		$response = $controller->executeWithApiResponse($this->api_mode);
	
		if ($response != null) {
			$tresponse = $response->getTransactionResponse();
			
			if ($tresponse != null && ($tresponse->getResponseCode() == 1 || $tresponse->getResponseCode() == 253)) {
				$response_array = array();
				$response_array['auth_code'] = $tresponse->getAuthCode();
				$response_array['auth_transaction_id'] = $tresponse->getTransId();
				return $response_array;
			} else {
				$errors = $tresponse->geterrors();
				
				if (is_array($errors) && !empty($errors)) {
					return $errors[0]->geterrorText();
				} else {
					$message = $response->getMessages()->getMessage();
					return $message[0]->getText();
				}
			}
			
		} else {
			return "Charge Credit card Null response returned";
		}
}