3
3
namespace Tighten \NovaStripe \Tests ;
4
4
5
5
use Illuminate \Foundation \Testing \WithFaker ;
6
- use Illuminate \Support \Facades \Config ;
7
- use Stripe \Charge ;
8
- use Stripe \StripeClient ;
6
+ use Tighten \NovaStripe \Clients \StripeClient ;
9
7
10
8
class StripeChargeControllerTest extends TestCase
11
9
{
@@ -17,11 +15,10 @@ class StripeChargeControllerTest extends TestCase
17
15
public function setUp (): void
18
16
{
19
17
parent ::setUp ();
20
- $ this ->stripe = new StripeClient (Config:: get ( ' services.stripe.secret ' )) ;
18
+ $ this ->stripe = new StripeClient ;
21
19
22
- $ this ->charge = $ this ->stripe ->charges ->all ()->count ()
23
- ? $ this ->stripe ->charges ->all (['limit ' => 1 ])->first ()
24
- : $ this ->stripe ->charges ->create ([
20
+ $ this ->charge = $ this ->findSuccessfulNonRefundedCharge ()
21
+ ?: $ this ->stripe ->createCharge ([
25
22
'amount ' => $ this ->faker ->numberBetween (50 , 1000 ),
26
23
'currency ' => 'usd ' ,
27
24
'source ' => 'tok_mastercard ' ,
@@ -54,7 +51,7 @@ public function it_returns_a_list_of_charges()
54
51
public function it_returns_charge_details ()
55
52
{
56
53
$ response = $ this ->get ('nova-vendor/nova-stripe/stripe/charges/ ' . $ this ->charge ->id );
57
- $ stripeCharge = Charge:: retrieve ([ ' id ' => $ this ->charge ->id , ' expand ' => [ ' balance_transaction ' ]], [ ' api_key ' => Config:: get ( ' services.stripe.secret ' )] );
54
+ $ stripeCharge = $ this -> stripe -> getCharge ( $ this ->charge ->id );
58
55
59
56
$ response ->assertJsonFragment ([
60
57
'id ' => $ stripeCharge ->id ,
@@ -86,12 +83,38 @@ public function it_returns_charge_details()
86
83
/** @test */
87
84
public function it_shows_the_current_balance ()
88
85
{
89
- $ balance = $ this ->stripe ->balance -> retrieve ();
86
+ $ balance = $ this ->stripe ->getBalance ();
90
87
91
88
$ this ->get ('nova-vendor/nova-stripe/stripe/balance ' )
92
89
->assertJsonFragment ([
93
90
'available ' => $ balance ->available ,
94
91
'pending ' => $ balance ->pending ,
95
92
]);
96
93
}
94
+
95
+ /** @test */
96
+ public function it_can_refund_a_charge_successfully ()
97
+ {
98
+ $ this ->post ('nova-vendor/nova-stripe/stripe/charges/ ' . $ this ->charge ->id . '/refund ' )
99
+ ->assertSuccessful ()
100
+ ->assertJsonFragment ([
101
+ 'charge ' => $ this ->charge ->id ,
102
+ 'status ' => 'succeeded ' ,
103
+ ]);
104
+
105
+ $ this ->assertTrue ($ this ->stripe ->getCharge ($ this ->charge ->id )->refunded );
106
+ }
107
+
108
+ public function findSuccessfulNonRefundedCharge ()
109
+ {
110
+ $ charges = $ this ->stripe ->listCharges ();
111
+
112
+ foreach ($ charges as $ charge ) {
113
+ if (! $ charge ->refunded && $ charge ->status === 'succeeded ' ) {
114
+ return $ charge ;
115
+ }
116
+ }
117
+
118
+ return null ;
119
+ }
97
120
}
0 commit comments