Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save itoshige/a26fba1a56c2383901a02c922090263d to your computer and use it in GitHub Desktop.
Save itoshige/a26fba1a56c2383901a02c922090263d to your computer and use it in GitHub Desktop.
Amazon Pay Sample Code
<html>
<body>
<div style="text-align: center; border: 1px solid #bbb;border-radius: 3px;padding:5px;margin:5px;"><div id="AmazonPayButton"></div></div><button type="button" name="button" id="Logout">Logout</button>
<div style="color:red;" id="errorMessage">配送先を選択してください</div>
<div id="billingAddressWidgetDiv" style="height:250px"></div>
<div id="shippingAddressBookWidgetDiv" style="height:250px"></div>
<div id="walletWidgetDiv" style="height:250px"></div>
<input type="button" value="next page" id="nextPage">
<script type='text/javascript'>
// get access token
function getURLParameter(name, source) {
return decodeURIComponent((new RegExp('[?|&amp;|#]' + name + '=' +
'([^&;]+?)(&|#|;|$)').exec(source) || [, ""])[1].replace(/\+/g, '%20')) || null;
}
var accessToken = getURLParameter("access_token", location.hash);
if (typeof accessToken === 'string' && accessToken.match(/^Atza/)) {
document.cookie = "amazon_Login_accessToken=" + accessToken + ";path=/;secure";
}
window.onAmazonLoginReady = function() {
amazon.Login.setClientId("amzn1.XXXXX");
};
window.onAmazonPaymentsReady = function() {
showAddressBookWidget(billingAddressSelected, "billingAddressWidgetDiv");
};
document.getElementById('Logout').onclick = function() {
amazon.Login.logout();
document.cookie = "amazon_Login_accessToken=; expires=Thu, 01 Jan 1970 00:00:00 GMT; path=/";
window.location.href = '/';
};
</script>
<script type='text/javascript'>
var shippingAddress = (function() {
var flag = false;
return {
selected: function() {
flag = true;
},
isSelected: function() {
return flag;
},
unselected: function() {
flag = false;
}
};
})();
function billingAddressSelected() {
document.getElementById("nextPage").disabled = "disabled";
document.getElementById("errorMessage").style.visibility = "visible";
showAddressBookWidget(shippingAddressSelected, "shippingAddressBookWidgetDiv");
shippingAddress.unselected();
}
function shippingAddressSelected() {
console.log(shippingAddress.isSelected());
if(shippingAddress.isSelected()) {
document.getElementById("nextPage").disabled = "";
document.getElementById("errorMessage").style.visibility = "hidden";
}
shippingAddress.selected();
}
function showAddressBookWidget(addressSelected, widgetDiv) {
// AddressBook
new OffAmazonPayments.Widgets.AddressBook({
sellerId: 'XXXXX',
onReady: function (orderReference) {
var orderReferenceId = orderReference.getAmazonOrderReferenceId();
var el;
if ((el = document.getElementById("orderReferenceId"))) {
el.value = orderReferenceId;
}
// Wallet
showWalletWidget(orderReferenceId);
},
onAddressSelect: function (orderReference) {
addressSelected();
// お届け先の住所が変更された時に呼び出されます、ここで手数料などの再計算ができます。
},
design: {
designMode: 'responsive'
},
onError: function (error) {
// エラー処理
// エラーが発生した際にonErrorハンドラーを使って処理することをお勧めします。
// @see https://payments.amazon.com/documentation/lpwa/201954960
console.log('OffAmazonPayments.Widgets.AddressBook', error.getErrorCode(), error.getErrorMessage());
switch (error.getErrorCode()) {
case 'AddressNotModifiable':
// オーダーリファレンスIDのステータスが正しくない場合は、お届け先の住所を変更することができません。
break;
case 'BuyerNotAssociated':
// 購入者とリファレンスIDが正しく関連付けられていません。
    // ウィジェットを表示する前に購入者はログインする必要があります。
break;
case 'BuyerSessionExpired':
// 購入者のセッションの有効期限が切れました。
     // ウィジェットを表示する前に購入者はログインする必要があります。
break;
case 'InvalidAccountStatus':
// マーチャントID(セラーID)がリクエストを実行する為に適切な状態ではありません。
     // 考えられる理由 : 制限がかかっているか、正しく登録が完了されていません。
break;
case 'InvalidOrderReferenceId':
// オーダーリファレンスIDが正しくありません。
break;
case 'InvalidParameterValue':
// 指定されたパラメータの値が正しくありません。
break;
case 'InvalidSellerId':
// マーチャントID(セラーID)が正しくありません。
break;
case 'MissingParameter':
// 指定されたパラメータが正しくありません。
break;
case 'PaymentMethodNotModifiable':
// オーダーリファレンスIDのステータスが正しくない場合はお支払い方法を変更することができません。
break;
case 'ReleaseEnvironmentMismatch':
// 使用しているオーダーリファレンスオブジェクトがリリース環境と一致しません。
break;
case 'StaleOrderReference':
// 使用しているオーダーリファレンスIDがキャンセルされています。
   // キャンセルされたオーダーリファレンスIDでウィジェットを関連付けすることはできません。
break;
case 'UnknownError':
// 不明なエラーが発生しました。(UnknownError)
break;
default:
// 不明なエラーが発生しました。
}
}
}).bind(widgetDiv);
}
function showWalletWidget(orderReferenceId) {
// Wallet
new OffAmazonPayments.Widgets.Wallet({
sellerId: 'XXXXX',
amazonOrderReferenceId: orderReferenceId,
onReady: function(orderReference) {
console.log(orderReference.getAmazonOrderReferenceId());
},
onPaymentSelect: function() {
console.log(arguments);
},
design: {
designMode: 'responsive'
},
onError: function(error) {
// エラー処理
// エラーが発生した際にonErrorハンドラーを使って処理することをお勧めします。
// @see https://payments.amazon.com/documentation/lpwa/201954960
console.log('OffAmazonPayments.Widgets.Wallet', error.getErrorCode(), error.getErrorMessage());
}
}).bind("walletWidgetDiv");
}
</script>
<script type="text/javascript"
src="https://static-fe.payments-amazon.com/OffAmazonPayments/jp/sandbox/lpa/js/Widgets.js"
async></script>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment