You are here

function ucRecurringPaypalWPSTestCase::testRecurringPaypal in UC Recurring Payments and Subscriptions 6.2

Same name and namespace in other branches
  1. 7.2 modules/uc_recurring_hosted/uc_recurring_hosted.test \ucRecurringPaypalWPSTestCase::testRecurringPaypal()

Place an order with the Paypal WPS gateway.

File

modules/uc_recurring_hosted/uc_recurring_hosted.test, line 29
UC Recurring paypal simpletest

Class

ucRecurringPaypalWPSTestCase
Test payment gateway api fuctions in uc_recurring.

Code

function testRecurringPaypal() {
  $this
    ->drupalLogin($this->user_recurring_admin);
  $settings = array(
    'uc_payment_method_paypal_wps_checkout' => TRUE,
    'uc_paypal_wps_email' => 'paypal@example.com',
  );
  $this
    ->drupalPost('admin/store/settings/payment/edit/methods', $settings, t('Save configuration'));

  // Select mock gateway for payments.
  $settings = array(
    'uc_recurring_payment_methods[paypal_wps]' => TRUE,
  );
  $this
    ->drupalPost('admin/store/settings/payment/edit/recurring', $settings, t('Save configuration'));

  // Create products with recurring features.
  $product = $this
    ->createProduct(array(
    'sell_price' => 20.0,
  ));
  $recurring_feature = $this
    ->createRecurringFeature($product->nid, array(
    'unlimited_intervals' => TRUE,
    'fee_amount' => 50,
  ));
  $this
    ->drupalLogout();
  variable_set('uc_paypal_wps_server', url('uc_recurring_hosted/paypal/cgi-bin/webscr', array(
    'absolute' => TRUE,
  )));
  variable_set('uc_paypal_wps_checkout_button', t('Submit order'));
  variable_set('uc_paypal_wps_debug_ipn', TRUE);
  $this
    ->drupalPost('node/' . $product->nid, array(), t('Add to cart'));
  $this
    ->assertRaw($product->title, t('The product name has been displayed on the cart page.'));
  $this
    ->assertRaw('added to', t('The product name has been displayed on the cart page.'));
  $this
    ->checkout(array(
    'panes[delivery][delivery_postal_code]' => '12345',
    'panes[billing][billing_postal_code]' => '12345',
  ));
  $order_id = $this
    ->lastCreatedOrderId();
  $this
    ->assertEqual($order_id, 1, t('First order created.'));
  $url = url('uc_recurring_hosted/paypal/ipn/' . $order_id, array(
    'absolute' => TRUE,
  ));
  $headers = array(
    'Content-Type' => 'application/x-www-form-urlencoded',
  );
  $post = $this
    ->paypal_message('subscr_signup', $order_id);
  $response = drupal_http_request($url, $headers, 'POST', http_build_query($post, '', '&'));
  $order = uc_order_load($order_id);
  $post = $this
    ->paypal_message('subscr_payment', $order_id, 20);
  $response = drupal_http_request($url, $headers, 'POST', http_build_query($post, '', '&'));
  $new_order_id = $this
    ->lastCreatedOrderId();
  $order = uc_order_load($new_order_id);
  $this
    ->assertTrue(uc_payment_balance($order) <= 0, t('Order @order_id payment processed', array(
    '@order_id' => $new_order_id,
  )));

  // Renew order.
  $post = $this
    ->paypal_message('subscr_payment', $order_id, 50);
  $response = drupal_http_request($url, $headers, 'POST', http_build_query($post, '', '&'));
  $new_order_id = $this
    ->lastCreatedOrderId();
  $order = uc_order_load($new_order_id);
  $this
    ->assertTrue(uc_payment_balance($order) <= 0, t('Order @order_id payment processed', array(
    '@order_id' => $new_order_id,
  )));
  $this
    ->assertEqual($order_id + 1, $new_order_id, t('New order created for renewal'));

  // Handle failed payment.
  $post = $this
    ->paypal_message('subscr_failed', $order_id, 50);
  $response = drupal_http_request($url, $headers, 'POST', http_build_query($post, '', '&'));
  $new_order_id = $this
    ->lastCreatedOrderId();
  $this
    ->assertEqual($order_id + 1, $new_order_id, t('No new order for failed payment'));

  // Handle cancellation.
  $post = $this
    ->paypal_message('subscr_cancel', $order_id, 50);
  $response = drupal_http_request($url, $headers, 'POST', http_build_query($post, '', '&'));
  $order = uc_order_load($new_order_id);
  $fee = $this
    ->getSingleRecurringFeeFromOrder($order_id);
  $this
    ->assertEqual($fee->number_intervals, 0, t('Order has been cancelled'));
  module_load_include('inc', 'uc_order', 'uc_order.admin');
  $this
    ->verbose(uc_order_log($order));
}