Projet

Général

Profil

Révision b858700c

Ajouté par Assos Assos il y a environ 10 ans

Weekly update of contrib modules

Voir les différences:

drupal7/sites/all/modules/commerce/modules/payment/modules/commerce_payment_example.module
15 15

  
16 16
  $payment_methods['commerce_payment_example'] = array(
17 17
    'title' => t('Example payment'),
18
    'description' => t('Demonstrates complete payment during checkout and serves as a development example.'),
18
    'description' => t('Demonstrates credit card payment during checkout and serves as a development example.'),
19 19
    'active' => TRUE,
20 20
  );
21 21

  
......
26 26
 * Payment method callback: submit form.
27 27
 */
28 28
function commerce_payment_example_submit_form($payment_method, $pane_values, $checkout_pane, $order) {
29
  $form = array();
29
  module_load_include('inc', 'commerce_payment', 'includes/commerce_payment.credit_card');
30 30

  
31
  // Merge in values from the order.
32
  if (!empty($order->data['commerce_payment_example'])) {
33
    $pane_values += $order->data['commerce_payment_example'];
34
  }
35

  
36
  // Merge in default values.
37
  $pane_values += array(
38
    'name' => '',
39
  );
40

  
41
  $form['name'] = array(
42
    '#type' => 'textfield',
43
    '#title' => t('Name'),
44
    '#description' => t('This is a demonstration field coded to fail validation for single character values.'),
45
    '#default_value' => $pane_values['name'],
46
    '#required' => TRUE,
47
  );
48

  
49
  return $form;
31
  // Default to a known test credit card number. For valid numbers of other card
32
  // types see: http://www.rimmkaufman.com/blog/credit-card-test-numbers/09112007/
33
  return commerce_payment_credit_card_form(array(), array('number' => '4111111111111111'));
50 34
}
51 35

  
52 36
/**
53 37
 * Payment method callback: submit form validation.
54 38
 */
55 39
function commerce_payment_example_submit_form_validate($payment_method, $pane_form, $pane_values, $order, $form_parents = array()) {
56
  // Throw an error if a long enough name was not provided.
57
  if (strlen($pane_values['name']) < 2) {
58
    form_set_error(implode('][', array_merge($form_parents, array('name'))), t('You must enter a name two or more characters long.'));
40
  // Validate the credit card fields.
41
  module_load_include('inc', 'commerce_payment', 'includes/commerce_payment.credit_card');
59 42

  
60
    // Even though the form error is enough to stop the submission of the form,
61
    // it's not enough to stop it from a Commerce standpoint because of the
62
    // combined validation / submission going on per-pane in the checkout form.
43
  $settings = array(
44
    'form_parents' => array_merge($form_parents, array('credit_card')),
45
  );
46

  
47
  // Even though a form error triggered by the validate handler would be enough
48
  // to stop the submission of the form, it's not enough to stop it from a
49
  // Commerce standpoint because of the combined validation / submission going
50
  // on per-pane in the checkout form. Thus even with a call to form_set_error()
51
  // this validate handler must still return FALSE.
52
  if (!commerce_payment_credit_card_validate($pane_values['credit_card'], $settings)) {
63 53
    return FALSE;
64 54
  }
65 55
}
......
68 58
 * Payment method callback: submit form submission.
69 59
 */
70 60
function commerce_payment_example_submit_form_submit($payment_method, $pane_form, $pane_values, $order, $charge) {
61
  // Just as an example, we might store information in the order object from the
62
  // payment parameters, though we would never save a full credit card number,
63
  // even in examples!
64
  $number = $pane_values['credit_card']['number'];
65
  $pane_values['credit_card']['number'] = substr($number, 0, 4) . str_repeat('-', strlen($number) - 8) . substr($number, -4);
66

  
71 67
  $order->data['commerce_payment_example'] = $pane_values;
72 68

  
73
  commerce_payment_example_transaction($payment_method, $order, $charge, $pane_values['name']);
69
  // Every attempted transaction should result in a new transaction entity being
70
  // created for the order to log either the success or the failure.
71
  commerce_payment_example_transaction($payment_method, $order, $charge);
74 72
}
75 73

  
76 74
/**
......
82 80
 *   The order object the payment applies to.
83 81
 * @param $charge
84 82
 *   An array indicating the amount and currency code to charge.
85
 * @param $name
86
 *   The name entered on the submission form.
87 83
 */
88
function commerce_payment_example_transaction($payment_method, $order, $charge, $name) {
84
function commerce_payment_example_transaction($payment_method, $order, $charge) {
85
  $card_details = $order->data['commerce_payment_example']['credit_card'];
86

  
89 87
  $transaction = commerce_payment_transaction_new('commerce_payment_example', $order->order_id);
90 88
  $transaction->instance_id = $payment_method['instance_id'];
91 89
  $transaction->amount = $charge['amount'];
92 90
  $transaction->currency_code = $charge['currency_code'];
93 91
  $transaction->status = COMMERCE_PAYMENT_STATUS_SUCCESS;
94
  $transaction->message = 'Name: @name';
95
  $transaction->message_variables = array('@name' => $name);
92

  
93
  $transaction->message = 'Number: @number<br/>Expiration: @month/@year';
94
  $transaction->message_variables = array(
95
    '@number' => $card_details['number'],
96
    '@month' => $card_details['exp_month'],
97
    '@year' => $card_details['exp_year'],
98
  );
96 99

  
97 100
  commerce_payment_transaction_save($transaction);
98 101
}

Formats disponibles : Unified diff