AuthorizeNet / sample-code-php

This repository contains working code samples which demonstrate php integration with the Authorize.Net API
MIT License
176 stars 197 forks source link

How to Pass BillTo Address & Name to php SDK? #67

Closed MilesYM closed 7 years ago

MilesYM commented 7 years ago

Hi guys,

Thanks for this awesome samples, really helpful. I’m trying to pass the name on the card to the API but I’m not sure how to do. I figured out there was a BillTo field with name in the API requests parameters but I’m not able to find it in the PHP SDK.

So far my piece of code is very similar to the original example:

`

          // Common setup for API credentials  
    $merchantAuthentication = new AnetAPI\MerchantAuthenticationType();   
    $merchantAuthentication->setName($this->ci->config->item('AUTHNETAPILOGINID'));   
    $merchantAuthentication->setTransactionKey($this->ci->config->item('AUTHNETKEY'));   
            $refId = 'ref' . time();

    // Remove potential white spaces
    $ccn = preg_replace('/\s+/', '', $ccn);

    // Create the payment data for a credit card
    $creditCard = new AnetAPI\CreditCardType();
    $creditCard->setCardNumber( intval( $ccn ) );
    $creditCard->setExpirationDate( $exp );
    $creditCard->setCardCode( $cvc );
    $paymentOne = new AnetAPI\PaymentType();
    $paymentOne->setCreditCard($creditCard);

    $order = new AnetAPI\OrderType();
    $order->setDescription("Ticket");

    // Create a transaction
    $transactionRequestType = new AnetAPI\TransactionRequestType();
    $transactionRequestType->setTransactionType("authCaptureTransaction");   
    $transactionRequestType->setAmount( $amount );
            $transactionRequestType->setOrder( $order );
    $transactionRequestType->setPayment($paymentOne);

    $request = new AnetAPI\CreateTransactionRequest();
    $request->setMerchantAuthentication($merchantAuthentication);
    $request->setRefId( $refId );
    $request->setTransactionRequest($transactionRequestType);
    $controller = new AnetController\CreateTransactionController($request);
    $response_api = $controller->executeWithApiResponse(\net\authorize\api\constants\ANetEnvironment::PRODUCTION);   

    if ($response_api != null)
    {
                    // More Code
            }

`

ashtru commented 7 years ago

@MilesYM Thanks for reaching out to us.

Similar to the other samples, the code for setting billing address should look something like this:

      // 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");
         //Assign to the transactionRequest field
     $transactionRequestType->setBillTo($billto);

Steps

MilesYM commented 7 years ago

Great! Thank you so much Ashtru!

Sorry I haven’t see that in the samples! I’m going to try! What’s the minimum required ? Name and postal code ?

Thanks!

MilesYM commented 7 years ago

How do you do when there is a Middle name or a suffix in name ?

ashtru commented 7 years ago

In general, there are no required fields for a billing address. Some exception are:

ashtru commented 7 years ago

In case of middle name or suffixes, there is no defined convention. You can generally add middle names (or more commonly middle initials) into the first name fields. The issuers normally use initials for the middle names.

@MilesYM Please respond if you were able to get this issue resolved. We will close this issue for for now. Feel free to comment in case of any further questions!

adavidw commented 7 years ago

We've updated the charge-credit-card and authorize-credit-card to show samples of this. (updated in PR#70)