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

Call to a member function getResultCode() on a non-object #46

Closed globatum closed 7 years ago

globatum commented 8 years ago

I am using laravel 5.2 and I cannot run any of the example codes because of the above error. It looks like nothing else is giving errors except when I get to

if (($response != null) && ($response->getMessages()->getResultCode() == "Ok") ) { echo "Succesfully create customer profile : " . $response->getCustomerProfileId() . "\n"; $paymentProfiles = $response->getCustomerPaymentProfileIdList(); echo "SUCCESS: PAYMENT PROFILE ID : " . $paymentProfiles[0] . "\n"; }

`public function createProfile($cardnum, $mm, $yy, $cvc, $email){

    $merchantAuthentication = new AnetAPI\MerchantAuthenticationType();
    $merchantAuthentication->setName("xxxxxx");
    $merchantAuthentication->setTransactionKey("yyyyyyyyy");
    $refId = 'ref' . time();

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

    // set profile
    $billto = new AnetAPI\CustomerAddressType();
    $billto->setFirstName("FullName");
    $billto->setCompany("Souveniropolis");

    // payment profile
    $paymentprofile = new AnetAPI\CustomerPaymentProfileType();
    $paymentprofile->setCustomerType('individual');
    $paymentprofile->setBillTo($billto);
    $paymentprofile->setPayment($paymentCreditCard);
    $paymentprofiles[] = $paymentprofile;
    $customerprofile = new AnetAPI\CustomerProfileType();
    $customerprofile->setDescription("Customer 2 Test PHP");

    $customerprofile->setMerchantCustomerId("M_".$email);
    $customerprofile->setEmail($email);
    $customerprofile->setPaymentProfiles($paymentprofiles);

    $request = new AnetAPI\CreateCustomerProfileRequest();
    $request->setMerchantAuthentication($merchantAuthentication);
    $request->setRefId( $refId);
    $request->setProfile($customerprofile);
    $controller = new AnetController\CreateCustomerProfileController($request);
    $response = $controller->executeWithApiResponse( \net\authorize\api\constants\ANetEnvironment::SANDBOX);

    if (($response != null) && ($response->getMessages()->getResultCode() == "Ok") )
      {
          echo "Succesfully create customer profile : " . $response->getCustomerProfileId() . "\n";
          $paymentProfiles = $response->getCustomerPaymentProfileIdList();
          echo "SUCCESS: PAYMENT PROFILE ID : " . $paymentProfiles[0] . "\n";
       }
      else
      {
          echo "ERROR :  Invalid response\n";
          $errorMessages = $response->getMessages()->getMessage();
          echo "Response : " . $errorMessages[0]->getCode() . "  " .$errorMessages[0]->getText() . "\n";
      }
      return $response;
}`
ashtru commented 8 years ago

@globatum Can you please provide us an email for communication? Feel free to contact ashu140510@gmail.com , sunnyrajrathod@gmail.com

daniel-farina commented 7 years ago

If you do:

dd($response);

You will most likely see that its working but would strongly suggest changing the response processing to something like:

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

            if ($tresponse != null && $tresponse->getMessages() != null)
            {
                echo " Transaction Response code : " . $tresponse->getResponseCode() . "\n";
                echo "Charge Credit Card AUTH CODE : " . $tresponse->getAuthCode() . "\n";
                echo "Charge Credit Card TRANS ID  : " . $tresponse->getTransId() . "\n";
                echo " Code : " . $tresponse->getMessages()[0]->getCode() . "\n";
                echo " Description : " . $tresponse->getMessages()[0]->getDescription() . "\n";
            }
            else
            {
                echo "Transaction Failed \n";
                if($tresponse->getErrors() != null)
                {
                    echo " Error code  : " . $tresponse->getErrors()[0]->getErrorCode() . "\n";
                    echo " Error message : " . $tresponse->getErrors()[0]->getErrorText() . "\n";
                }
            }
        }
        else
        {
            echo "Transaction Failed \n";
            $tresponse = $response->getTransactionResponse();
            if($tresponse != null && $tresponse->getErrors() != null)
            {
                echo " Error code  : " . $tresponse->getErrors()[0]->getErrorCode() . "\n";
                echo " Error message : " . $tresponse->getErrors()[0]->getErrorText() . "\n";
            }
            else
            {
                echo " Error code  : " . $response->getMessages()->getMessage()[0]->getCode() . "\n";
                echo " Error message : " . $response->getMessages()->getMessage()[0]->getText() . "\n";
            }
        }
    }
    else
    {
        echo  "No response returned \n";
    }`

The following example is much cleaner than the one on the main readme:

https://github.com/AuthorizeNet/sample-code-php/blob/master/PaymentTransactions/charge-credit-card.php

ashtru commented 7 years ago

Thank you @daniel-farina for your input. @globatum please reply if you were able to get this working or have further issues. We are closing the issue if you don't have further comments.