【发布时间】:2021-06-18 00:42:23
【问题描述】:
我正在使用 PHP 和 HTML 创建一个购物车,一切正常。但我希望在创建一个 PayPal(PP) 按钮以适应可变金额(即购物车中的总额)并将货币更改为澳元 (AUD) 方面提供一些帮助。当我提交下面的代码时,只要我不尝试更改默认为美元的货币,它就可以正常工作。我还需要设置一个返回“感谢”URL 和一个“取消”URL。我已经用 Google 搜索了几个小时,但到目前为止还没有找到适合我的解决方案。
任何帮助将不胜感激,并在期待中感谢您。
<!-- now the button -->
<form action="https://www.sandbox.paypal.com/cgi-bin/webscr" method="post" target="_blank">
<input type="hidden" name="cmd" value="_cart">
<input type="hidden" name="business" value="xxxx@yyyy.com.au">
<input type="hidden" name="item_name" value="Item Description">
<input type="hidden" name="item_number" value="">
<input type="hidden" name="amount" value="$total">
<input type="hidden" name="handling" value="0.00">
<input type="hidden" name="shipping" value="0.00">
<!-- <input type="hidden" name="currency_code" value="AUD"> -->
<input type="hidden" name="lc" value="AU">
<input type="hidden" name="bn" value="PP-BuyNowBF">
<input type="image" src="https://www.paypalobjects.com/en_AU/i/btn/btn_buynowCC_LG.gif" border="0" name="submit" alt="PayPal – The safer, easier way to pay online!">
<img alt="" border="0" src="https://www.paypalobjects.com/en_AU/i/scr/pixel.gif" width="1" height="1">
<input type="hidden" name="add" value="1">
</form>
感谢 Preston PHX 的建议,我的代码现在看起来像这样,这很好,但是单击 PayPal 链接,即使它显示了手形图标,也不会去任何地方。请告诉我我做错了什么或错过了什么。谢谢。
echo <<<DD1
<!-- DEVELOPMENT TEST!!!-->
<div id="smart-button-container" style="width: 60%; margin-left: 21%;">
<div style="text-align: left"><label class="checkout_form_mt_right v_align_top" for="description">Item: </label><input type="text" name="descriptionInput" id="description" size="40" value="Text here"></div>
<p id="descriptionError" style="visibility: hidden; color:red; text-align: center;">Please enter a description</p>
<div style="text-align: left"><label class="checkout_form_mt_right v_align_top" for="amount">Amount: </label><input name="amountInput" type="string" id="amount" value="$total"><span> AUD</span><span class='v_align_top white_text'><small><i> Australian dollars</i></small></span></div>
<p id="priceLabelError" style="visibility: hidden; color:red; text-align: center;">Please enter a price</p>
<div id="invoiceidDiv" style="text-align: center; display: none;"><label for="invoiceid"> </label><input name="invoiceid" maxlength="127" type="text" id="invoiceid" value="" ></div>
<p id="invoiceidError" style="visibility: hidden; color:red; text-align: center;">Please enter an Invoice ID</p>
<div style="text-align: center; margin-top: 0.625rem;" id="paypal-button-container"></div>
</div>
<script src="https://www.paypal.com/sdk/js?client-id=sb¤cy=AUD" data-sdk-integration-source="button-factory"></script>
<script>
function initPayPalButton() {
var description = document.querySelector('#smart-button-container #description');
var amount = document.querySelector('#smart-button-container #amount');
var descriptionError = document.querySelector('#smart-button-container #descriptionError');
var priceError = document.querySelector('#smart-button-container #priceLabelError');
var invoiceid = document.querySelector('#smart-button-container #invoiceid');
var invoiceidError = document.querySelector('#smart-button-container #invoiceidError');
var invoiceidDiv = document.querySelector('#smart-button-container #invoiceidDiv');
var elArr = [description, amount];
if (invoiceidDiv.firstChild.innerHTML.length > 1) {
invoiceidDiv.style.display = "block";
}
var purchase_units = [];
purchase_units[0] = {};
purchase_units[0].amount = {};
function validate(event) {
return event.value.length > 0;
}
paypal.Buttons({
style: {
color: 'gold',
shape: 'rect',
label: 'paypal',
layout: 'vertical',
},
onInit: function (data, actions) {
actions.disable();
if(invoiceidDiv.style.display === "block") {
elArr.push(invoiceid);
}
elArr.forEach(function (item) {
item.addEventListener('keyup', function (event) {
var result = elArr.every(validate);
if (result) {
actions.enable();
} else {
actions.disable();
}
});
});
},
onClick: function () {
if (description.value.length < 1) {
descriptionError.style.visibility = "visible";
} else {
descriptionError.style.visibility = "hidden";
}
if (amount.value.length < 1) {
priceError.style.visibility = "visible";
} else {
priceError.style.visibility = "hidden";
}
if (invoiceid.value.length < 1 && invoiceidDiv.style.display === "block") {
invoiceidError.style.visibility = "visible";
} else {
invoiceidError.style.visibility = "hidden";
}
purchase_units[0].description = description.value;
purchase_units[0].amount.value = amount.value;
if(invoiceid.value !== '') {
purchase_units[0].invoice_id = invoiceid.value;
}
},
createOrder: function (data, actions) {
return actions.order.create({
purchase_units: purchase_units,
});
},
onApprove: function (data, actions) {
return actions.order.capture().then(function (details) {
alert('Transaction completed by ' + details.payer.name.given_name + '!');
});
},
onError: function (err) {
console.log(err);
}
}).render('#paypal-button-container');
}
initPayPalButton();
</script>
<br /><br />
DD1; } // 结束开发
PayPal 栏应该是提交按钮吗?如果是这样,我设置它的方式将无济于事。我确实有一个开发人员 API 密钥,但不知道是否/如何/在哪里合并它。我删除了表单 - 感谢您的建议。
再次,任何建议将不胜感激。
这是为黄色 PayPal 按钮生成的代码的屏幕截图。如前所述,我看不到任何 HTML
请注意,目前我正在尝试进入沙盒 - 尽管我注意到 Preston 提供的链接包含“开发者”,但可能有一个不同的链接可以为沙盒生成按钮?
【问题讨论】:
-
您生成的智能按钮不应进入
<form>。如果您仍然遇到问题,请发布生成的完整代码(没有添加或删除任何内容)。 -
对于 Preston PHX - 我确实对表单区域代码进行了一些更改,但它们都是标准的 HTML 更改。我希望这些变化不会把事情搞砸。我还使用 Firefox CTRL+SHIFT+I 查看了代码,没有显示错误。
-
嗨 Preston PHX,生成的代码大约有 6 页。在 Firefox 中,我无法选择代码,所以我对它进行了屏幕抓取。这对你有用吗?我怎么把 jpg 屏幕截图给你?再次感谢您的关注。
-
通过说“发布生成的完整代码”,我指的是按钮生成器输出的内容——而不是加载和运行时在浏览器中呈现的内容。
标签: paypal