Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Commerce Marketplace Integration #1

Open
wants to merge 6 commits into
base: 7.x-1.x
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 25 additions & 0 deletions commerce_stripe.api.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
<?php
/**
*
* @param $charge
* This is the charge object which will be handed to Stripe_Charge::create().
* You can modify it according to the docs here: https://stripe.com/docs/api/php#charges
* @param $context is an array of variables, the context of the charge, including:
* $context['dest'] will be handed as the second paramenter to Stripe_Charge::create()
* $context['transaction'] is the transation object being created, for reference.
* $context['payment_method'] is the enabled payment method, included for reference.
*
*/
function hook_commerce_stripe_charge_alter(&$charge, &$transaction) {
// No example.
}

/**
*
* Modify the transaction before it's passed to commerce_payment_transaction_save.
* @param $transaction The transaction object to be modified.
* @param $response The response object from Stripe, for reference.
*/
function hook_commerce_stripe_transaction_alter(&$transaction, $response) {
// No example.
}
17 changes: 11 additions & 6 deletions commerce_stripe.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,11 @@
if (settings.stripe.fetched == null) {
settings.stripe.fetched = true;

var commercePaymentDom = 'commerce-payment';
if ($('form#commerce-checkout-form-review fieldset').length > 0) {
commercePaymentDom = 'commerce-marketplace-payment';
}

var createToken = function (cardFieldMap, responseHandler) {
Stripe.setPublishableKey(settings.stripe.publicKey);

Expand Down Expand Up @@ -72,7 +77,7 @@
// Prevent the Stripe actions to be triggered if Stripe is not selected.
if ($("input[value*='commerce_stripe|']").is(':checked')) {
// Do not fetch the token if cardonfile is enabled and the customer has selected an existing card.
if ($('.form-item-commerce-payment-payment-details-cardonfile').length) {
if ($('.form-item-' + commercePaymentDom + '-payment-details-cardonfile').length) {
// If select list enabled in card on file settings
if ($("select[name='commerce_payment[payment_details][cardonfile]']").length
&& $("select[name='commerce_payment[payment_details][cardonfile]'] option:selected").val() != 'new') {
Expand Down Expand Up @@ -105,11 +110,11 @@
$('.form-submit').attr("disabled", "disabled");

var cardFields = {
number: 'edit-commerce-payment-payment-details-credit-card-number',
cvc: 'edit-commerce-payment-payment-details-credit-card-code',
exp_month: 'edit-commerce-payment-payment-details-credit-card-exp-month',
exp_year: 'edit-commerce-payment-payment-details-credit-card-exp-year',
name: 'edit-commerce-payment-payment-details-credit-card-owner'
number: 'edit-' + commercePaymentDom + '-payment-details-credit-card-number',
cvc: 'edit-' + commercePaymentDom + '-payment-details-credit-card-code',
exp_month: 'edit-' + commercePaymentDom + '-payment-details-credit-card-exp-month',
exp_year: 'edit-' + commercePaymentDom + '-payment-details-credit-card-exp-year',
name: 'edit-' + commercePaymentDom + '-payment-details-credit-card-owner'
};

var responseHandler = makeResponseHandler(
Expand Down
43 changes: 31 additions & 12 deletions commerce_stripe.module
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ function commerce_stripe_libraries_info() {
'dependencies' => array(),
'version arguments' => array(
'file' => 'VERSION',
'pattern' => '/(1.\d+(\.\d+)?)/',
'pattern' => '/(.*)/',
),
'files' => array(
'php' => array(
Expand Down Expand Up @@ -157,7 +157,7 @@ function commerce_stripe_submit_form($payment_method, $pane_values, $checkout_pa
$order_wrapper = entity_metadata_wrapper('commerce_order', $order);
$field = field_info_field('commerce_customer_address');
$instance = field_info_instance('commerce_customer_profile', 'commerce_customer_address', 'billing');

$available_countries = NULL;
if (isset($form_state['input']['country'])) {
$available_countries = array($form_state['input']['country'] => NULL);
Expand Down Expand Up @@ -216,8 +216,7 @@ function _commerce_stripe_credit_card_field_remove_name($content, $element) {
*/
function commerce_stripe_submit_form_submit($payment_method, $pane_form, $pane_values, $order, $charge) {
// If instructed to do so, try using the specified card on file.
if (module_exists('commerce_cardonfile') && $payment_method['settings']['cardonfile'] &&
!empty($pane_values['cardonfile']) && $pane_values['cardonfile'] !== 'new') {
if (module_exists('commerce_cardonfile') && $payment_method['settings']['cardonfile'] && !empty($pane_values['cardonfile']) && $pane_values['cardonfile'] !== 'new') {
$card_data = commerce_cardonfile_load($pane_values['cardonfile']);
if (empty($card_data) || $card_data->status == 0) {
drupal_set_message(t('The requested card on file is no longer valid.'), 'error');
Expand Down Expand Up @@ -248,8 +247,7 @@ function commerce_stripe_submit_form_submit($payment_method, $pane_form, $pane_v
);

// To later store the card with all required fields, carry out necessary steps before making the charge request.
if (module_exists('commerce_cardonfile') && !empty($payment_method['settings']['cardonfile']) &&
!empty($pane_values['credit_card']['cardonfile_store']) && $pane_values['credit_card']['cardonfile_store']) {
if (module_exists('commerce_cardonfile') && !empty($payment_method['settings']['cardonfile']) && !empty($pane_values['credit_card']['cardonfile_store']) && $pane_values['credit_card']['cardonfile_store']) {
$card = _commerce_stripe_create_card($_POST['stripeToken'], $order->uid, $payment_method);

// If the card is not declined or otherwise is error-free, we can save it.
Expand All @@ -266,14 +264,25 @@ function commerce_stripe_submit_form_submit($payment_method, $pane_form, $pane_v
$transaction->instance_id = $payment_method['instance_id'];
$transaction->amount = $charge['amount'];
$transaction->currency_code = $currency_code;

//The dest parameter can be used to send the charge to a connected stripe account.
$context = array(
'transaction' => $transaction,
'dest' => array(),
'payment_method' => $payment_method,
);

drupal_alter('commerce_stripe_charge', $c, $context);

try {
// Stripe does not appreciate $0 transfers.
if ($charge['amount'] > 0) {
$response = Stripe_Charge::create($c);
$response = isset($context['dest']) ? Stripe_Charge::create($c, $context['dest']) : Stripe_Charge::create($c);
$transaction->remote_id = $response->id;
$transaction->payload[REQUEST_TIME] = $response->__toJSON();
$transaction->message = t('Payment completed successfully.');
$transaction->status = COMMERCE_PAYMENT_STATUS_SUCCESS;
drupal_alter('commerce_stripe_transaction', $transaction, $response);
commerce_payment_transaction_save($transaction);
$card_response = $response->card;
}
Expand Down Expand Up @@ -605,12 +614,23 @@ function commerce_stripe_load_library() {
}
else {
$minimum_version = '1.17.1';
if (version_compare($library['version'], $minimum_version) < 0 ) {
$message = "Commerce Stripe is currently tested with stripe-php library version @minimum_version. You are using version @installed_version, and you should update.";
$variables = array('@minimum_version' => $minimum_version, '@installed_version' => $library['version']);
$maximum_version = '1.18.0';
$message = "Commerce Stripe is currently tested with stripe-php library versions @minimum_version through @maximum_version. You are using version @installed_version, and you should @upgrade_or_downgrade.";

//check that it's not lower than the minimum required version
if (version_compare($library['version'], $minimum_version, '<')) {
$variables = array('@minimum_version' => $minimum_version, '@maximum_version' => $maximum_version, '@installed_version' => $library['version'], '@upgrade_or_downgrade' => 'upgrade');
watchdog('commerce_stripe', $message, $variables, WATCHDOG_WARNING);
return FALSE;
}

//check that it's not higher than the maximum tested version
elseif (version_compare($library['version'], $maximum_version, '>')) {
$variables = array('@minimum_version' => $minimum_version, '@maximum_version' => $maximum_version, '@installed_version' => $library['version'], '@upgrade_or_downgrade' => 'downgrade');
watchdog('commerce_stripe', $message, $variables, WATCHDOG_WARNING);
return FALSE;
}

return TRUE;
}
}
Expand All @@ -632,7 +652,6 @@ function commerce_stripe_customer_id($uid, $instance_id) {
$card_data = reset($stored_cards);
list($customer_id, $card_id) = explode('|', $card_data->remote_id);
}

return !empty($customer_id) ? $customer_id : FALSE;
}

Expand All @@ -647,7 +666,7 @@ function commerce_stripe_customer_id($uid, $instance_id) {
*/
function commerce_stripe_field_widget_addressfield_standard_form_alter(&$element, &$form_state, $context) {
if (!$context['field']['field_name'] == 'commerce_customer_address') {
return;
return;
}
commerce_stripe_set_addressfield_class_names($element);
}
Expand Down