Commit a8ca1624 authored by Jordan Magnuson's avatar Jordan Magnuson
Browse files

Issue #2096185 by markdorison, quicksketch, JordanMagnuson, mrded, loze,...

Issue #2096185 by markdorison, quicksketch, JordanMagnuson, mrded, loze, JurriaanRoelofs: Add RecurlyJS PayPal support
parent 68117d33
Loading
Loading
Loading
Loading
+32 −0
Original line number Diff line number Diff line
@@ -223,6 +223,21 @@ function recurlyjs_subscribe_form($form, $form_state, $entity_type, $entity, $pl
    );
  }

  // Payment method (PayPal support).
  $accept_paypal = variable_get('recurlyjs_accept_paypal', FALSE);
  if ($accept_paypal) {
    $form['payment_method'] = array(
      '#title' => t('Payment method'),
      '#type' => 'radios',
      '#options' => array('credit_card' => t('Credit Card'), 'paypal' => t('PayPal')),
      '#default_value' => 'credit_card',
    );

    $form['paypal'] = array(
      '#markup' => '<div class="recurlyjs-element recurlyjs-paypal-button"><input '. drupal_attributes(array('type' => 'button', 'value' => t('Check out with PayPal') )) .' class="button form-button" id="paypal-button" /></div>',
    );
  }

  _recurlyjs_append_billing_fields($form);
  _recurlyjs_form_attach_js($form);

@@ -373,6 +388,23 @@ function recurlyjs_update_billing_form($form, $form_state, $entity_type, $entity
    return $form;
  }

  // PayPal subscriptions can't be updated locally. Users must visit PayPal.com.
  $is_paypal_subscription = !empty($billing_info->paypal_billing_agreement_id);
  if ($is_paypal_subscription) {
    $form = array();
    $form['paypal_message'] = array(
      '#markup' => t('<p>Because your subscription was created through PayPal, you must log into your PayPal account to update your billing information.</p>'),
    );
    $form['paypal_link'] = array(
      '#markup' => '<p><a id="paypal-update-link" class="button form-button" href="https://www.paypal.com/myaccount/money">Click here to update your payment details at PayPal.com</a></p>',
    );
    $form['help_link'] = array(
      '#markup' => '<p>For assistance please visit the <a href="https://www.paypal.com/bz/selfhelp/topic/UPDATE_ACCOUNT_INFO_MYACCOUNT">PayPal Help Center</a>.</p>',
    );

    return $form;
  }

  $form['#entity_type'] = $entity_type;
  $form['#entity'] = $entity;

+42 −8
Original line number Diff line number Diff line
@@ -12,6 +12,35 @@ Drupal.behaviors.recurlyJSSubscribeForm = {
    $('#recurlyjs-subscribe-form').once('recurlyjs-subscribe-form', function () {
      $(this).bind('submit', Drupal.recurly.recurlyJSTokenFormSubmit);
    });

    // Show/hide Payment methods based on radio selection.
    $('.recurlyjs-paypal-button').hide();
    $('#recurlyjs-subscribe-form input[type=radio][name=payment_method]').change(function() {
      if (this.value == 'credit_card') {
        $('#edit-billing').show();
        $('#edit-submit').show();
        $('.recurlyjs-paypal-button').hide();
      }
      else if (this.value == 'paypal') {
        $('#edit-billing').hide();
        $('#edit-submit').hide();
        $('.recurlyjs-paypal-button').show();
      }
    });

    // Check out with PayPal.
    $('#paypal-button').click(function() {
      var order_total = $('.recurlyjs-total').text();
      var opts = { description: order_total };
        recurly.paypal(opts, function (err, token) {
        if (err) {
          Drupal.recurly.recurlyJSFormError(err);
        } else {
          $('input[name="recurly-token"]').val(token.id);
          $('#recurlyjs-subscribe-form').submit();
        }
      });
    });
  }
};

@@ -37,6 +66,10 @@ Drupal.recurly.recurlyJSTokenFormSubmit = function(event) {
  $('button').attr('disabled', true);

  var form = this;
  if($('input[name="recurly-token"]').val()!='') {
    // If we already have a token (e.g. returned from PayPal) submit the form.
    form.submit();
  } else {
    recurly.token(form, function (err, token) {
      if (err) {
        Drupal.recurly.recurlyJSFormError(err);
@@ -45,6 +78,7 @@ Drupal.recurly.recurlyJSTokenFormSubmit = function(event) {
        form.submit();
      }
    });
  }
};

/**