mirror of
https://github.com/woocommerce/woocommerce-paypal-payments.git
synced 2025-09-06 18:16:38 +08:00
load form in the back, redirect ok, auth fails
This commit is contained in:
parent
10613711b9
commit
a1f4ead149
2 changed files with 63 additions and 56 deletions
|
@ -72,7 +72,6 @@ import {loadPaypalScript} from '../../../ppcp-button/resources/js/modules/Helper
|
|||
loadPaypalScript(PayPalCommerceGateway, () => {
|
||||
bootstrapped = true;
|
||||
const applepay = paypal.Applepay();
|
||||
console.log(applepay)
|
||||
applepay.config()
|
||||
.then(applepayConfig => {
|
||||
const appleContainer = document.getElementById("applepay-container");
|
||||
|
@ -96,7 +95,6 @@ import {loadPaypalScript} from '../../../ppcp-button/resources/js/modules/Helper
|
|||
let applePaySession = () => {
|
||||
const session = new ApplePaySession(4, paymentRequest)
|
||||
session.begin()
|
||||
console.log(session)
|
||||
if (needShipping) {
|
||||
session.onshippingmethodselected = function (event) {
|
||||
jQuery.ajax({
|
||||
|
@ -115,9 +113,7 @@ import {loadPaypalScript} from '../../../ppcp-button/resources/js/modules/Helper
|
|||
},
|
||||
success: (applePayShippingMethodUpdate, textStatus, jqXHR) => {
|
||||
let response = applePayShippingMethodUpdate.data
|
||||
console.log('onshippingmethod', response)
|
||||
selectedShippingMethod = event.shippingMethod
|
||||
console.log(selectedShippingMethod)
|
||||
//order the response shipping methods, so that the selected shipping method is the first one
|
||||
let orderedShippingMethods = response.newShippingMethods.sort((a, b) => {
|
||||
if (a.label === selectedShippingMethod.label) {
|
||||
|
@ -156,7 +152,6 @@ import {loadPaypalScript} from '../../../ppcp-button/resources/js/modules/Helper
|
|||
success: (applePayShippingContactUpdate, textStatus, jqXHR) => {
|
||||
let response = applePayShippingContactUpdate.data
|
||||
updatedContactInfo = event.shippingContact
|
||||
console.log('onshippingcontact', response)
|
||||
if (applePayShippingContactUpdate.success === false) {
|
||||
response.errors = createAppleErrors(response.errors)
|
||||
}
|
||||
|
@ -209,6 +204,7 @@ import {loadPaypalScript} from '../../../ppcp-button/resources/js/modules/Helper
|
|||
let createOrderInPayPal = actionHandler.createOrder()
|
||||
const processInWooAndCapture = async (data) => {
|
||||
try {
|
||||
console.log('processInWooAndCapture', data)
|
||||
const billingContact = data.billing_contact
|
||||
const shippingContact = data.shipping_contact
|
||||
jQuery.ajax({
|
||||
|
@ -222,32 +218,7 @@ import {loadPaypalScript} from '../../../ppcp-button/resources/js/modules/Helper
|
|||
'billing_contact': billingContact,
|
||||
'token': event.payment.token,
|
||||
'shipping_method': selectedShippingMethod,
|
||||
'mollie-payments-for-woocommerce_issuer_applepay': 'applepay',
|
||||
'woocommerce-process-checkout-nonce': nonce,
|
||||
'billing_first_name': billingContact.givenName || '',
|
||||
'billing_last_name': billingContact.familyName || '',
|
||||
'billing_company': '',
|
||||
'billing_country': billingContact.countryCode || '',
|
||||
'billing_address_1': billingContact.addressLines[0] || '',
|
||||
'billing_address_2': billingContact.addressLines[1] || '',
|
||||
'billing_postcode': billingContact.postalCode || '',
|
||||
'billing_city': billingContact.locality || '',
|
||||
'billing_state': billingContact.administrativeArea || '',
|
||||
'billing_phone': billingContact.phoneNumber || '000000000000',
|
||||
'billing_email': shippingContact.emailAddress || '',
|
||||
'shipping_first_name': shippingContact.givenName || '',
|
||||
'shipping_last_name': shippingContact.familyName || '',
|
||||
'shipping_company': '',
|
||||
'shipping_country': shippingContact.countryCode || '',
|
||||
'shipping_address_1': shippingContact.addressLines[0] || '',
|
||||
'shipping_address_2': shippingContact.addressLines[1] || '',
|
||||
'shipping_postcode': shippingContact.postalCode || '',
|
||||
'shipping_city': shippingContact.locality || '',
|
||||
'shipping_state': shippingContact.administrativeArea || '',
|
||||
'shipping_phone': shippingContact.phoneNumber || '000000000000',
|
||||
'shipping_email': shippingContact.emailAddress || '',
|
||||
'order_comments': '',
|
||||
'payment_method': 'ppcp-gateway',
|
||||
'funding_source': 'applepay',
|
||||
'_wp_http_referer': '/?wc-ajax=update_order_review',
|
||||
'paypal_order_id': data.paypal_order_id,
|
||||
|
@ -255,18 +226,18 @@ import {loadPaypalScript} from '../../../ppcp-button/resources/js/modules/Helper
|
|||
complete: (jqXHR, textStatus) => {
|
||||
},
|
||||
success: (authorizationResult, textStatus, jqXHR) => {
|
||||
let result = authorizationResult.data
|
||||
|
||||
if (authorizationResult.success === true) {
|
||||
redirectionUrl = result['returnUrl'];
|
||||
session.completePayment(result['responseToApple'])
|
||||
window.location.href = redirectionUrl
|
||||
} else {
|
||||
result.errors = createAppleErrors(result.errors)
|
||||
session.completePayment(result)
|
||||
}
|
||||
console.log('success authorizationResult', authorizationResult)
|
||||
if (authorizationResult.result === "success") {
|
||||
redirectionUrl = authorizationResult.redirect;
|
||||
//session.completePayment(ApplePaySession.STATUS_SUCCESS)
|
||||
window.location.href = redirectionUrl
|
||||
} else {
|
||||
//session.completePayment(ApplePaySession.STATUS_FAILURE)
|
||||
}
|
||||
},
|
||||
error: (jqXHR, textStatus, errorThrown) => {
|
||||
console.log('error authorizationResult', errorThrown)
|
||||
session.completePayment(ApplePaySession.STATUS_FAILURE)
|
||||
console.warn(textStatus, errorThrown)
|
||||
session.abort()
|
||||
},
|
||||
|
@ -276,6 +247,7 @@ import {loadPaypalScript} from '../../../ppcp-button/resources/js/modules/Helper
|
|||
}
|
||||
}
|
||||
createOrderInPayPal([], []).then((orderId) => {
|
||||
console.log('createOrderInPayPal', orderId)
|
||||
applepay.confirmOrder(
|
||||
{
|
||||
orderId: orderId,
|
||||
|
@ -283,27 +255,18 @@ import {loadPaypalScript} from '../../../ppcp-button/resources/js/modules/Helper
|
|||
billingContact: event.payment.billingContact
|
||||
}
|
||||
).then(
|
||||
confirmResult => {
|
||||
() => {
|
||||
session.completePayment(ApplePaySession.STATUS_SUCCESS);
|
||||
let data = {
|
||||
billing_contact: event.payment.billingContact,
|
||||
shipping_contact: event.payment.shippingContact,
|
||||
paypal_order_id: orderId
|
||||
}
|
||||
processInWooAndCapture(data).then(
|
||||
() => {
|
||||
console.log('done in woo and capture')
|
||||
let result = confirmResult.data
|
||||
redirectionUrl = result['returnUrl'];
|
||||
session.completePayment(result['responseToApple'])
|
||||
window.location.href = redirectionUrl
|
||||
}
|
||||
)
|
||||
processInWooAndCapture(data)
|
||||
}
|
||||
)
|
||||
.catch(err => {
|
||||
).catch(err => {
|
||||
console.error('Error confirming order with applepay token');
|
||||
session.completePayment(ApplePaySession.STATUS_FAILURE);
|
||||
console.error('Error confirming order with applepay token');
|
||||
console.error(err);
|
||||
}
|
||||
);
|
||||
|
|
|
@ -292,9 +292,10 @@ class ApplepayPaymentMethod {
|
|||
* @throws \Exception
|
||||
*/
|
||||
public function create_wc_order() {
|
||||
$this->response_after_successful_result();
|
||||
//$this->response_after_successful_result();
|
||||
$applepay_request_data_object = $this->applepay_data_object_http();
|
||||
$applepay_request_data_object->order_data('productDetail');
|
||||
$this->update_posted_data($applepay_request_data_object);
|
||||
$cart_item_key = $this->prepare_cart($applepay_request_data_object);
|
||||
$cart = WC()->cart;
|
||||
$this->which_calculate_totals($cart, $applepay_request_data_object );
|
||||
|
@ -311,9 +312,12 @@ class ApplepayPaymentMethod {
|
|||
}
|
||||
$this->add_addresses_to_order($applepay_request_data_object);
|
||||
//add_action('woocommerce_checkout_order_processed', array($this, 'process_order_as_paid'), 10, 3);
|
||||
add_filter('woocommerce_payment_successful_result', function (array $result) use ($cart, $cart_item_key) : array {
|
||||
$this->clear_current_cart($cart, $cart_item_key);
|
||||
$this->reload_cart( $cart );
|
||||
return $result;
|
||||
});
|
||||
WC()->checkout()->process_checkout();
|
||||
$this->clear_current_cart($cart, $cart_item_key);
|
||||
$this->reload_cart( $cart );
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -746,4 +750,44 @@ class ApplepayPaymentMethod {
|
|||
wc_reduce_stock_levels($order_id);
|
||||
$order->save();
|
||||
}
|
||||
|
||||
protected function update_posted_data( $applepay_request_data_object )
|
||||
{
|
||||
add_filter(
|
||||
'woocommerce_checkout_posted_data',
|
||||
function ($data) use ($applepay_request_data_object) {
|
||||
|
||||
$data['payment_method'] = "ppcp-gateway";
|
||||
$data['shipping_method'] = $applepay_request_data_object->shipping_method();
|
||||
$data['billing_first_name'] = $applepay_request_data_object->billing_address()['first_name'] ?? '';
|
||||
$data['billing_last_name'] = $applepay_request_data_object->billing_address()['last_name'] ?? '';
|
||||
$data['billing_company'] = $applepay_request_data_object->billing_address()['company'] ?? '';
|
||||
$data['billing_country'] = $applepay_request_data_object->billing_address()['country'] ?? '';
|
||||
$data['billing_address_1'] = $applepay_request_data_object->billing_address()['address_1'] ?? '';
|
||||
$data['billing_address_2'] = $applepay_request_data_object->billing_address()['address_2'] ?? '';
|
||||
$data['billing_city'] = $applepay_request_data_object->billing_address()['city'] ?? '';
|
||||
$data['billing_state'] = $applepay_request_data_object->billing_address()['state'] ?? '';
|
||||
$data['billing_postcode'] = $applepay_request_data_object->billing_address()['postcode'] ?? '';
|
||||
|
||||
|
||||
if ( $applepay_request_data_object->shipping_method() !== null ) {
|
||||
$data['billing_email'] = $applepay_request_data_object->shipping_address()['email'] ?? '';
|
||||
$data['billing_phone'] = $applepay_request_data_object->shipping_address()['phone'] ?? '';
|
||||
$data['shipping_first_name'] = $applepay_request_data_object->shipping_address()['first_name'] ?? '';
|
||||
$data['shipping_last_name'] = $applepay_request_data_object->shipping_address()['last_name'] ?? '';
|
||||
$data['shipping_company'] = $applepay_request_data_object->shipping_address()['company'] ?? '';
|
||||
$data['shipping_country'] = $applepay_request_data_object->shipping_address()['country'] ?? '';
|
||||
$data['shipping_address_1'] = $applepay_request_data_object->shipping_address()['address_1'] ?? '';
|
||||
$data['shipping_address_2'] = $applepay_request_data_object->shipping_address()['address_2'] ?? '';
|
||||
$data['shipping_city'] = $applepay_request_data_object->shipping_address()['city'] ?? '';
|
||||
$data['shipping_state'] = $applepay_request_data_object->shipping_address()['state'] ?? '';
|
||||
$data['shipping_postcode'] = $applepay_request_data_object->shipping_address()['postcode'] ?? '';
|
||||
$data['shipping_email'] = $applepay_request_data_object->shipping_address()['email'] ?? '';
|
||||
$data['shipping_phone'] = $applepay_request_data_object->shipping_address()['phone'] ?? '';
|
||||
}
|
||||
|
||||
return $data;
|
||||
}
|
||||
);
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue