diff --git a/.env b/.env index d0b018d..e6e72e0 100644 --- a/.env +++ b/.env @@ -5,6 +5,7 @@ PP_ACCOUNT=dslaky-seller@gmail.com PP_CLIENTID=AfnGR9VCjtBF_M0TemwikSG7q0sIm0mE4maIWw9vhiT1-X7vd9ONTvf-D3mEw1AaG9t2CWjoNbHIltI8 PP_SECRET=ENR4dvs4Y4836E-rDGUUMRIY6QEsCzOLsy16yoIBUrDpyt-Jfqi9PwNzBIDeKUpTuTbZUxQBvIe44jC0 +PP_CANCEL=http://iolovolio.local/checkout PP_RETURN=http://iolovolio.local/thankyou PP_AUTH_URL=https://api-m.sandbox.paypal.com/v1/oauth2/token PP_ORDER_URL=https://api-m.sandbox.paypal.com/v2/checkout/orders diff --git a/api/user_add.php b/api/user_add.php new file mode 100644 index 0000000..eaa998a --- /dev/null +++ b/api/user_add.php @@ -0,0 +1,42 @@ +getConnection(); + +$data = json_decode(file_get_contents("php://input")); + +$query = "INSERT INTO `users` + (`id`, `first_name`, `last_name`, `email`, `phone`, `address`, `city`, `zip_code`, `province`, `password`) + VALUES (NULL, '".trim($data->first_name)."', '".trim($data->last_name)."', '".trim($data->email)."', + '".trim($data->phone)."', '".trim($data->address)."', '".trim($data->city)."', + '".trim($data->zip_code)."', '".trim($data->province)."', '".md5(trim($data->password))."')"; + +$stmt = $conn->prepare($query); + +if($stmt->execute()) { + http_response_code(200); + echo json_encode( + array( + "status" => 200, + "profile" => $data + )); +} else { + http_response_code(400); + echo json_encode( + array( + "status" => 400, + "message" => "Error inserting new user" + )); +} + +?> + diff --git a/components/checkout/checkout.html b/components/checkout/checkout.html index ef27bf1..67b74ff 100644 --- a/components/checkout/checkout.html +++ b/components/checkout/checkout.html @@ -14,7 +14,7 @@
Dettagli fatturazione - Sei già nostro cliente? Accedi + Sei già nostro cliente?
@@ -179,5 +179,3 @@
- diff --git a/components/checkout/checkout.js b/components/checkout/checkout.js index 23c4c81..60875c1 100644 --- a/components/checkout/checkout.js +++ b/components/checkout/checkout.js @@ -8,12 +8,18 @@ $(document).ready( () => { const place = component.find('.place-order') const extraAddress = component.find('.different-address') const extraAddressRadio = component.find('input[name="other_address"]') + const toggleLogin = $('.toggle-login') let hasExtraAddress = false + let isNewUser = true place.off('.click').on('click.click', () => { checkForm() }) + toggleLogin.off('.click').on('click.click', () => { + isNewUser = !isNewUser + }) + extraAddressRadio.off('.click').on('click.click', (e) => { const checked = $(e.currentTarget).val() hasExtraAddress = checked == 'yes' @@ -102,23 +108,13 @@ $(document).ready( () => { body[name] = input.val() } }) - // placeOrder() - } - const profile = { - first_name: 'Carmine', - last_name: 'De Rosa', - address: 'C.so Sempione, 76', - city: 'Milano', - province: 'MI', - zip_code: '20154' - } - - placeOrder(profile) + placeOrder(body, isNewUser) + } } - const placeOrder = (profile) => { + const placeOrder = (profile, isNewUser) => { Apis.getCart().then( (data) => { const cartItems = data.cart @@ -147,7 +143,16 @@ $(document).ready( () => { Apis.placeOrder(cart, token).then( (data) => { const capture = data.links.find(item => item.rel == 'capture') const approve = data.links.find(item => item.rel == 'approve') - window.location = approve.href + if(isNewUser) { + Apis.addUser(profile).then( (data) => { + console.log(data) + window.location = approve.href + }).catch( (error) => { + console.error(error) + }) + } else { + window.location = approve.href + } }).catch( (error) => { console.error(error) }) diff --git a/components/checkout/checkout.scss b/components/checkout/checkout.scss index 2d5030b..68f6175 100644 --- a/components/checkout/checkout.scss +++ b/components/checkout/checkout.scss @@ -18,6 +18,11 @@ height: 40px; } + .toggle-login { + color: $brown; + cursor: pointer; + } + .label { display: block; @include font-style($font-sans, 700, $font-16); diff --git a/src/js/index.js b/src/js/index.js index 4a903da..974847b 100644 --- a/src/js/index.js +++ b/src/js/index.js @@ -57,6 +57,26 @@ window.Apis.getCart = (body) => { }) } +window.Apis.addUser = (body) => { + + return new Promise((resolve, reject) => { + $.ajax({ + type: 'POST', + url: apiUrl + `/user_add.php?buster=${new Date().getTime()}`, + data: JSON.stringify(body), + dataType: 'json', + async: true, + contentType: 'application/json; charset=utf-8', + success: (data) => { + resolve(data) + }, + error: (error) => { + reject(error) + } + }) + }) +} + window.Apis.getToken = () => { return new Promise((resolve, reject) => { $.ajax({ @@ -84,7 +104,7 @@ window.Apis.placeOrder = (cart, token) => { brand_name: 'IoLovOlio', locale: 'it-IT', return_url: ENV.PP_RETURN, - cancel_url: ENV.PP_RETURN + cancel_url: ENV.PP_CANCEL }, purchase_units: cart } @@ -107,4 +127,3 @@ window.Apis.placeOrder = (cart, token) => { }) }) } -