|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
class WC_Stripe_Test extends WP_UnitTestCase { |
|
4
|
|
|
public function test_constants_defined() { |
|
5
|
|
|
$this->assertTrue( defined( 'WC_STRIPE_VERSION' ) ); |
|
6
|
|
|
$this->assertTrue( defined( 'WC_STRIPE_MIN_PHP_VER' ) ); |
|
7
|
|
|
$this->assertTrue( defined( 'WC_STRIPE_MIN_WC_VER' ) ); |
|
8
|
|
|
$this->assertTrue( defined( 'WC_STRIPE_MAIN_FILE' ) ); |
|
9
|
|
|
$this->assertTrue( defined( 'WC_STRIPE_PLUGIN_URL' ) ); |
|
10
|
|
|
$this->assertTrue( defined( 'WC_STRIPE_PLUGIN_PATH' ) ); |
|
11
|
|
|
} |
|
12
|
|
|
|
|
13
|
|
|
/** |
|
14
|
|
|
* Stripe requires price in the smallest dominations aka cents. |
|
15
|
|
|
* This test will see if we're indeed converting the price correctly. |
|
16
|
|
|
*/ |
|
17
|
|
|
public function test_price_conversion_before_send_to_stripe() { |
|
18
|
|
|
$this->assertEquals( 10050, WC_Stripe_Helper::get_stripe_amount( 100.50, 'USD' ) ); |
|
19
|
|
|
$this->assertEquals( 10050, WC_Stripe_Helper::get_stripe_amount( 10050, 'JPY' ) ); |
|
20
|
|
|
$this->assertEquals( 100, WC_Stripe_Helper::get_stripe_amount( 100.50, 'JPY' ) ); |
|
21
|
|
|
$this->assertEquals( 10050, WC_Stripe_Helper::get_stripe_amount( 100.50 ) ); |
|
22
|
|
|
$this->assertInternalType( 'int', WC_Stripe_Helper::get_stripe_amount( 100.50, 'USD' ) ); |
|
23
|
|
|
} |
|
24
|
|
|
|
|
25
|
|
|
/** |
|
26
|
|
|
* We store balance fee/net amounts coming from Stripe. |
|
27
|
|
|
* We need to make sure we format it correctly to be stored in WC. |
|
28
|
|
|
* These amounts are posted in lowest dominations. |
|
29
|
|
|
*/ |
|
30
|
|
|
public function test_format_balance_fee() { |
|
31
|
|
|
$balance_fee1 = new stdClass(); |
|
32
|
|
|
$balance_fee1->fee = 10500; |
|
33
|
|
|
$balance_fee1->net = 10000; |
|
34
|
|
|
$balance_fee1->currency = 'USD'; |
|
35
|
|
|
|
|
36
|
|
|
$this->assertEquals( 105.00, WC_Stripe_Helper::format_balance_fee( $balance_fee1, 'fee' ) ); |
|
37
|
|
|
|
|
38
|
|
|
$balance_fee2 = new stdClass(); |
|
39
|
|
|
$balance_fee2->fee = 10500; |
|
40
|
|
|
$balance_fee2->net = 10000; |
|
41
|
|
|
$balance_fee2->currency = 'JPY'; |
|
42
|
|
|
|
|
43
|
|
|
$this->assertEquals( 10500, WC_Stripe_Helper::format_balance_fee( $balance_fee2, 'fee' ) ); |
|
44
|
|
|
|
|
45
|
|
|
$balance_fee3 = new stdClass(); |
|
46
|
|
|
$balance_fee3->fee = 10500; |
|
47
|
|
|
$balance_fee3->net = 10000; |
|
48
|
|
|
$balance_fee3->currency = 'USD'; |
|
49
|
|
|
|
|
50
|
|
|
$this->assertEquals( 100.00, WC_Stripe_Helper::format_balance_fee( $balance_fee3, 'net' ) ); |
|
51
|
|
|
|
|
52
|
|
|
$balance_fee4 = new stdClass(); |
|
53
|
|
|
$balance_fee4->fee = 10500; |
|
54
|
|
|
$balance_fee4->net = 10000; |
|
55
|
|
|
$balance_fee4->currency = 'JPY'; |
|
56
|
|
|
|
|
57
|
|
|
$this->assertEquals( 10000, WC_Stripe_Helper::format_balance_fee( $balance_fee4, 'net' ) ); |
|
58
|
|
|
|
|
59
|
|
|
$balance_fee5 = new stdClass(); |
|
60
|
|
|
$balance_fee5->fee = 10500; |
|
61
|
|
|
$balance_fee5->net = 10000; |
|
62
|
|
|
$balance_fee5->currency = 'USD'; |
|
63
|
|
|
|
|
64
|
|
|
$this->assertEquals( 105.00, WC_Stripe_Helper::format_balance_fee( $balance_fee5 ) ); |
|
65
|
|
|
|
|
66
|
|
|
$this->assertInternalType( 'string', WC_Stripe_Helper::format_balance_fee( $balance_fee5 ) ); |
|
67
|
|
|
} |
|
68
|
|
|
|
|
69
|
|
|
/** |
|
70
|
|
|
* Stripe requires statement_descriptor to be no longer than 22 characters. |
|
71
|
|
|
* In addition, it cannot contain <>"' special characters. |
|
72
|
|
|
*/ |
|
73
|
|
|
public function test_statement_descriptor_sanitation() { |
|
74
|
|
|
$statement_descriptor1 = array( |
|
75
|
|
|
'actual' => 'Test\'s Store', |
|
76
|
|
|
'expected' => 'Tests Store', |
|
77
|
|
|
); |
|
78
|
|
|
|
|
79
|
|
|
$this->assertEquals( $statement_descriptor1['expected'], WC_Stripe_Helper::clean_statement_descriptor( $statement_descriptor1['actual'] ) ); |
|
80
|
|
|
|
|
81
|
|
|
$statement_descriptor2 = array( |
|
82
|
|
|
'actual' => 'Test\'s Store > Driving Course Range', |
|
83
|
|
|
'expected' => 'Tests Store Driving C', |
|
84
|
|
|
); |
|
85
|
|
|
|
|
86
|
|
|
$this->assertEquals( $statement_descriptor2['expected'], WC_Stripe_Helper::clean_statement_descriptor( $statement_descriptor2['actual'] ) ); |
|
87
|
|
|
|
|
88
|
|
|
$statement_descriptor3 = array( |
|
89
|
|
|
'actual' => 'Test\'s Store < Driving Course Range', |
|
90
|
|
|
'expected' => 'Tests Store Driving C', |
|
91
|
|
|
); |
|
92
|
|
|
|
|
93
|
|
|
$this->assertEquals( $statement_descriptor3['expected'], WC_Stripe_Helper::clean_statement_descriptor( $statement_descriptor3['actual'] ) ); |
|
94
|
|
|
|
|
95
|
|
|
$statement_descriptor4 = array( |
|
96
|
|
|
'actual' => 'Test\'s Store " Driving Course Range', |
|
97
|
|
|
'expected' => 'Tests Store Driving C', |
|
98
|
|
|
); |
|
99
|
|
|
|
|
100
|
|
|
$this->assertEquals( $statement_descriptor4['expected'], WC_Stripe_Helper::clean_statement_descriptor( $statement_descriptor4['actual'] ) ); |
|
101
|
|
|
} |
|
102
|
|
|
|
|
103
|
|
|
/** |
|
104
|
|
|
* Test if credit card is of type 3DS. |
|
105
|
|
|
*/ |
|
106
|
|
|
public function test_is_3ds_card() { |
|
107
|
|
|
$stripe = new WC_Gateway_Stripe(); |
|
108
|
|
|
|
|
109
|
|
|
$source = new stdClass(); |
|
110
|
|
|
$source->type = 'three_d_secure'; |
|
111
|
|
|
|
|
112
|
|
|
$this->assertEquals( true, $stripe->is_3ds_card( $source ) ); |
|
113
|
|
|
|
|
114
|
|
|
$source = new stdClass(); |
|
115
|
|
|
$source->type = 'card'; |
|
116
|
|
|
|
|
117
|
|
|
$this->assertEquals( false, $stripe->is_3ds_card( $source ) ); |
|
118
|
|
|
} |
|
119
|
|
|
|
|
120
|
|
|
/** |
|
121
|
|
|
* Test if 3DS is required. |
|
122
|
|
|
*/ |
|
123
|
|
|
public function test_is_3ds_required() { |
|
124
|
|
|
$stripe = new WC_Gateway_Stripe(); |
|
125
|
|
|
|
|
126
|
|
|
$source = new stdClass(); |
|
127
|
|
|
$source->type = 'card'; |
|
128
|
|
|
$source->card = new stdClass(); |
|
129
|
|
|
$source->card->three_d_secure = 'required'; |
|
130
|
|
|
|
|
131
|
|
|
$this->assertEquals( true, $stripe->is_3ds_required( $source ) ); |
|
132
|
|
|
|
|
133
|
|
|
$source = new stdClass(); |
|
134
|
|
|
$source->type = 'card'; |
|
135
|
|
|
$source->card = new stdClass(); |
|
136
|
|
|
$source->card->three_d_secure = 'optional'; |
|
137
|
|
|
|
|
138
|
|
|
$this->assertEquals( false, $stripe->is_3ds_required( $source ) ); |
|
139
|
|
|
|
|
140
|
|
|
$source = new stdClass(); |
|
141
|
|
|
$source->type = 'card'; |
|
142
|
|
|
$source->card = new stdClass(); |
|
143
|
|
|
$source->card->three_d_secure = 'not_supported'; |
|
144
|
|
|
|
|
145
|
|
|
$this->assertEquals( false, $stripe->is_3ds_required( $source ) ); |
|
146
|
|
|
} |
|
147
|
|
|
} |
|
148
|
|
|
|