我正在測試一個基本的PayPal按鈕表單,這個按鈕表單是用PHP動態創建的,並且在頁面加載後經過短暫的延遲後用JS提交給沙盒。大約有一半的時間工作正常,PayPal大約有一半的時間響應500錯誤。當我收到錯誤信息時,我可以回到瀏覽器並讓它重新提交完全相同的表單,此時,它有時會起作用,有時會出錯。使用JavaScript提交PayPal按鈕時發生間歇性500錯誤
任何想法將不勝感激。這可能是某種餅乾問題?我已經向PayPal提交了一張票 - 尚未回覆。謝謝!
編輯:這裏是相關的代碼。這是一個Drupal 8網站,這是一個使用Form API進行多步驟表單的自定義模塊。被稱爲(然後validateForm()
)然後submitForm()
。這種情況下的流程在「步驟」3建立之後開始。客戶點擊「支付」,並調用submitForm()
。
public function buildForm(array $form, FormStateInterface $form_state) {
...
//'step' 3
...
//'step' 4 is just to redirect to PayPal
elseif ($form_state->get('step') == 4) {
//prints in main form content area
$form['redirecting'] = array(
'#type' => 'html_tag',
'#tag' => 'div',
'#value' => $this->t('Redirecting to PayPal...'),
);
}
...
}
public function submitForm(array &$form, FormStateInterface $form_state) {
...
//called after 'step' 3
//gets id of record just inserted into database
$paypal_invoice = $query->execute()->fetchField();
$paypal_item_name = '[name of item]';
$paypal_amount = $form_state->getValue('amount');
$token = bin2hex(openssl_random_pseudo_bytes(16));
$paypal_html =
'<form action="https://www.sandbox.paypal.com/cgi-bin/webscr" method="post" target="_top" id="paypal_form">
<input type="hidden" name="cmd" value="_xclick">
<input type="hidden" name="business" value="______________">
<input type="hidden" name="lc" value="US">
<input type="hidden" name="invoice" value="' . $paypal_invoice . '">
<input type="hidden" name="item_name" value="' . $paypal_item_name . '">
<input type="hidden" name="amount" value="' . $paypal_amount . '">
<input type="hidden" name="button_subtype" value="services">
<input type="hidden" name="no_note" value="1">
<input type="hidden" name="no_shipping" value="1">
<input type="hidden" name="rm" value="1">
<input type="hidden" name="return" value="http://__________________">
<input type="hidden" name="cancel_return" value="http://______________?cancel=1&token=' . $token . '">
<input type="hidden" name="currency_code" value="USD">
</form>
<script type="text/javascript">
window.onload = setTimeout(function() {
document.getElementById("paypal_form").submit();
}, 4000);
</script>';
//make sure html tags are evaluated
$rendered_message = \Drupal\Core\Render\Markup::create($paypal_html);
//string will be added to message area of form when 'step' 4 is loaded with buildForm()
drupal_set_message($rendered_message);
$form_state->set(['step'], 4);
...
}
你能分享你的代碼嗎? – bluepnume