@@ -15,8 +15,6 @@ public static function charge() {
15
15
require_once ( 'lib/Stripe.php ' );
16
16
Stripe::setApiKey ( STRIPE_SECRET );
17
17
18
- header ( 'Content-Type: application/json ' );
19
-
20
18
// Verify required data
21
19
if ( ! isset ( $ _REQUEST ['nonce ' ] ) || ! wp_verify_nonce ( $ _REQUEST ['nonce ' ], 'stripe-nonce ' ) ) {
22
20
self ::fail ( 'Invalid Nonce ' );
@@ -32,6 +30,8 @@ public static function charge() {
32
30
33
31
$ token = $ _REQUEST ['token ' ];
34
32
$ email = $ _REQUEST ['email ' ];
33
+
34
+ // Create Stripe subscription
35
35
$ customer = Stripe_Customer::create ( array (
36
36
'email ' => $ email ,
37
37
'card ' => $ token
@@ -40,7 +40,33 @@ public static function charge() {
40
40
'plan ' => $ _REQUEST ['planId ' ]
41
41
) );
42
42
43
- // TODO: create user
43
+ // Create or update WordPress user
44
+ $ user = get_user_by ( 'email ' , $ email );
45
+ if ( $ user ) {
46
+ $ user_id = $ user ->ID ;
47
+ } else {
48
+ $ user_id = wp_insert_user (array (
49
+ 'user_pass ' => wp_generate_password (),
50
+ 'user_login ' => $ email ,
51
+ 'user_email ' => $ email ,
52
+ 'display_name ' => $ _REQUEST [ 'name ' ]
53
+ ) );
54
+ }
55
+
56
+ // Store Stripe ID and gift choices on WordPress user
57
+ add_user_meta ( $ user_id , 'stripe_id ' , $ customer ->id );
58
+ add_user_meta ( $ user_id , 'address ' , $ _REQUEST ['address ' ] );
59
+ if ( !empty ( $ _REQUEST ['tshirt ' ] ) ) {
60
+ add_user_meta ( $ user_id , 'tshirt ' , $ _REQUEST ['tshirt ' ] );
61
+ }
62
+ if ( !empty ( $ _REQUEST ['hoodie ' ] ) ) {
63
+ add_user_meta ( $ user_id , 'hoodie ' , $ _REQUEST ['hoodie ' ] );
64
+ }
65
+ if ( !empty ( $ _REQUEST ['bag ' ] ) ) {
66
+ add_user_meta ( $ user_id , 'bag ' , $ _REQUEST ['bag ' ] );
67
+ }
68
+
69
+ echo $ user_id ;
44
70
exit ();
45
71
}
46
72
0 commit comments