3
3
namespace Osiset \ShopifyApp \Test \Traits ;
4
4
5
5
use Illuminate \Auth \AuthManager ;
6
+ use Osiset \ShopifyApp \Exceptions \MissingShopDomainException ;
6
7
use Osiset \ShopifyApp \Storage \Models \Charge ;
7
8
use Osiset \ShopifyApp \Storage \Models \Plan ;
8
9
use Osiset \ShopifyApp \Test \Stubs \Api as ApiStub ;
@@ -112,7 +113,7 @@ public function testUsageChargeSuccess(): void
112
113
$ response = $ this ->call (
113
114
'post ' ,
114
115
'/billing/usage-charge ' ,
115
- array_merge ($ data , ['signature ' => $ signature ->toNative ()])
116
+ array_merge ($ data , ['signature ' => $ signature ->toNative (), ' shop ' => $ shop -> name ])
116
117
);
117
118
$ response ->assertRedirect ($ data ['redirect ' ]);
118
119
$ response ->assertSessionHas ('success ' );
@@ -125,13 +126,13 @@ public function testUsageChargeSuccess(): void
125
126
$ response = $ this ->call (
126
127
'post ' ,
127
128
'/billing/usage-charge ' ,
128
- array_merge ($ data , ['signature ' => $ signature ->toNative ()])
129
+ array_merge ($ data , ['signature ' => $ signature ->toNative (), ' shop ' => $ shop -> name ])
129
130
);
130
131
$ response ->assertRedirect ('http://localhost ' );
131
132
$ response ->assertSessionHas ('success ' );
132
133
}
133
134
134
- public function testReturnToSettingScreenNoPlan ()
135
+ public function testReturnToSettingScreenNoPlan (): void
135
136
{
136
137
// Set up a shop
137
138
$ shop = factory ($ this ->model )->create ([
@@ -149,4 +150,82 @@ public function testReturnToSettingScreenNoPlan()
149
150
//Confirm we get sent back to the homepage of the app
150
151
$ response ->assertRedirect ('https://example-app.com?shop= ' .$ shop ->name );
151
152
}
153
+
154
+ public function testUsageChargeSuccessWithShopParam (): void
155
+ {
156
+ // Stub the responses
157
+ ApiStub::stubResponses ([
158
+ 'post_recurring_application_charges_usage_charges_alt ' ,
159
+ ]);
160
+
161
+ // Create the shop
162
+ $ plan = factory (Util::getShopifyConfig ('models.plan ' , Plan::class))->states ('type_recurring ' )->create ();
163
+ $ shop = factory ($ this ->model )->create ([
164
+ 'plan_id ' => $ plan ->getId ()->toNative (),
165
+ ]);
166
+ factory (Util::getShopifyConfig ('models.charge ' , Charge::class))->states ('type_recurring ' )->create ([
167
+ 'plan_id ' => $ plan ->getId ()->toNative (),
168
+ 'user_id ' => $ shop ->getId ()->toNative (),
169
+ ]);
170
+
171
+ // Login the shop
172
+ $ this ->auth ->login ($ shop );
173
+
174
+ // Set up the data for the usage charge and the signature for it
175
+ $ secret = $ this ->app ['config ' ]->get ('shopify-app.api_secret ' );
176
+ $ data = ['description ' => 'One email ' , 'price ' => 1.00 , 'redirect ' => 'https://localhost/usage-success ' ];
177
+ $ signature = Util::createHmac (['data ' => $ data , 'buildQuery ' => true ], $ secret );
178
+
179
+ // Run the call
180
+ $ response = $ this ->call (
181
+ 'post ' ,
182
+ '/billing/usage-charge ' ,
183
+ array_merge ($ data , ['signature ' => $ signature ->toNative (), 'shop ' => $ shop ->name ])
184
+ );
185
+ $ response ->assertRedirect ($ data ['redirect ' ]);
186
+ $ response ->assertSessionHas ('success ' );
187
+ $ this ->assertDatabaseHas ('charges ' , ['description ' => 'One email ' ]);
188
+ }
189
+
190
+ public function testUsageChargeFailWithoutShopParam (): void
191
+ {
192
+ $ this ->withoutExceptionHandling ();
193
+ $ this ->expectException (MissingShopDomainException::class);
194
+
195
+ // Stub the responses
196
+ ApiStub::stubResponses ([
197
+ 'post_recurring_application_charges_usage_charges_alt ' ,
198
+ ]);
199
+
200
+ // Create the shop
201
+ $ plan = factory (Util::getShopifyConfig ('models.plan ' , Plan::class))
202
+ ->states ('type_recurring ' )->create ();
203
+ $ shop = factory ($ this ->model )->create ([
204
+ 'plan_id ' => $ plan ->getId ()->toNative (),
205
+ ]);
206
+ factory (Util::getShopifyConfig ('models.charge ' , Charge::class))
207
+ ->states ('type_recurring ' )->create ([
208
+ 'plan_id ' => $ plan ->getId ()->toNative (),
209
+ 'user_id ' => $ shop ->getId ()->toNative (),
210
+ ]);
211
+
212
+ // Login the shop
213
+ $ this ->auth ->login ($ shop );
214
+
215
+ // Set up the data for the usage charge and the signature for it
216
+ $ secret = $ this ->app ['config ' ]->get ('shopify-app.api_secret ' );
217
+ $ data = [
218
+ 'description ' => 'One email ' ,
219
+ 'price ' => 1.00 ,
220
+ 'redirect ' => 'https://localhost/usage-success ' ,];
221
+ $ signature = Util::createHmac (['data ' => $ data , 'buildQuery ' => true ], $ secret );
222
+
223
+ // Run the call
224
+ $ response = $ this ->call (
225
+ 'post ' ,
226
+ '/billing/usage-charge ' ,
227
+ array_merge ($ data , ['signature ' => $ signature ->toNative ()])
228
+ );
229
+ $ this ->assertDatabaseMissing ('charges ' , ['description ' => 'One email ' ]);
230
+ }
152
231
}
0 commit comments