send billing information when creating order // pcp-11

This commit is contained in:
David Remer 2020-04-16 10:29:25 +03:00
parent a2d77d4089
commit 7efe09f3ab
7 changed files with 50 additions and 10 deletions

View file

@ -79,9 +79,11 @@ class Payer
$payer = [
'name' => $this->name()->toArray(),
'email_address' => $this->emailAddress(),
'payer_id' => $this->payerId(),
'address' => $this->address()->toArray(),
];
if ($this->payerId()) {
$payer['payer_id'] = $this->payerId();
}
if ($this->phone()) {
$payer['phone'] = $this->phone()->toArray();

View file

@ -26,7 +26,7 @@ class PayerFactory
$data->name->surname
);
$phone = (isset($data->phone)) ? new PhoneWithType(
$data->phone->type,
$data->phone->phone_type,
new Phone(
$data->phone->phone_number->national_number
)
@ -40,7 +40,7 @@ class PayerFactory
return new Payer(
$payerName,
$data->email_address,
$data->payer_id,
(isset($data->payer_id)) ? $data->payer_id : '',
$address,
$birthDate,
$phone,

File diff suppressed because one or more lines are too long

File diff suppressed because one or more lines are too long

View file

@ -10,11 +10,32 @@ class CheckoutActionHandler {
configuration() {
const createOrder = (data, actions) => {
const payer = {
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, {
method: 'POST',
body: JSON.stringify({
nonce: this.config.ajax.create_order.nonce,
purchase_units:[]
payer
})
}).then(function (res) {
return res.json();

View file

@ -51,7 +51,8 @@ return [
$requestData = $container->get('button.request-data');
$repository = $container->get('api.repository.cart');
$apiClient = $container->get('api.endpoint.order');
return new CreateOrderEndpoint($requestData, $repository, $apiClient);
$payerFactory = $container->get('api.factory.payer');
return new CreateOrderEndpoint($requestData, $repository, $apiClient, $payerFactory);
},
'button.endpoint.approve-order' => function (ContainerInterface $container): ApproveOrderEndpoint {
$requestData = $container->get('button.request-data');

View file

@ -3,6 +3,7 @@ declare(strict_types=1);
namespace Inpsyde\PayPalCommerce\Button\Endpoint;
use Inpsyde\PayPalCommerce\ApiClient\Factory\PayerFactory;
use Inpsyde\PayPalCommerce\Button\Exception\RuntimeException;
use Inpsyde\PayPalCommerce\ApiClient\Repository\CartRepository;
use Inpsyde\PayPalCommerce\ApiClient\Endpoint\OrderEndpoint;
@ -15,15 +16,18 @@ class CreateOrderEndpoint implements EndpointInterface
private $requestData;
private $repository;
private $apiEndpoint;
private $payerFactory;
public function __construct(
RequestData $requestData,
CartRepository $repository,
OrderEndpoint $apiEndpoint
OrderEndpoint $apiEndpoint,
PayerFactory $payerFactory
) {
$this->requestData = $requestData;
$this->repository = $repository;
$this->apiEndpoint = $apiEndpoint;
$this->payerFactory = $payerFactory;
}
public static function nonce() : string
@ -34,10 +38,22 @@ class CreateOrderEndpoint implements EndpointInterface
public function handleRequest() : bool
{
try {
$this->requestData->readRequest($this->nonce());
$data = $this->requestData->readRequest($this->nonce());
$purchaseUnits = $this->repository->all();
$payer = null;
if ($data['payer']) {
if (isset($data['payer']['phone']['phone_number']['national_number'])) {
// make sure the phone number contains only numbers and is max 14. chars long.
$number = $data['payer']['phone']['phone_number']['national_number'];
$number = preg_replace("/[^0-9]/", "", $number);
$number = substr($number,0,14);
$data['payer']['phone']['phone_number']['national_number'] = $number;
}
$payer = $this->payerFactory->fromPayPalResponse(json_decode(json_encode($data['payer'])));
}
$order = $this->apiEndpoint->createForPurchaseUnits(
...$purchaseUnits
$purchaseUnits,
$payer
);
wp_send_json_success($order->toArray());
return true;