如何远程向PayFast沙盒网站发出取消订阅?
该假设是使用Ajax PUT或Form PUT。
<form action="https://api.payfast.co.za/subscription/d776203c-341e-c12d-16cb-aa8872d5edee/cancel?testing=true" method="PUT">
<input type="hidden" name="merchant_id" value="10007713"><input type="hidden" name="merchant_key" value="hagd8cby0yk5o"><input type="hidden" name="return_url" value="https://fraternate.herokuapp.com/payment_finished"><input type="hidden" name="cancel_url" value="https://fraternate.herokuapp.com/payment_cancelled"><input type="hidden" name="notify_url" value="https://fraternate.herokuapp.com/payment_notify"><input type="hidden" name="name_first" value="Bob"><input type="hidden" name="name_last" value="Smith"><input type="hidden" name="email_address" value="s@gmail.com"><input type="hidden" name="m_payment_id" value="FAKE5551234"><input type="hidden" name="amount" value="1080.00"><input type="hidden" name="item_name" value="Single User - Subscription"><input type="hidden" name="item_description" value=""><input type="hidden" name="email_confirmation" value="1"><input type="hidden" name="confirmation_address" value="s@gmail.com"><input type="hidden" name="subscription_type" value="1"><input type="hidden" name="frequency" value="6"><input type="hidden" name="cycles" value="0"><input type="hidden" name="custom_str1" value="s@gmail.com"><input type="hidden" name="custom_str2" value="595b8b63dfbafe053c10e425">
<button type="submit">submit</button>
</form>
或
$.ajax({
type: "PUT", /* or type:"GET" or type:"PUT" */
crossDomain: true,
url: 'https://sandbox.payfast.co.za/sb/subs/cancel/f0d6c3bf-223a-958f-e4dd-5c37ef87821b?merchant_id=10007713&merchant_key=hagd8cby0yk5o&return_url=https%3A%2F%2Ffraternate.herokuapp.com%2Fpayment_finished&cancel_url=https%3A%2F%2Ffraternate.herokuapp.com%2Fpayment_cancelled¬ify_url=https%3A%2F%2Ffraternate.herokuapp.com%2Fpayment_notify&name_first=Bob&name_last=Smith&email_address=sd%40gmail.com&m_payment_id=FAKE5551234&amount=1080.00&item_name=Single+User+-+Subscription&item_description=&email_confirmation=1&confirmation_address=sd%40gmail.com&subscription_type=1&frequency=6&cycles=0&custom_str1=sd%40gmail.com&custom_str2=595b8b63dfbafe053c10e425',
success: function(data){
console.log('data',)
},
error:function(err){
console.log(err)
}
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
然而,这些选项都不起作用。
有许多可能的网址:
https://sandbox.payfast.co.za/sb/subs/cancel/d776203c-341e-c12d-16cb-aa8872d5edee
https://api.payfast.co.za/[endpoint]/[token]/[action]?testing=true
https://sandbox.payfast.co.za/subscriptions/d776203c-341e-c12d-16cb-aa8872d5edee/cancel
但这些项目没有成功。似乎需要使用merchcant id,token和subscription将某种头信息传递给PUT调用。但是,如果没有样本,则很难复制。
任何使用沙箱API取消订阅网址的协助都将不胜感激。
答案 0 :(得分:0)
要对此进行更新。仍然无法正常工作,但需要额外的进展。
为了更多的交叉orgin问题,服务器需要发布put和get。 这是通过npm require完成的。
var request = require('request'); // http requests
var moment = require('moment'); // timestamp
详情适用于沙盒上的商家帐户
Merchant ID :10007713
遵循developerkit API集成说明的详细信息。 (主要假设https://sandbox.payfast.co.za/eng/process = https://api.payfast.co.za/)
var timestamp = moment().toISOString();
request.get('https://https://sandbox.payfast.co.za/eng/process/subscriptions/ping', function (error, response, body) {
console.log('error:', error); // Print the error if one occurred
console.log('statusCode:', response && response.statusCode); // Print the response status code if a response was received
console.log('body:', body); // Print the HTML for the Google homepage.
});
request.get('https://https://sandbox.payfast.co.za/eng/process/subscriptions/f0d6c3bf-223a-958f-e4dd-5c37ef87821b/ping', function (error, response, body) {
console.log('error:', error); // Print the error if one occurred
console.log('statusCode:', response && response.statusCode); // Print the response status code if a response was received
console.log('body:', body); // Print the HTML for the Google homepage.
});
request.get('https://https://sandbox.payfast.co.za/eng/process/subscriptions/f0d6c3bf-223a-958f-e4dd-5c37ef87821b/ping?merchant_id=10007713&version=v1×tamp=&'+timestamp+'&signature=dc4a98c5b5ee17540be5ea5cf458efa7', function (error, response, body) {
console.log('error:', error); // Print the error if one occurred
console.log('statusCode:', response && response.statusCode); // Print the response status code if a response was received
console.log('body:', body); // Print the HTML for the Google homepage.
});
request.put('https://https://sandbox.payfast.co.za/eng/process/subscriptions/f0d6c3bf-223a-958f-e4dd-5c37ef87821b/cancel?merchant_id=10007713&version=v1×tamp=&'+timestamp+'&signature=dc4a98c5b5ee17540be5ea5cf458efa7', function (error, response, body) {
console.log('error:', error); // Print the error if one occurred
console.log('statusCode:', response && response.statusCode); // Print the response status code if a response was received
console.log('body:', body); // Print the HTML for the Google homepage.
});
request.put('https://https://sandbox.payfast.co.za/eng/process/subscriptions/f0d6c3bf-223a-958f-e4dd-5c37ef87821b/cancel:80?merchant_id=10007713&version=v1×tamp=&'+timestamp+'&signature=dc4a98c5b5ee17540be5ea5cf458efa7', function (error, response, body) {
console.log('error:', error); // Print the error if one occurred
console.log('statusCode:', response && response.statusCode); // Print the response status code if a response was received
console.log('body:', body); // Print the HTML for the Google homepage.
});
以上所有都经过测试,但没有成功。尝试了各种迭代的沙盒和api以及测试。所有在沙盒上都返回443错误,在取消订阅时返回404或401.
请求的任何帮助或示例都会很棒。
答案 1 :(得分:0)
PayFast API服务器不允许跨源资源共享(CORS)。
这意味着当进行ajax调用时由JQuery执行的飞行前检查将失败,因为服务器响应不包含&#34; Access-Control-Allow-Origin&#34;报头中。
以下在您对PayFast API的ajax调用中无效:
dataType: "json",
jsonp: false,
cache: true,
crossDomain: true,
您需要执行 ajax调用服务器端脚本,然后调用PayFast API。然后,您可以在ajax成功中检查API调用的结果,以便为用户提供适当的反馈。
404的回复表示找不到您的终端(错误的网址),沙箱的正确的API取消终端是:
https://api.payfast.co.za/subscriptions/[your sub token]/cancel?testing=true
检查PayFast docs以查看应如何构建端点,以及API响应的含义(即401 - 商家授权失败)