Ошибка: < /p>
INVALID_RESOURCE_ID
at https://www.paypal.com/smart/buttons
click_initiate_payment_reject {err: 'Error: INVALID_RESOURCE_ID\n at https://www.payp…false&supportsPopups=true&vault=false
INVALID_RESOURCE_ID
at buttons?style.layout=...)
Кнопка для активации кнопок PayPal:
PayPal
Loading...
Loading PayPal...
< /code>
Функция JavaScript для обработки платежа PayPal < /p>
@if(config('paypal.mode') && config('paypal.' . config('paypal.mode') . '.client_id'))
// Enhanced PayPal integration
document.getElementById('paypal-tab').addEventListener('shown.bs.tab', function() {
// Clear previous errors
document.getElementById('card-errors').textContent = '';
// Check if already loaded
if (typeof paypal !== 'undefined' && typeof paypal.Buttons === 'function') {
renderPayPalButton();
return;
}
// Show loading state
document.getElementById('paypal-button-container').innerHTML = `
Loading...
Loading PayPal...
`;
// Load PayPal SDK
const script = document.createElement('script');
script.src = `https://www.paypal.com/sdk/js?client-id={{ config('paypal.' . config('paypal.mode') . '.client_id') }}¤cy=USD&components=buttons`;
script.async = true;
script.onload = function() {
if (typeof paypal === 'undefined' || typeof paypal.Buttons !== 'function') {
showPayPalError('PayPal failed to load properly');
return;
}
renderPayPalButton();
};
script.onerror = function() {
showPayPalError('Failed to load PayPal SDK. Please refresh or try another method');
};
document.head.appendChild(script);
// Add timeout for slow loading
const paypalLoadTimeout = setTimeout(() => {
if (!window.paypal) {
showPayPalError('PayPal loading delayed. Please check your connection');
}
}, 7000);
});
function showPayPalError(message) {
document.getElementById('paypal-button-container').innerHTML = `
${message}
`;
}
function renderPayPalButton() {
try {
// Clear the container
document.getElementById('paypal-button-container').innerHTML = '';
// Render the button
paypal.Buttons({
style: {
layout: 'vertical',
color: 'blue',
shape: 'rect',
height: 45
},
createOrder: function(data, actions) {
if (!document.getElementById('agree-terms').checked) {
return Promise.reject(new Error('You must agree to the terms and conditions'));
}
document.getElementById('button-text').textContent = 'Creating PayPal Order...';
document.getElementById('button-spinner').classList.remove('d-none');
return fetch('{{ route("paypal.create-order") }}', {
method: 'POST',
headers: {
'Content-Type': 'application/json',
'X-CSRF-TOKEN': '{{ csrf_token() }}'
},
body: JSON.stringify({
plan_id: '{{ $plan->id }}'
})
})
.then(function(res) {
if (!res.ok) {
throw new Error(`HTTP ${res.status}`);
}
return res.json();
})
.then(function(orderData) {
if (!orderData.id || !orderData.status) {
throw new Error('Invalid response from server');
}
return orderData.id;
})
.catch(function(error) {
console.error('Create Order Error:', error);
const errorDiv = document.getElementById('card-errors');
errorDiv.innerHTML = `${error.message}`;
resetButtonState();
return Promise.reject(error);
});
},
onApprove: function(data, actions) {
document.getElementById('button-text').textContent = 'Processing PayPal Payment...';
document.getElementById('button-spinner').classList.remove('d-none');
return fetch('{{ route("paypal.capture-order") }}', {
method: 'POST',
headers: {
'Content-Type': 'application/json',
'X-CSRF-TOKEN': '{{ csrf_token() }}'
},
body: JSON.stringify({
orderID: data.orderID,
plan_id: '{{ $plan->id }}'
})
})
.then(function(res) {
if (!res.ok) {
throw new Error(`HTTP ${res.status}`);
}
return res.json();
})
.then(function(details) {
if (details.error) {
throw new Error(details.error);
}
if (details.redirect_url) {
window.location.href = details.redirect_url;
} else {
window.location.href = '#';
}
})
.catch(function(error) {
console.error('Payment Error:', error);
const errorDiv = document.getElementById('card-errors');
errorDiv.innerHTML = `
${error.message || 'Payment processing failed'}
`;
resetButtonState();
});
},
onError: function(err) {
console.error('PayPal Error:', err);
const errorDiv = document.getElementById('card-errors');
errorDiv.innerHTML = `
${err.message || 'An error occurred with PayPal. Please try again.'}
`;
resetButtonState();
}
}).render('#paypal-button-container');
} catch (err) {
console.error('PayPal Button Initialization Error:', err);
showPayPalError('Failed to initialize PayPal button');
}
}
function resetButtonState() {
document.getElementById('button-text').textContent =
`Complete Subscription for ${{ number_format($plan->price, 2) }}/month`;
document.getElementById('button-spinner').classList.add('d-none');
}
// Global error handler
window.addEventListener('unhandledrejection', event => {
const errorDiv = document.getElementById('card-errors');
errorDiv.textContent = `System error: ${event.reason.message || event.reason}`;
errorDiv.classList.add('alert', 'alert-danger');
resetButtonState();
});
@else
document.getElementById('paypal-tab').addEventListener('shown.bs.tab', function() {
document.getElementById('paypal-button-container').innerHTML = `
PayPal is not configured. Please use another payment method.
`;
});
@endif
< /code>
Контроллер для обработки подписки: < /p>
Подробнее здесь: https://stackoverflow.com/questions/797 ... rt-buttons
Мобильная версия