2017-11-25 143 views
0

試圖使用this示例代碼在Google Apps腳本中創建條紋結帳和付款。使用Google Apps腳本進行條紋付款

中將Html.HTML文件

<!DOCTYPE html> 
 
<!-- modification of https://stripe.com/docs/checkout#integration-custom --> 
 
<button id="customButton">Purchase</button> 
 

 
<script src="https://checkout.stripe.com/checkout.js"></script> 
 
<script src="//ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script> 
 

 
<script> 
 
var handler = StripeCheckout.configure(
 
{ 
 
    key: 'pk_test_UUbDY16wDCECOujIs0vQ2vTi', 
 
    image: 'https://i.imgur.com/M0ku9HH.png', 
 
    token: function(token) 
 
    { 
 
    // Use the token to create the charge with a server-side script. 
 
    // You can access the token ID with `token.id` 
 
    google.script.run.withSuccessHandler(chargeSuccess) 
 
    google.script.run.withFailureHandler(chargeFailure) 
 
    google.script.run.processCharge(token); 
 
    } 
 
} 
 
); 
 

 
$('#customButton').on('click', function(e) 
 
{ 
 
    // Open Checkout with further options 
 
    handler.open(
 
    { 
 
    name: 'ASD Anastasiya Ballet S.', 
 
    description: 'Pagamento online' 
 
    }); 
 
    e.preventDefault(); 
 
}); 
 
function chargeSuccess(result) { 
 
    // handle response code client side 
 
} 
 
function chargeFailure(error) { 
 
    // client error handling 
 
} 
 
</script>

這裏GS.gs文件

// IFRAME mode required 
function doGet() { 
return HtmlService.createHtmlOutputFromFile('HTML') 
.setSandboxMode(HtmlService.SandboxMode.IFRAME); 
} 

/** 
* Read Stripe token passed from google.script.run instead of 
* using a form POST request - which can't happen in HtmlService. 
* 
* @param {Object} token from checkout.js 
* @return {number} HTTP Response code 
*/ 
function processCharge(token) { 


var tokenId = token.id; 
var stripeEmail = token.email; 

// Create a Customer (optional) 
/* 
var path = "/customers"; 
var customer = Stripe_PostRequest(path, [], [], { 
    "description": "test customer", 
    "source": tokenId, 
    "email": stripeEmail 
}); 

var custId = JSON.parse(customer.getContentText()).id; 
*/ 

// Create a Charge 
path = "/charges"; 
var charge = Stripe_PostRequest(path, [], [], { 
    "currency": "usd", 
    "amount": "500", 
    //"customer": custId 
}); 

return charge.getResponseCode(); 
} 

/** 
* Generic function for making a POST request to the Stripe API. 
* Provided by Stripe support 
* 
* @param {string} path 
* @param {Object} parameters 
* @return {HTTPResponse} 
*/ 
var Stripe_PostRequest = function(path, fields, expandableFields, parameters) { 
    // Expand related fields when accessing sub-properties 
    // (e.g. `customer.email` should expand the customer 
    // object when retrieving a charge). 
    if (expandableFields !== undefined) { 
    parameters["expand[]"] = []; 
    fields.forEach(function(field) { 
     field = field.split(".")[0]; 
     if (expandableFields.indexOf(field) !== -1) { 
     parameters["expand[]"].push("data." + field); 
     } 
    }); 
    } 

    var scriptProperties = PropertiesService.getScriptProperties(); 
    var secret = scriptProperties.getProperty('testSecret'); 

    var options = { 
    "method" : "post", 
    "headers": { 
     "Authorization": "Bearer " + secret, 
     "User-Agent": "Stripe Example/0.1" 
    } 
    }; 
    var url = "https://api.stripe.com/v1" + path + serializeQueryString(parameters); 
    return UrlFetchApp.fetch(url, options); 
} 

/** 
* Serialize a dictionary to a query string for GET requests 
*/ 
var serializeQueryString = function(parameters) { 
    var str = []; 
    for (var key in parameters) { 
    var value = parameters[key]; 
    if (parameters.hasOwnProperty(key) && value) { 
     if (value.map) { 
     str.push(value.map(function(array_value) { 
      return key + "=" + encodeURIComponent(array_value); 
     }).join("&")); 
     } else { 
     str.push(key + "=" + encodeURIComponent(value)); 
     } 
    } 
    } 
    return '?' + str.join("&"); 
} 

的形式,做工精細,但沒有創建費用,因爲似乎沒有令牌創建。 作爲根本不是一個JS專家,我不知道,如果功能

令牌:功能(令牌)

調用

google.script.run.processCharge(令牌);

正確執行。

+0

首先,你的函數調用看起來不工作的例子,如:google.script.run.withSuccessHandler(chargeSuccess) .withFailureHandler(chargeFailure) .processCharge(令牌); – Kos

+0

是的,我改變了它,假設不清楚。這不是問題,因爲我無法發送正確的密鑰。不確定Google Script POST創建功能是否正常工作。任何使用Google Script創建標準stripe.charges.create調用的方式。 – davide445

回答

0

您的代碼失敗,因爲您已經註釋了custId - 必須提供。這樣做沒有任何壞處,它只會創建一個測試客戶。否則,在「條帶」控制面板中創建一個客戶,並將該ID複製到custId中。

您可以隨時查看「查看>執行記錄」以獲取腳本失敗原因的線索。