/** * 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); }
/** * This is helper function called inside createProgrammatically used for order payment transaction * @param $order * This is order object */ private function paymentTransaction($order) { $transaction = commerce_payment_transaction_new('commerce_stripe', $order->order_id); $payment_method = commerce_payment_method_instance_load('commerce_stripe|commerce_payment_commerce_stripe'); $strip_token = Utils::getStripeToken()->verify(get_class()); $charge = $order->commerce_order_total['und'][0]; if (!commerce_stripe_load_library()) { drupal_set_message(t('Error capturing payment. Please contact shop admin to proceed.'), 'error'); } $c = array('amount' => $charge['amount'], 'currency' => $charge['currency_code'], 'card' => $strip_token, 'capture' => TRUE, 'description' => t('Order Number: @order_number', array('@order_number' => $order->order_number))); \Stripe::setApiKey($payment_method['settings']['secret_key']); try { if ($charge['amount'] > 0) { $response = \Stripe_Charge::create($c); $transaction->remote_id = $response->id; $transaction->payload[REQUEST_TIME] = $response->__toJSON(); $transaction->remote_status = 'AUTH_CAPTURE'; $transaction->message = t('Payment authorized only successfully.'); $transaction->message .= '<br />' . t('Captured: @date', array('@date' => format_date(REQUEST_TIME, 'short'))); $transaction->message .= '<br />' . t('Captured Amount: @amount', array('@amount' => $charge['amount'] / 100)); $transaction->status = COMMERCE_PAYMENT_STATUS_SUCCESS; $transaction->amount = $charge['amount']; $transaction->currency_code = $charge['currency_code']; sleep(5); commerce_payment_transaction_save($transaction); commerce_payment_commerce_payment_transaction_insert($transaction); } } catch (Exception $e) { drupal_set_message(t('We received the following error when trying to capture the transaction.'), 'error'); drupal_set_message(check_plain($e->getMessage()), 'error'); $transaction->payload[REQUEST_TIME] = $e->json_body; $transaction->message = t('Capture processing error: @stripe_error', array('@stripe_error' => $e->getMessage())); $transaction->status = COMMERCE_PAYMENT_STATUS_FAILURE; $transaction->remote_status = 'FAILED'; commerce_payment_transaction_save($transaction); } if ($charge['amount'] == 0) { $user = user_load($order->uid); $transaction->instance_id = $payment_method['instance_id']; $transaction->amount = $charge['amount']; $transaction->currency_code = $charge['currency_code']; $transaction->status = COMMERCE_PAYMENT_STATUS_SUCCESS; $transaction->message = '@name'; $transaction->message_variables = array('@name' => 'Payment authorized only successfully'); commerce_payment_transaction_save($transaction); commerce_payment_commerce_payment_transaction_insert($transaction); } if (module_exists('commerce_cardonfile')) { $strip_token = Utils::getStripeToken()->verify(get_class()); $card = _commerce_stripe_create_card($strip_token, $order->uid, $payment_method); $remote_id = (string) $card->customer . '|' . (string) $card->id; $card_data = commerce_cardonfile_new(); $card_data->uid = $order->uid; $card_data->order_id = $order->order_id; $card_data->payment_method = $payment_method['method_id']; $card_data->instance_id = $payment_method['instance_id']; $card_data->remote_id = $remote_id; $card_data->card_type = 'Visa'; $card_data->card_name = $user->name; $card_data->card_number = '1111'; $card_data->card_exp_month = 5; $card_data->card_exp_year = 2018; $card_data->status = 1; $card_data->instance_default = 1; commerce_cardonfile_save($card_data); } }