クラス
AmazonMiddleware
ソース ソース
ファイル: src/Routing/AmazonMiddleware.php
class AmazonMiddleware
{
/**
* Amazon Pay module object
*
* @var AmazonPay
*/
private $module;
/**
* Instantiates middleware
*
* @author Evan D Shaw <evandanielshaw@gmail.com>
* @param AmazonPay $module
*/
public function __construct(AmazonPay $module) {
$this->module = $module;
}
/**
* Sets `$_POST['offer']['payment_name']` to Amazon Pay payment method name
*
* @author Evan D Shaw <evandanielshaw@gmail.com>
* @return Closure&#Function#cececce
*/
public function setPaymentMethodToAmazonPay() {
return function () {
$_POST['offer']['payment_name'] = $this->module->getPaymentNameByActingFlag();
};
}
/**
* Clears `$_SESSION['usces_entry']['delivery']`
*
* @author Evan D Shaw <evandanielshaw@gmail.com>
* @return Closure&#Function#86f215e4
*/
public function payOnlyClearDeliverySession() {
return function () {
unset($_SESSION['usces_entry']['delivery']);
};
}
/**
* Puts checkout session response in `$_SESSION`
*
* @author Evan D Shaw <evandanielshaw@gmail.com>
* @return Closure&#Function#7a371dcb
*/
public function putGetCheckoutSessionResponseInSession() {
return function () {
$sessionId = $_REQUEST['amazonCheckoutSessionId'];
$result = (new CheckoutSession\Get($this->module))->get($sessionId);
$response = json_decode($result['response'], true);
$_SESSION['amazonCheckoutSession'] = $response;
$_SESSION['amazonCheckoutSessionId'] = $sessionId;
};
}
/**
* Populate Welcart `$_POST['customer']` with Amazon checkout session data
*
* @author Evan D Shaw <evandanielshaw@gmail.com>
* @param bool $payandship
* @return Closure&#Function#28b01377
*/
public function setCustomerDataWithCheckoutSession($payandship) {
return function () use ($payandship) {
if (!usces_is_login()) {
$response = $_SESSION['amazonCheckoutSession'];
$fullname = explode(' ', $response['buyer']['name']);
$name1 = $fullname[0];
$name2 = isset($fullname[1]) ? join(' ', array_slice($fullname, 1)) : '';
$entries = isset($_SESSION['usces_entry']) ? $_SESSION['usces_entry'] : [];
$_POST['customer'] = isset($entries['customer']) ? $entries['customer'] : [];
$customer = [];
$customer['name1'] = $name1;
$customer['name2'] = $name2;
$customer['mailaddress1'] = $response['buyer']['email'];
$customer['mailaddress2'] = $response['buyer']['email'];
$opts = $this->module->getActingOpts();
if ($opts['quickpay_default_customer_address_details'] === 'on' && $payandship === true) {
$delivery = $this->getDeliveryAddressFromCheckoutSession($response);
$customer['country'] = $delivery['country'];
$customer['fax'] = $delivery['fax'];
$customer['pref'] = $delivery['pref'];
$customer['tel'] = $delivery['tel'];
$customer['zipcode'] = $delivery['zipcode'];
$customer['address1'] = $delivery['address1'];
$customer['address2'] = $delivery['address2'];
$customer['address3'] = $delivery['address3'];
}
$_POST['customer'] = $customer;
}
};
}
/**
* Populate Welcart `$_POST['delivery']` with Amazon checkout session data
*
* @author Evan D Shaw <evandanielshaw@gmail.com>
* @param bool $clearExisting `true` if `$_SESSION['usces_entry']['delivery']` should be cleared, `false` otherwise.
* Default: `false`
* @return Closure&#Function#3fcb4e59
*/
public function setDeliveryDataWithCheckoutSession($clearExisting = false) {
return function () use ($clearExisting) {
if ($clearExisting === true) {
unset($_SESSION['usces_entry']['delivery']);
}
$response = $_SESSION['amazonCheckoutSession'];
$_POST['delivery'] = $this->getDeliveryAddressFromCheckoutSession($response);
};
}
/**
* Returns delivery address parsed from an Amazon Checkout Session
*
* @author Evan D Shaw <evandanielshaw@gmail.com>
* @param array $checkoutSession
* @return array
*/
public function getDeliveryAddressFromCheckoutSession($checkoutSession) {
$opts = $this->module->getActingOpts();
$sandbox = $opts['sandbox'];
$address = $checkoutSession['shippingAddress'];
$pref = $address['stateOrRegion'];
$deliveryFullname = explode(' ', $address['name']);
$deliveryName1 = $deliveryFullname[0];
$deliveryName2 = isset($deliveryFullname[1]) ? join(' ', array_slice($deliveryFullname, 1)) : '';
// set flag to 1 because address is pulled from Amazon account
$delivery = [];
$delivery['delivery_flag'] = 1;
$delivery['name1'] = $deliveryName1;
$delivery['name2'] = $deliveryName2;
$delivery['name3'] = '';
$delivery['name4'] = '';
$delivery['country'] = $address['countryCode'];
$delivery['fax'] = '';
$delivery['pref'] = $pref;
$delivery['tel'] = $address['phoneNumber'];
$delivery['zipcode'] = $address['postalCode'];
$delivery['address1'] = $sandbox ? $address['city'] . $address['addressLine1'] : $address['addressLine1'];
$delivery['address2'] = $address['addressLine2'];
$delivery['address3'] = $address['addressLine3'];
// make sure Japanese prefecture name is converted to Japanese if it is in English
if ($delivery['country'] === 'JP') {
if (array_key_exists($delivery['pref'], Constants::ETOJ_PREF_MAP) === true) {
$delivery['pref'] = Constants::ETOJ_PREF_MAP[$delivery['pref']];
}
} elseif ($delivery['country'] === 'US') {
$states = get_option('usces_states');
if (!in_array($delivery['pref'], $states, true)) {
if (array_key_exists($delivery['pref'], Constants::ISO_US_STATE_CODE_MAP) === true) {
$delivery['pref'] = Constants::ISO_US_STATE_CODE_MAP[$delivery['pref']];
}
}
}
return $delivery;
}
}
- __construct — Instantiates middleware
- getDeliveryAddressFromCheckoutSession — Returns delivery address parsed from an Amazon Checkout Session
- payOnlyClearDeliverySession — Clears `$_SESSION['usces_entry']['delivery']`
- putGetCheckoutSessionResponseInSession — Puts checkout session response in `$_SESSION`
- setCustomerDataWithCheckoutSession — Populate Welcart `$_POST['customer']` with Amazon checkout session data
- setDeliveryDataWithCheckoutSession — Populate Welcart `$_POST['delivery']` with Amazon checkout session data
- setPaymentMethodToAmazonPay — Sets `$_POST['offer']['payment_name']` to Amazon Pay payment method name