/**
  * Default constructor for the Commerce Customer Profile.
  *
  * @param int $order_id
  *   Order id if an existing order is to be loaded.
  */
 public function __construct($profile_id = NULL)
 {
     if (!is_null($profile_id) && is_numeric($profile_id)) {
         $profile = commerce_customer_profile_load($profile_id);
         if (!$profile) {
             $this->setErrors("Profile with id {$profile} does not exist.");
             $this->setInitialized(FALSE);
             return;
         }
     } else {
         global $user;
         $class = new \ReflectionClass(get_called_class());
         $type = Utils::makeSnakeCase($class->getShortName());
         $profile = commerce_customer_profile_new($type, $user->uid);
     }
     parent::__construct($profile);
 }
 /**
  * Create or update a card with a web service response
  */
 protected function processCardResponse(&$context)
 {
     // Exit if not enough info
     if (empty($context['response']) || empty($context['state'])) {
         return;
     }
     // Exit if request failed
     if (empty($context['response']['transaction_approved'])) {
         return;
     }
     $response = $context['response'];
     $state =& $context['state'];
     // Exit if no transarmor response
     if (empty($response['transarmor_token'])) {
         return;
     }
     // Extract card values from the response
     $new_values = $this->createResponseCardValues($response, $state);
     // Reference card and create new if needed
     $card =& $state['card'];
     if (empty($card)) {
         // New cards
         $card = commerce_cardonfile_new($new_values);
         $new_values = array();
     } elseif (!empty($card->card_id)) {
         // Existing card - only care if remote id changed or type was determined
         $new_values = array_intersect_key($new_values, array('remote_id' => 1, 'card_type' => 1));
     }
     // Exit if set not to store
     if (!empty($card->no_store)) {
         return;
     }
     // Determine new billing profile
     $new_billing_profile = NULL;
     $existing_billing_profile = NULL;
     if (!empty($card->commerce_cardonfile_profile[LANGUAGE_NONE][0]['profile_id'])) {
         $existing_billing_profile = commerce_customer_profile_load($card->commerce_cardonfile_profile[LANGUAGE_NONE][0]['profile_id']);
     }
     if (!empty($state['billing_address'])) {
         $create_billing = TRUE;
         if (!empty($existing_billing_profile)) {
             $existing_billing_profile_wrapper = entity_metadata_wrapper('commerce_customer_profile', $existing_billing_profile);
             if ($this->controller->addressIsEqual($state['billing_address'], $existing_billing_profile_wrapper->commerce_customer_address->value())) {
                 $create_billing = FALSE;
             }
         }
         if ($create_billing) {
             if (!empty($state['billing_profile_id'])) {
                 $resolved_billing_profile = commerce_customer_profile_load($state['billing_profile_id']);
                 $resolved_billing_profile_wrapper = entity_metadata_wrapper('commerce_customer_profile', $resolved_billing_profile);
                 if ($this->controller->addressIsEqual($state['billing_address'], $resolved_billing_profile_wrapper->commerce_customer_address->value())) {
                     $new_billing_profile = $resolved_billing_profile;
                 }
             }
             if (!$new_billing_profile) {
                 $new_billing_profile = commerce_customer_profile_new('billing', $card->uid);
                 $new_billing_profile_wrapper = entity_metadata_wrapper('commerce_customer_profile', $new_billing_profile);
                 $new_billing_profile_wrapper->commerce_customer_address = $state['billing_address'] + addressfield_default_values();
             }
         }
     }
     // If new card ...
     if (empty($card->card_id)) {
         // Check required properties
         if (empty($card->uid) || empty($card->payment_method) || empty($card->instance_id)) {
             /** @todo: ??? **/
             return;
         }
     }
     // Create / Update the card
     $this->saveCard($card, $new_billing_profile, $new_values);
 }
 /**
  * Relay response: order processing
  */
 protected function relayResponseProcessOrder(&$context)
 {
     $response = $context['response'];
     $state =& $context['state'];
     // Exit if no order
     if (empty($response['commerce_order_id'])) {
         return;
     }
     // Reference order in resolved state
     $order =& $state['order'];
     $order_wrapper = entity_metadata_wrapper('commerce_order', $order);
     // Save transaction
     $transaction = $this->controller->saveTransaction($response, $state);
     if ($transaction) {
         $state['transaction'] = $transaction;
     }
     // If this order does not have a billing profile yet ...
     if ($order_wrapper->commerce_customer_billing->value() === NULL) {
         // Create new and set address to resolved billing address from response
         $billing_profile = commerce_customer_profile_new('billing', $order->uid);
         $billing_profile_wrapper = entity_metadata_wrapper('commerce_customer_profile', $billing_profile);
         $billing_profile_wrapper->commerce_customer_address = $state['billing_address'] + addressfield_default_values();
         $billing_profile_wrapper->save();
         $order_wrapper->commerce_customer_billing = $billing_profile_wrapper;
         $order_wrapper->save();
         watchdog('commerce_firstdata_gge4', 'Billing profile created for Order @order_number containing the address data from the offsite Hosted Payment Page.', array('@order_number' => $order->order_number));
     }
     /** @todo need this or checkout pane handle it? paypal does this ... **/
     if (!empty($response['transaction_approved'])) {
         // Send the customer on to the next checkout page.
         commerce_payment_redirect_pane_next_page($order, t('Customer successfully submitted payment at the payment gateway.'));
     } else {
         // Otherwise send the customer back.
         commerce_payment_redirect_pane_previous_page($order, t('Customer payment submission failed at the payment gateway.'));
     }
 }
 /**
  * @Given /^customer profiles:$/
  */
 public function createCustomerProfiles(TableNode $nodesTable)
 {
     foreach ($nodesTable->getHash() as $nodeHash) {
         $profile = commerce_customer_profile_new('billing', isset($this->user->uid) ? $this->user->uid : 0);
         $wrapper = entity_metadata_wrapper('commerce_customer_profile', $profile);
         if (isset($nodeHash['country'])) {
             $wrapper->commerce_customer_address->country = $nodeHash['country'];
         }
         if (isset($nodeHash['name'])) {
             $wrapper->commerce_customer_address->name_line = $nodeHash['name'];
         }
         if (isset($nodeHash['address'])) {
             $wrapper->commerce_customer_address->thoroughfare = $nodeHash['address'];
         }
         if (isset($nodeHash['locality'])) {
             $wrapper->commerce_customer_address->locality = $nodeHash['locality'];
         }
         if (isset($nodeHash['postal_code'])) {
             $wrapper->commerce_customer_address->postal_code = $nodeHash['postal_code'];
         }
         $wrapper->save();
         if (isset($nodeHash['profile_id'])) {
             $this->customerProfiles[$nodeHash['profile_id']] = $wrapper->profile_id->value();
         } else {
             $this->customerProfiles[] = $wrapper->profile_id->value();
         }
     }
 }