Soumik
Soumik

Reputation: 137

How to get customerpaymentrofileId from an existing customerprofileid in authorize.net

How to get customerpaymentrofileId from an existing customerprofileid in authorize.net I am a novice in autorize.net . I need to get the customerpaymentrofileId of a customer's customerprofileid , ao that i can use them to charge for a transaction in Authorize.net.

$email="[email protected]";
        // Common setup for API credentials
      $merchantAuthentication = new AnetAPI\MerchantAuthenticationType();
      $merchantAuthentication->setName("1212121");
      $merchantAuthentication->setTransactionKey("12121221");
      $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);

  // 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");

 // Create a Customer Profile Request
 //  1. create a Payment Profile
 //  2. create a Customer Profile   
 //  3. Submit a CreateCustomerProfile Request
 //  4. Validate Profiiel ID returned

        $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 1");

  //$customerprofile->setMerchantCustomerId("M_".time());
  $customerprofile->setEmail($email."d");
  $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;

  echo "response=> <pre>"; print_r($response); echo "</pre>";

Upvotes: 0

Views: 469

Answers (1)

KiKMak
KiKMak

Reputation: 830

You can use the GetCustomerProfile request for a profile id to get the list of payment profiles, the response will contain list of payment profiles.

For php implementation you can look into this PHP sample code : https://github.com/AuthorizeNet/sample-code-php/blob/master/CustomerProfiles/get-customer-profile.php

Upvotes: 1

Related Questions