1
|
<?php
|
2
|
|
3
|
/**
|
4
|
* @file
|
5
|
* Provides an example payment method for Drupal Commerce for testing and
|
6
|
* development.
|
7
|
*/
|
8
|
|
9
|
|
10
|
/**
|
11
|
* Implements hook_commerce_payment_method_info().
|
12
|
*/
|
13
|
function commerce_payment_example_commerce_payment_method_info() {
|
14
|
$payment_methods = array();
|
15
|
|
16
|
$payment_methods['commerce_payment_example'] = array(
|
17
|
'title' => t('Example payment'),
|
18
|
'description' => t('Demonstrates credit card payment during checkout and serves as a development example.'),
|
19
|
'active' => TRUE,
|
20
|
);
|
21
|
|
22
|
return $payment_methods;
|
23
|
}
|
24
|
|
25
|
/**
|
26
|
* Payment method callback: submit form.
|
27
|
*/
|
28
|
function commerce_payment_example_submit_form($payment_method, $pane_values, $checkout_pane, $order) {
|
29
|
module_load_include('inc', 'commerce_payment', 'includes/commerce_payment.credit_card');
|
30
|
|
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'));
|
34
|
}
|
35
|
|
36
|
/**
|
37
|
* Payment method callback: submit form validation.
|
38
|
*/
|
39
|
function commerce_payment_example_submit_form_validate($payment_method, $pane_form, $pane_values, $order, $form_parents = array()) {
|
40
|
// Validate the credit card fields.
|
41
|
module_load_include('inc', 'commerce_payment', 'includes/commerce_payment.credit_card');
|
42
|
|
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)) {
|
53
|
return FALSE;
|
54
|
}
|
55
|
}
|
56
|
|
57
|
/**
|
58
|
* Payment method callback: submit form submission.
|
59
|
*/
|
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
|
|
67
|
$order->data['commerce_payment_example'] = $pane_values;
|
68
|
|
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);
|
72
|
}
|
73
|
|
74
|
/**
|
75
|
* Creates an example payment transaction for the specified charge amount.
|
76
|
*
|
77
|
* @param $payment_method
|
78
|
* The payment method instance object used to charge this payment.
|
79
|
* @param $order
|
80
|
* The order object the payment applies to.
|
81
|
* @param $charge
|
82
|
* An array indicating the amount and currency code to charge.
|
83
|
*/
|
84
|
function commerce_payment_example_transaction($payment_method, $order, $charge) {
|
85
|
$card_details = $order->data['commerce_payment_example']['credit_card'];
|
86
|
|
87
|
$transaction = commerce_payment_transaction_new('commerce_payment_example', $order->order_id);
|
88
|
$transaction->instance_id = $payment_method['instance_id'];
|
89
|
$transaction->amount = $charge['amount'];
|
90
|
$transaction->currency_code = $charge['currency_code'];
|
91
|
$transaction->status = COMMERCE_PAYMENT_STATUS_SUCCESS;
|
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
|
);
|
99
|
|
100
|
commerce_payment_transaction_save($transaction);
|
101
|
}
|