【问题标题】:PayFast - Subscription cancellation on sandbox sitePayFast - 沙盒网站上的订阅取消
【发布时间】:2018-08-01 01:06:50
【问题描述】:

如何远程向 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&notify_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)
  }
});
&lt;script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"&gt;&lt;/script&gt;

但是,这些选项都不起作用。

有许多可能的 URL:

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

但是这些项目没有成功。似乎需要将某种标头信息与商家 ID、令牌和订阅一起传递到 PUT 调用中。但是,没有样本就很难复制。

任何有关沙盒 API 取消订阅 url 帖子的帮助将不胜感激。

【问题讨论】:

  • 我假设 CORS 已禁用,因此您无法通过前端的 JS 发送请求
  • 所以你建议客户端需要将canecellation req传递给快速服务器,然后转发取消服务器端......现在开始测试。

标签: javascript html node.js mongodb


【解决方案1】:

对此进行更新。仍然无法正常工作,但有额外的进展。

为了更多的跨域问题,服务器需要发布 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&timestamp=&'+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&timestamp=&'+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&timestamp=&'+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。

请求的任何帮助或示例都会很棒。

【讨论】:

    【解决方案2】:

    PayFast API 服务器不允许跨域资源共享 (CORS)。

    这意味着在进行 ajax 调用时 JQuery 执行的飞行前检查将失败,因为服务器响应不包含“Access-Control-Allow-Origin”标头。

    在您对 PayFast API 的 ajax 调用中,以下将不起作用

    dataType: "json",
    jsonp: false,
    cache: true,
    crossDomain: true,
    

    您需要对服务器端脚本执行 ajax 调用,然后调用 PayFast API。然后,您可以在 ajax 成功中检查 API 调用的结果,以便为用户提供适当的反馈。

    404 响应表示未找到您的端点(不正确的 url),沙盒的正确的 API 取消端点是:

    https://api.payfast.co.za/subscriptions/[your sub token]/cancel?testing=true

    查看PayFast docs 以了解应如何构建端点,以及 API 响应的含义(即 401 - 商家授权失败)

    【讨论】:

      猜你喜欢
      • 2013-06-30
      • 2012-11-23
      • 2013-05-20
      • 2016-09-27
      • 2020-07-24
      • 1970-01-01
      • 2013-08-12
      • 2018-05-18
      • 2018-05-27
      相关资源
      最近更新 更多