我想在我的网站上放置“付款”按钮。 以下代码创建了一个付款按钮。
<script src="https://www.paypalobjects.com/api/checkout.js"></script>
<script>
// Render the PayPal button
paypal.Button.render({
// Set your environment
env: 'sandbox', // sandbox | production
// Specify the style of the button
style: {
layout: 'vertical', // horizontal | vertical
size: 'medium', // medium | large | responsive
shape: 'rect', // pill | rect
color: 'gold' // gold | blue | silver | black
},
// Specify allowed and disallowed funding sources
//
// Options:
// - paypal.FUNDING.CARD
// - paypal.FUNDING.CREDIT
// - paypal.FUNDING.ELV
funding: {
allowed: [paypal.FUNDING.CARD, paypal.FUNDING.CREDIT],
disallowed: []
},
// PayPal Client IDs - replace with your own
// Create a PayPal app: https://developer.paypal.com/developer/applications/create
client: {
sandbox: 'ATisM6RpM7e4hAOW4thPsg5jY3HlHKGLnXjz4ahB59MUj4uVTYNcc6l2bMTdVhjzuLy031UaphwTqQj8', //'AZDxjDScFpQtjWTOUtWKbyN_bDt4OgqaF4eYXlewfBP4-8aqX3PiV8e1GWU6liB2CUXlkA59kJXE7M6R',
production:'ATisM6RpM7e4hAOW4thPsg5jY3HlHKGLnXjz4ahB59MUj4uVTYNcc6l2bMTdVhjzuLy031UaphwTqQj8' //env.payments.paypal.production//'<insert production client id>'
},
//payment: function (data, actions) {
// return actions.payment.create({
// payment: {
// transactions: [
// {
// amount: { total: '0.01', currency: 'USD' }
// }
// ]
// }
// });
//},
//payment() is called when the button is clicked
payment: function () {
// Set up a url on your server to create the payment
var createUrl = '../WebServices/PaypalService.asmx/PayPalCreate';
// Make a call to your server to set up the payment
//return window.paypal.request.post(createUrl)
// .then(function (res) {
// return res.paymentID;
// });
return window.paypal.request({
method:'post',
url: createUrl,
}).then(function(res) {
return res.paymentID;
});
},
onAuthorize: function (data, actions) {
return actions.payment.execute().then(function () {
window.alert('Payment Complete!');
});
}
// onAuthorize() is called when the buyer approves the payment
//onAuthorize: function (data, actions) {
// // Set up a url on your server to execute the payment
// var executeUrl = '/demo/checkout/api/paypal/payment/execute/';
// // Set up the data you need to pass to your server
// var returnData = {
// paymentID: data.paymentID,
// payerID: data.payerID
// };
// // Make a call to your server to execute the payment
// return window.paypal.request.post(executeUrl, returnData)
// .then(function (res) {
// window.alert('Payment Complete!');
// });
//}
}, '#paypal-button-container');
</script>
以下代码是Web服务
[WebMethod(EnableSession = true)]
[ScriptMethod(ResponseFormat = ResponseFormat.Json)]
public void PayPalCreate()
{
var apiContext = new PayPal.Api.APIContext(GetAccessToken());
//apiContext.(paypalSdkConfig());
//var apiContext =
// new PayPal.Api.APIContext("ELEozvVIxT_-MKi2quRFhoRZpim-B4BN98zXvPVrSahVHOBiXDamKBY_Y4d_exENNnD0_WRP7kBvsITW",
// "ATQYe3uKh3M3Yd2ttUuo3WVEY1RjXWP80bBh-PnZbJp9tuWbOEzM8WwxyGf1jag97yXc_HlJIBPAcLm5"); //上线记得改为正式环境 , "sandbox"
var payment = new PayPal.Api.Payment();//交易对象
var payr = new Payer {payment_method = "paypal"};//支付信息
payment.payer = payr;
payment.intent = "sale";//交易类型
var redirectUrls = new RedirectUrls { cancel_url = "http://www.yida.co.kr/", return_url = "http://www.yida.co.kr/" };//回跳信息
payment.redirect_urls= redirectUrls;
var transactions = new List<Transaction>(); //商品信息
var transaction = new Transaction();
var amount = new Amount {total = "3.00", currency = "USD"};//总计费用
transaction.amount = amount;
transaction.description = "testOrder"; //"测试订单"
transaction.invoice_number = "order1"; //"自己系统的订单号"
transactions.Add(transaction);
payment.transactions = transactions;
String payId = null;
try
{
var pay = payment.Create(apiContext);
payId = pay.id;
}
catch (WebException ex)
{
if (ex.Response is HttpWebResponse)
{
HttpStatusCode statusCode = ((HttpWebResponse)ex.Response).StatusCode;
//logger.Info("Got " + statusCode.ToString() + " response from server");
using (WebResponse wResponse = (HttpWebResponse)ex.Response)
{
using (Stream data = wResponse.GetResponseStream())
{
string text = new StreamReader(data).ReadToEnd();
//return text;
}
}
}
//if (!RequiresRetry(ex))
//{
// // Server responses in the range of 4xx and 5xx throw a WebException
// throw new ConnectionException("Invalid HTTP response " + ex.Message);
//}
}
var hash = new Hashtable {{"paymentID", payId}};
var result = new Dictionary<string, string> {{"paymentID", payId}};
var test = "{\"\"}";
Context.Response.Write(JsonConvert.SerializeObject(hash));
}
//public Dictionary<String, String> PaypalSdkConfig(){
// var sdkConfig = new Dictionary<string,string> {{"mode", pay "sandbox"}};
// return sdkConfig;
//}
//public OAuthTokenCredential AuthTokenCredential()
//{
// return new OAuthTokenCredential("ATQYe3uKh3M3Yd2ttUuo3WVEY1RjXWP80bBh-PnZbJp9tuWbOEzM8WwxyGf1jag97yXc_HlJIBPAcLm5", "ELEozvVIxT_-MKi2quRFhoRZpim-B4BN98zXvPVrSahVHOBiXDamKBY_Y4d_exENNnD0_WRP7kBvsITW");
//}
private static string GetAccessToken()
{
var clientId = System.Configuration.ConfigurationManager.AppSettings["clientId"];
var secretToken = System.Configuration.ConfigurationManager.AppSettings["clientSecret"];
var config = new Dictionary<string, string> { { "mode", "sandbox" } };
var tokenCredential = new OAuthTokenCredential(clientId, secretToken, config);
var accessToken = tokenCredential.GetAccessToken();
return accessToken;
}
我可以获得PaymentID。我也有那个Paypal登录页面,但是输入我的AccountName和密码后,我得到了错误 enter image description here enter image description here jquery.min.js:5 POST https://www.sandbox.paypal.com/webapps/hermes/api/batch/setbuyer 500(内部服务器错误) jquery.min.js:5 POST https://www.sandbox.paypal.com/webapps/hermes/api/checkout/EC-6JC97965EW327414Y/session/create 500(内部服务器错误)
我对这个问题一无所知。 我的网站是http://www.yida.co.kr/MemberCenter/PayPalTest.aspx。