Skip to content

Commit 6c53788

Browse files
committed
jquery.org: Create user when signing up for membership.
1 parent c109375 commit 6c53788

File tree

2 files changed

+40
-8
lines changed

2 files changed

+40
-8
lines changed

mu-plugins/jquery.org/stripe.php

Lines changed: 29 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,6 @@ public static function charge() {
1515
require_once( 'lib/Stripe.php' );
1616
Stripe::setApiKey( STRIPE_SECRET );
1717

18-
header( 'Content-Type: application/json' );
19-
2018
// Verify required data
2119
if ( ! isset( $_REQUEST['nonce'] ) || ! wp_verify_nonce( $_REQUEST['nonce'], 'stripe-nonce' ) ) {
2220
self::fail( 'Invalid Nonce' );
@@ -32,6 +30,8 @@ public static function charge() {
3230

3331
$token = $_REQUEST['token'];
3432
$email = $_REQUEST['email'];
33+
34+
// Create Stripe subscription
3535
$customer = Stripe_Customer::create( array(
3636
'email' => $email,
3737
'card' => $token
@@ -40,7 +40,33 @@ public static function charge() {
4040
'plan' => $_REQUEST['planId']
4141
) );
4242

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;
4470
exit();
4571
}
4672

themes/jquery/js/main.js

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,7 @@ $(function() {
7777
* Join page
7878
*/
7979
(function() {
80-
// Enlarged goodies
80+
// Enlarged gifts
8181
$(".enlarge").colorbox();
8282

8383
// Gift forms
@@ -133,12 +133,18 @@ $(function() {
133133
description: button.data("description"),
134134
panelLabel: button.data("panel-label"),
135135
amount: button.data("amount"),
136-
token: function( data ) {
137-
processMembership({
138-
token: data.id,
136+
token: function( stripeData ) {
137+
var data = {
138+
token: stripeData.id,
139+
planId: button.data( "plan-id" ),
140+
name: name,
139141
email: email,
140-
planId: button.data( "plan-id" )
142+
address: address
143+
};
144+
gifts.each(function() {
145+
data[ this.name ] = this.value;
141146
});
147+
processMembership( data );
142148
}
143149
});
144150
});

0 commit comments

Comments
 (0)