近期在做微信付出那方面的事情,因为要在之前开辟人员的基础上举行开辟,个中运用到了ping++这个第3方付出的SDK。不得不说,ping++的SDK做的挺简朴的,然则其文档至心写的有点坑。不过相对其他的接入,坑少了那末一些。
下面梳理下平常开辟的流程,请点击下面的链接付款。
能够看到主要有5个步骤:
设置 API-Key
SDK 考证署名设置
提议付出要求猎取付出凭证
将取得的付出凭证传给 Client
吸收 Webhooks关照(开启Live形式才有效)
然后再看下SDK生意业务流程,能够发明写的照样挺通俗易懂的。
个人以为比较坑爹的是它API文档的部份,完整就是逗你玩的。为何这么说,请往下看。
官方说Ping++的API采纳REST作风设想,真的是REST作风,完整停留在CURD的阶段,离背面的生长另有很长的路要走。实话说,Github API写的还比较像REST些,而这个ping++的就不想说了。
入坑
下面我们正式入坑,其最简朴的体式格局以下,运用curl举行要求:
curl https://api.pingxx.com/v1/charges \
-u sk_test_ibbTe5jLGCi5rzfH4OqPW9KC:
官方采纳Authentication Basic的体式格局举行考证,平常都邑过的,返回你要的数据。然则官方供应的例子至心的简朴,而且只用uncap这类体式格局,而我们公司要运用的是微信民众付出,那末坑就出来了。当时我是如许要求的:
c@y-pc:~$ curl https://api.pingxx.com/v1/charges -u sk_test_ibbTe5jLGi5rzfH4OqPW9KC: -d order_no=123456789 -d amount=100 -d app[id]=app_1Gqj58ynP0mHeX1q -d channel=wx_pub -d currency=cny -d client_ip=127.0.0.1 -d subject="Your Subject" -d body="Your Body" -d extra='{"open_id":"xxxxx"}'
{
"error": {
"type": "invalid_request_error",
"message": " 无效的参数 extra: 必需是一组键值对",
"param": "extra"
}
注重,这里我们将channel修改成wx_pub
了,因而必需增加1个extra字段,在该字段中有1个open_id
必填的玩意。效果,老是提醒extra必需是1组键值对。很明显,岂非extra='{"open_id":"xxxxx"}
不是1个键值对吗,然则机械看不出来它是。
出坑
背面,我们预备跳出这个坑,我们去看官方给我们供应的SDK中的例子,能够发明app[id]这里的id是以{“id”:”xxxx”}如许的体式格局存在的,而不是理解为app[id]如许1个字段,因而我们换个体式格局举行要求:
curl https://api.pingxx.com/v1/charges -u sk_test_ibbTe5jLGi5rzfH4OqPW9KC: -d order_no=123456789 -d amount=100 -d app[id]=app_1Gqj58ynP0mHeX1q -d channel=wx_pub -d currency=cny -d client_ip=127.0.0.1 -d subject="Your Subject" -d body="Your Body" -d extra[open_id]=xxxxx
{
"id": "ch_C4O4CKXHCK48rnvn5CH8iP4G",
"object": "charge",
"created": 1460257030,
"livemode": false,
"paid": false,
"refunded": false,
"app": "app_1Gqj58ynP0mHeX1q",
"channel": "wx_pub",
"order_no": "123456789",
"client_ip": "127.0.0.1",
"amount": 100,
"amount_settle": 100,
"currency": "cny",
"subject": "Your Subject",
"body": "Your Body",
"extra": {
"open_id": "xxxxx"
},
"time_paid": null,
"time_expire": 1460264230,
"time_settle": null,
"transaction_no": null,
"refunds": {
"object": "list",
"url": "/v1/charges/ch_C4O4CKXHCK48rnvn5CH8iP4G/refunds",
"has_more": false,
"data": []
},
"amount_refunded": 0,
"failure_code": null,
"failure_msg": null,
"metadata": {},
"credential": {
"object": "credential",
"wx_pub": {
"appId": "wxmbrpg8tm1k04yzbt",
"timeStamp": 1460257030,
"nonceStr": "25c7de13570a1b5f6d23614bdf49443d",
"package": "prepay_id=1101000000160410rtepylz1surl5uzd",
"signType": "MD5",
"paySign": "10F1AACE79C0ED0E10EB4080D140F913"
}
},
"description": null
这里我们将open_id
放在extra中就胜利了,如许才能够吸收到。不得不说,这API挪用完整就是1个坑。主假如官方没有供应明白的要求头信息给我们,致使我们一直在兜圈。
所以,我不消除这文档就是1个实习生写的。不得不说,官方给我们供应了1个不知名的REST的scheme,假如不是过这个坑完整就是不知道它在弄哪样。比方jsonapi供应了如许1种要求体式格局,估计假如不申明,你也看不懂它写的是什么:
GET /posts?include=author&sort[posts]=-created,title&sort[people]=name
下面是上述要求的相应头信息:
c@y-pc:~$ curl https://api.pingxx.com/v1/charges -u sk_test_ibbTe5jLGCi5rzfH4OqPW9KC: -d order_no=123456789 -d amount=100 - app[id]=app_1Gqj58ynP0mHeX1q -d channel=wx_pub -d currency=cny -d client_ip=127.0.0.1 -d subject="Your Subject" -d body="Your Body" -d extra[open_id]=xxxxx -v
* Trying 121.43.74.100...
* Connected to api.pingxx.com (121.43.74.100) port 443 (#0)
* ALPN, offering http/1.1
* Cipher selection: ALL:!EXPORT:!EXPORT40:!EXPORT56:!aNULL:!LOW:!RC4:@STRENGTH
* successfully set certificate verify locations:
* CAfile: /etc/ssl/certs/ca-certificates.crt
CApath: none
* TLSv1.2 (OUT), TLS header, Certificate Status (22):
* TLSv1.2 (OUT), TLS handshake, Client hello (1):
* TLSv1.2 (IN), TLS handshake, Server hello (2):
* NPN, negotiated HTTP1.1
* TLSv1.2 (IN), TLS handshake, Certificate (11):
* TLSv1.2 (IN), TLS handshake, Server key exchange (12):
* TLSv1.2 (IN), TLS handshake, Server finished (14):
* TLSv1.2 (OUT), TLS handshake, Client key exchange (16):
* TLSv1.2 (OUT), TLS change cipher, Client hello (1):
* TLSv1.2 (OUT), TLS handshake, Unknown (67):
* TLSv1.2 (OUT), TLS handshake, Finished (20):
* TLSv1.2 (IN), TLS change cipher, Client hello (1):
* TLSv1.2 (IN), TLS handshake, Finished (20):
* SSL connection using TLSv1.2 / ECDHE-RSA-AES128-GCM-SHA256
* ALPN, server did not agree to a protocol
* Server certificate:
* subject: C=CN; ST=Shanghai; L=Shanghai; O=Shanghai Jianmi Network Technology Co., LTD; OU=IT; CN=api.pingxx.com
* start date: Nov 19 00:00:00 2015 GMT
* expire date: Jan 17 23:59:59 2017 GMT
* subjectAltName: api.pingxx.com matched
* issuer: C=US; O=Symantec Corporation; OU=Symantec Trust Network; CN=Symantec Class 3 Secure Server CA - G4
* SSL certificate verify ok.
* Server auth using Basic with user 'sk_test_ibbTe5jLGCi5rzfH4OqPW9KC'
> POST /v1/charges HTTP/1.1
> Host: api.pingxx.com
> Authorization: Basic c2tfdGVzdF9pYmJUZTVqTEdDaTVyemZINE9xUFc5S0M6
> User-Agent: curl/7.47.1
> Accept: */*
> Content-Length: 163
> Content-Type: application/x-www-form-urlencoded
>
* upload completely sent off: 163 out of 163 bytes
< HTTP/1.1 200 OK
< Date: Sun, 10 Apr 2016 03:26:51 GMT
< Content-Type: application/json;charset=utf-8
< Content-Length: 1229
< Connection: keep-alive
< X-Powered-By: PHP/5.6.2
< Access-Control-Allow-Methods: GET, POST, HEAD, OPTIONS, DELETE
< Access-Control-Max-Age: 300
< Access-Control-Allow-Credentials: true
< Cache-Control: no-cache, no-store
< Strict-Transport-Security: max-age=31536000; includeSubDomains
我们运用-v
选项查看了其细致的内容,甚至连TCP的3次握手历程都能够看到。
另有就是失足信息,最好看下面的链接报错信息。
参考文章:
https://www.pingxx.com/document/api/#api-charges