跳出ping++退款的坑

近期在项目的开发过程中,需要用到ping++的退款功能,由于使用的版本比官方提供的要低2个小版本,因此问题并不是很大。但是由于官方文档有些内容写的比较含蓄,因此遇到了一些问题。
我们可以通过如下的方式来获取SDK的版本:

>>> import pingpp
>>> pingpp.VERSION
'2.0.7'
>>> pingpp.api_version
'2.0.7'

在官方文档得说明中,我们可以看到这样3句代码:

import pingpp
pingpp.api_key = 'sk_test_ibbTe5jLGCi5rzfH4OqPW9KC'
ch = pingpp.Charge.retrieve('CH-ID')
re = ch.refunds.create(description='desc', amount=1)

入坑

在这里,我们看到我们先导入pingpp库,然后通过赋值的方式将其传入。然后我们通过pingpp得Charge类的retrieve方法获取给定ch_id,然后再根据其refunds属性得create方法传入关键字参数来实现退款的操作。
如果我们没有传入api_key,将得到1个AuthenticationError权限错误:

AuthenticationError: No API key provided. (HINT: set your API key using "pingpp.api_key = <API-KEY>"). You can generate
 API keys from the Ping++ web interface.  See https://pingxx.com for details, or email support@pingxx.com if you have a
ny questions.

在这里,我按照官方提供的方式进行赋值,直接就出现上面的情况了,让人比较纳闷。

出坑

下面我们来看下其实现的源码,在resource模块下的Charge类继承自3个类,分别为CreateableAPIResource, ListableAPIResource,UpdateableAPIResource。其源码如下:

class Charge(CreateableAPIResource, ListableAPIResource,
             UpdateableAPIResource):
    def refund(self, **params):
        ...

而这3个父类继承自APIResource类,而APIResource类继承自PingppObject类,它是1个Pingpp对象。

class CreateableAPIResource(APIResource):
    @classmethod
    def create(cls, api_key=None, **params):
        requestor = api_requestor.APIRequestor(api_key)
        url = cls.class_url()
        response, api_key = requestor.request('post', url, params)
        return convert_to_pingpp_object(response, api_key)

class APIResource(PingppObject):
    @classmethod
    def retrieve(cls, id, api_key=None, **params):
        instance = cls(id, api_key, **params)
        instance.refresh()
        return instance

    def refresh(self):
        self.refresh_from(self.request('get', self.instance_url()))
        return self
    @classmethod
    def class_name(cls):
        ...
        return str(urllib.quote_plus(cls.__name__.lower()))

    @classmethod
    def class_url(cls):
        cls_name = cls.class_name()
        return "/v1/%ss" % (cls_name,)

    def instance_url(self):
        ...
        extn = urllib.quote_plus(id)
        return "%s/%s" % (base, extn)

从上述代码,我们可以发现,我们还可以直接将api_key以关键字参数的形式传入到retrieve方法中。
我们调用Charge类的retrieve方法时,其会生成1个实例,然后调用该实例得refresh方法。而后调研该实例的refresh_from方法,使用get请求,而地址为该实例的url。
因此,最终的url地址为https://api.pingxx.com/v1/charges/,首先class_name方法返回Charge类名的小写名称,而后在class_url方法中进行组装后返回给instance_url方法。
而在调用request方法的过程中,我们会涉及到1个convert_to_pingpp_object方法,其将响应的内容转换为pingpp对象。
通过这种方式我们完成了官方文档中查询Charge对象的操作,即进行如下的操作:

GET https://api.pingxx.com/v1/charges/{CHARGE_ID}

因此,上面ch最终的结果为我们使用API调用后得到的JSON数据结果,而后我们通过ch的refunds属性得到这样1个对象:

 ...
   "refunds": {
    "url": "/v1/charges/ch_xxx/refunds", 
    "has_more": false,
    "object": "list",
    "data": [
      {
        ...
      }
    ]
  }

而这个转换的过程是在refresh_from函数中进行的:

def refresh_from(self, values, api_key=None, partial=False):
        ...
        for k, v in values.iteritems():
            super(PingppObject, self).__setitem__(
                k, convert_to_pingpp_object(v, api_key))

而后我们通过object属性获取到ch.refunds的结果为list。通过如下的方式我们得到的ch.refunds为1个ListObject:

def convert_to_pingpp_object(resp,api_key):
    klass_name = resp.get('object')
    if isinstance(klass_name, basestring):
       klass = types.get(klass_name, PingppObject)

这样,我们在create方法中传入的参数与API文档中创建Refund对象的参数一一对应了。而这些传入的参数将在调用api_requestor模块中得APIRequestor类时传入。其中,url为refund对象中的url属性,即上面的/v1/charges/ch_xxx/refunds
因此,第3行中的关键字参数description和amount正好对应官方文档中的说明。需要提示的是,description参数只能是最大255个unicode字符,不然又会出现一些问题。

总结

其实ping++的SDK是与其API接口对应的,如果你在使用SDK的过程中对其传入的参数不明确,可以查看API文档相应篇章中的说明。不然你会遇到ping++平台给你返回一些让你摸不着头脑的回复。

原文:

http://yuki-onna.github.io/jump-out-of-the-refund-of-ping++/

参考文章:

https://www.pingxx.com/api#api-r-new
https://www.pingxx.com/guidance/server/charge/refund
https://github.com/PingPlusPlus/pingpp-python/blob/2.0.7/example/refund.py

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值