public static function CreateCustomerProfile(Payload $payload)
 {
     // Common setup for API credentials
     $merchantAuthentication = new AnetAPI\MerchantAuthenticationType();
     $merchantAuthentication->setName(config('subscription.API_LOGIN_ID'));
     $merchantAuthentication->setTransactionKey(config('subscription.TRANSACTION_KEY'));
     $refId = 'ref' . time();
     // Create the payment data for a credit card
     $creditCard = new AnetAPI\CreditCardType();
     $creditCard->setCardNumber($payload->cardNumber);
     $creditCard->setExpirationDate($payload->expiryDate);
     $paymentCreditCard = new AnetAPI\PaymentType();
     $paymentCreditCard->setCreditCard($creditCard);
     // Create the Bill To info
     $billto = new AnetAPI\CustomerAddressType();
     $billto->setFirstName($payload->firstName);
     $billto->setLastName($payload->lastName);
     $billto->setCompany($payload->company);
     $billto->setAddress($payload->address);
     $billto->setCity($payload->city);
     $billto->setState($payload->state);
     $billto->setZip($payload->zip);
     $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($payload->description);
     $customerprofile->addToShipToList($billto);
     $merchantCustomerId = time() . rand(1, 150);
     $customerprofile->setMerchantCustomerId($merchantCustomerId);
     $customerprofile->setEmail($payload->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") {
         return $response;
     } else {
         throw new PaymentErrorException($response->getMessages()->getMessage()[0]->getText());
     }
 }
Beispiel #2
0
 /**
  * Executes some API calls and obtains a customer id
  * @method customerId
  * @return {string} The customer id
  */
 function customerId()
 {
     $options = $this->options;
     // Common Set Up for API Credentials
     $merchantAuthentication = new AnetAPI\MerchantAuthenticationType();
     $merchantAuthentication->setName($options['authname']);
     $merchantAuthentication->setTransactionKey($options['authkey']);
     $refId = 'ref' . time();
     $user = $options['user'];
     $merchantCustomerId = $user->id;
     $customer = new Assets_Customer();
     $customer->userId = $user->id;
     $customer->payments = 'authnet';
     if ($customer->retrieve()) {
         return $customer->customerId;
     }
     $customerprofile = new AnetAPI\CustomerProfileType();
     $customerprofile->setMerchantCustomerId($merchantCustomerId);
     $customerprofile->setDescription($user->displayName());
     $customerprofile->setEmail($user->emailAddress);
     $request = new AnetAPI\CreateCustomerProfileRequest();
     $request->setMerchantAuthentication($merchantAuthentication);
     $request->setRefId($refId);
     $request->setProfile($customerprofile);
     $controller = new AnetController\CreateCustomerProfileController($request);
     $response = $controller->executeWithApiResponse($options['server']);
     if ($response != null && $response->getMessages()->getResultCode() == "Ok") {
         return $response->getCustomerProfileId();
     }
     if ($response != null && $response->getMessages()->getResultCode() == "Ok") {
         $customerId = $response->getCustomerProfileId();
     } else {
         $messages = $response->getMessages()->getMessage();
         $message = reset($messages);
         // workaround to get customerProfileId
         // https://community.developer.authorize.net/t5/Integration-and-Testing/How-to-lookup-customerProfileId-and-paymentProfileId-by/td-p/52501
         if (isset($response) and $message->getCode() != "E00039") {
             throw new Assets_Exception_InvalidResponse(array('response' => $message->getCode() . ' ' . $message->getText()));
         }
         $parts = explode(' ', $message->getText());
         $customerId = $parts[5];
     }
     $customer->customerId = $customerId;
     $customer->save();
     return $customerId;
 }
// Create a Customer Profile Request
//  1. create a Payment Profile
//  2. create a Customer Profile
//  3. Submit a CreateCustomerProfile Request
$paymentprofile = new AnetAPI\CustomerPaymentProfileType();
$paymentprofile->setCustomerType('individual');
$paymentprofile->setBillTo($billto);
$paymentprofile->setPayment($paymentCreditCard);
$paymentprofiles[] = $paymentprofile;
$customerprofile = new AnetAPI\CustomerProfileType();
$customerprofile->setDescription("Create Customer Profile Request Test for PHP");
$merchantCustomerId = time() . rand(1, 150);
$customerprofile->setMerchantCustomerId($merchantCustomerId);
$customerprofile->setEmail("*****@*****.**");
$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 "SUCCESS: CreateCustomerProfile PROFILE ID : " . $response->getCustomerProfileId() . "\n";
    $profileidcreated = $response->getCustomerProfileId();
} else {
    echo "ERROR :  CreateCustomerProfile: Invalid response\n";
}
// Delete an existing customer profile
$request = new AnetAPI\DeleteCustomerProfileRequest();
$request->setMerchantAuthentication($merchantAuthentication);
$request->setCustomerProfileId($profileidcreated);