PCP-13 // add payer information to mini-cart, cart and single product

This commit is contained in:
David Remer 2020-04-28 09:14:05 +03:00
parent 05aac334e7
commit a9e0cc0cf7
9 changed files with 77 additions and 29 deletions

File diff suppressed because one or more lines are too long

File diff suppressed because one or more lines are too long

View file

@ -1545,12 +1545,14 @@
"babel-plugin-syntax-object-rest-spread": { "babel-plugin-syntax-object-rest-spread": {
"version": "6.13.0", "version": "6.13.0",
"resolved": "https://registry.npmjs.org/babel-plugin-syntax-object-rest-spread/-/babel-plugin-syntax-object-rest-spread-6.13.0.tgz", "resolved": "https://registry.npmjs.org/babel-plugin-syntax-object-rest-spread/-/babel-plugin-syntax-object-rest-spread-6.13.0.tgz",
"integrity": "sha1-/WU28rzhODb/o6VFjEkDpZe7O/U=" "integrity": "sha1-/WU28rzhODb/o6VFjEkDpZe7O/U=",
"dev": true
}, },
"babel-plugin-transform-object-rest-spread": { "babel-plugin-transform-object-rest-spread": {
"version": "6.26.0", "version": "6.26.0",
"resolved": "https://registry.npmjs.org/babel-plugin-transform-object-rest-spread/-/babel-plugin-transform-object-rest-spread-6.26.0.tgz", "resolved": "https://registry.npmjs.org/babel-plugin-transform-object-rest-spread/-/babel-plugin-transform-object-rest-spread-6.26.0.tgz",
"integrity": "sha1-DzZpLVD+9rfi1LOsFHgTepY7ewY=", "integrity": "sha1-DzZpLVD+9rfi1LOsFHgTepY7ewY=",
"dev": true,
"requires": { "requires": {
"babel-plugin-syntax-object-rest-spread": "^6.8.0", "babel-plugin-syntax-object-rest-spread": "^6.8.0",
"babel-runtime": "^6.26.0" "babel-runtime": "^6.26.0"
@ -1560,6 +1562,7 @@
"version": "6.26.0", "version": "6.26.0",
"resolved": "https://registry.npmjs.org/babel-runtime/-/babel-runtime-6.26.0.tgz", "resolved": "https://registry.npmjs.org/babel-runtime/-/babel-runtime-6.26.0.tgz",
"integrity": "sha1-llxwWGaOgrVde/4E/yM3vItWR/4=", "integrity": "sha1-llxwWGaOgrVde/4E/yM3vItWR/4=",
"dev": true,
"requires": { "requires": {
"core-js": "^2.4.0", "core-js": "^2.4.0",
"regenerator-runtime": "^0.11.0" "regenerator-runtime": "^0.11.0"
@ -2149,7 +2152,8 @@
"core-js": { "core-js": {
"version": "2.6.11", "version": "2.6.11",
"resolved": "https://registry.npmjs.org/core-js/-/core-js-2.6.11.tgz", "resolved": "https://registry.npmjs.org/core-js/-/core-js-2.6.11.tgz",
"integrity": "sha512-5wjnpaT/3dV+XB4borEsnAYQchn00XSgTAWKDkEqv+K8KevjbzmofK6hfJ9TZIlpj2N0xQpazy7PiRQiWHqzWg==" "integrity": "sha512-5wjnpaT/3dV+XB4borEsnAYQchn00XSgTAWKDkEqv+K8KevjbzmofK6hfJ9TZIlpj2N0xQpazy7PiRQiWHqzWg==",
"dev": true
}, },
"core-js-compat": { "core-js-compat": {
"version": "3.6.4", "version": "3.6.4",
@ -5313,7 +5317,8 @@
"regenerator-runtime": { "regenerator-runtime": {
"version": "0.11.1", "version": "0.11.1",
"resolved": "https://registry.npmjs.org/regenerator-runtime/-/regenerator-runtime-0.11.1.tgz", "resolved": "https://registry.npmjs.org/regenerator-runtime/-/regenerator-runtime-0.11.1.tgz",
"integrity": "sha512-MguG95oij0fC3QV3URf4V2SDYGJhJnJGqvIIgdECeODCT98wSWDAJ94SSuVpYQUoTcGUIL6L4yNB7j1DFFHSBg==" "integrity": "sha512-MguG95oij0fC3QV3URf4V2SDYGJhJnJGqvIIgdECeODCT98wSWDAJ94SSuVpYQUoTcGUIL6L4yNB7j1DFFHSBg==",
"dev": true
}, },
"regex-not": { "regex-not": {
"version": "1.0.2", "version": "1.0.2",

View file

@ -1,4 +1,5 @@
import onApprove from './onApproveForContinue.js'; import onApprove from './onApproveForContinue.js';
import {payerData} from "./Payer";
class CartActionHandler { class CartActionHandler {
@ -9,11 +10,13 @@ class CartActionHandler {
configuration() { configuration() {
const createOrder = (data, actions) => { const createOrder = (data, actions) => {
const payer = payerData();
return fetch(this.config.ajax.create_order.endpoint, { return fetch(this.config.ajax.create_order.endpoint, {
method: 'POST', method: 'POST',
body: JSON.stringify({ body: JSON.stringify({
nonce: this.config.ajax.create_order.nonce, nonce: this.config.ajax.create_order.nonce,
purchase_units: [], purchase_units: [],
payer
}), }),
}).then(function(res) { }).then(function(res) {
return res.json(); return res.json();

View file

@ -1,4 +1,5 @@
import onApprove from './onApproveForPayNow.js'; import onApprove from './onApproveForPayNow.js';
import {payerData} from "./Payer";
class CheckoutActionHandler { class CheckoutActionHandler {
@ -10,27 +11,7 @@ class CheckoutActionHandler {
configuration() { configuration() {
const createOrder = (data, actions) => { const createOrder = (data, actions) => {
const payer = { const payer = payerData();
email_address:(document.querySelector('#billing_email')) ? document.querySelector('#billing_email').value : "",
name : {
surname: (document.querySelector('#billing_last_name')) ? document.querySelector('#billing_last_name').value : "",
given_name: (document.querySelector('#billing_first_name')) ? document.querySelector('#billing_first_name').value : ""
},
address : {
country_code : (document.querySelector('#billing_country')) ? document.querySelector('#billing_country').value : "",
address_line_1 : (document.querySelector('#billing_address_1')) ? document.querySelector('#billing_address_1').value : "",
address_line_2 : (document.querySelector('#billing_address_2')) ? document.querySelector('#billing_address_2').value : "",
admin_area_1 : (document.querySelector('#billing_city')) ? document.querySelector('#billing_city').value : "",
admin_area_2 : (document.querySelector('#billing_state')) ? document.querySelector('#billing_state').value : "",
postal_code : (document.querySelector('#billing_postcode')) ? document.querySelector('#billing_postcode').value : ""
},
phone : {
phone_type:"HOME",
phone_number:{
national_number : (document.querySelector('#billing_phone')) ? document.querySelector('#billing_phone').value : ""
}
}
};
return fetch(this.config.ajax.create_order.endpoint, { return fetch(this.config.ajax.create_order.endpoint, {
method: 'POST', method: 'POST',
body: JSON.stringify({ body: JSON.stringify({

View file

@ -0,0 +1,27 @@
export const payerData = () => {
const payer = PayPalCommerceGateway.payer;
if (! payer) {
return null;
}
return {
email_address:(document.querySelector('#billing_email')) ? document.querySelector('#billing_email').value : payer.email_address,
name : {
surname: (document.querySelector('#billing_last_name')) ? document.querySelector('#billing_last_name').value : payer.name.surname,
given_name: (document.querySelector('#billing_first_name')) ? document.querySelector('#billing_first_name').value : payer.name.given_name
},
address : {
country_code : (document.querySelector('#billing_country')) ? document.querySelector('#billing_country').value : payer.address.country_code,
address_line_1 : (document.querySelector('#billing_address_1')) ? document.querySelector('#billing_address_1').value : payer.address.address_line_1,
address_line_2 : (document.querySelector('#billing_address_2')) ? document.querySelector('#billing_address_2').value : payer.address.address_line_2,
admin_area_1 : (document.querySelector('#billing_city')) ? document.querySelector('#billing_city').value : payer.address.admin_area_1,
admin_area_2 : (document.querySelector('#billing_state')) ? document.querySelector('#billing_state').value : payer.address.admin_area_2,
postal_code : (document.querySelector('#billing_postcode')) ? document.querySelector('#billing_postcode').value : payer.address.postal_code
},
phone : {
phone_type:"HOME",
phone_number:{
national_number : (document.querySelector('#billing_phone')) ? document.querySelector('#billing_phone').value : payer.phone.phone_number.national_number
}
}
};
}

View file

@ -1,6 +1,7 @@
import ButtonsToggleListener from './ButtonsToggleListener'; import ButtonsToggleListener from './ButtonsToggleListener';
import Product from './Product'; import Product from './Product';
import onApprove from './onApproveForContinue'; import onApprove from './onApproveForContinue';
import {payerData} from "./Payer";
class SingleProductActionHandler { class SingleProductActionHandler {
@ -73,11 +74,13 @@ class SingleProductActionHandler {
this.errorHandler.clear(); this.errorHandler.clear();
const onResolve = (purchase_units) => { const onResolve = (purchase_units) => {
const payer = payerData();
return fetch(this.config.ajax.create_order.endpoint, { return fetch(this.config.ajax.create_order.endpoint, {
method: 'POST', method: 'POST',
body: JSON.stringify({ body: JSON.stringify({
nonce: this.config.ajax.create_order.nonce, nonce: this.config.ajax.create_order.nonce,
purchase_units purchase_units,
payer
}) })
}).then(function (res) { }).then(function (res) {
return res.json(); return res.json();

View file

@ -100,6 +100,7 @@ class SmartButton implements SmartButtonInterface
'nonce' => wp_create_nonce(ApproveOrderEndpoint::nonce()), 'nonce' => wp_create_nonce(ApproveOrderEndpoint::nonce()),
], ],
], ],
'payer' => $this->payerData(),
'button' => [ 'button' => [
'wrapper' => '#ppc-button', 'wrapper' => '#ppc-button',
'mini_cart_wrapper' => '#ppc-button-minicart', 'mini_cart_wrapper' => '#ppc-button-minicart',
@ -116,6 +117,34 @@ class SmartButton implements SmartButtonInterface
return $localize; return $localize;
} }
private function payerData() : ?array {
$customer = WC()->customer;
if (! is_user_logged_in() || ! is_a($customer, \WC_Customer::class)) {
return null;
}
return [
'email_address' => $customer->get_billing_email(),
'name' => [
'surname' => $customer->get_billing_last_name(),
'given_name' => $customer->get_billing_last_name(),
],
'address' => [
'country_code' => $customer->get_billing_country(),
'address_line_1' => $customer->get_billing_address_1(),
'address_line_2' => $customer->get_billing_address_2(),
'admin_area_1' => $customer->get_billing_city(),
'admin_area_2' => $customer->get_billing_state(),
'postal_code' => $customer->get_billing_postcode(),
],
'phone' => [
'phone_type' => 'HOME',
'phone_number' => [
'national_number' => $customer->get_billing_phone(),
],
],
];
}
private function url() : string private function url() : string
{ {
$params = [ $params = [
@ -130,7 +159,7 @@ class SmartButton implements SmartButtonInterface
//ToDo: Probably only needed, when DCC //ToDo: Probably only needed, when DCC
'vault' => 'false', 'vault' => 'false',
'commit' => is_checkout() ? 'true' : 'false', 'commit' => is_checkout() ? 'true' : 'false',
'intent' => $this->settings->get('intent') 'intent' => $this->settings->get('intent'),
]; ];
$payee = $this->payeeRepository->payee(); $payee = $this->payeeRepository->payee();
if ($payee->merchantId()) { if ($payee->merchantId()) {

View file

@ -41,7 +41,7 @@ class CreateOrderEndpoint implements EndpointInterface
$data = $this->requestData->readRequest($this->nonce()); $data = $this->requestData->readRequest($this->nonce());
$purchaseUnits = $this->repository->all(); $purchaseUnits = $this->repository->all();
$payer = null; $payer = null;
if (isset($data['payer'])) { if (isset($data['payer']) && $data['payer']) {
if (isset($data['payer']['phone']['phone_number']['national_number'])) { if (isset($data['payer']['phone']['phone_number']['national_number'])) {
// make sure the phone number contains only numbers and is max 14. chars long. // make sure the phone number contains only numbers and is max 14. chars long.
$number = $data['payer']['phone']['phone_number']['national_number']; $number = $data['payer']['phone']['phone_number']['national_number'];