redenvelope.php 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839
  1. <?php
  2. /* *
  3. * Ping++ Server SDK
  4. * 说明:
  5. * 以下代码只是为了方便商户测试而提供的样例代码,商户可根据自己网站需求按照技术文档编写, 并非一定要使用该代码。
  6. * 接入红包流程参考开发者中心:https://www.pingxx.com/docs/server/red-envelope ,文档可筛选后端语言和接入渠道。
  7. * 该代码仅供学习和研究 Ping++ SDK 使用,仅供参考。
  8. */
  9. require dirname(__FILE__) . '/../init.php';
  10. // api_key 获取方式:登录 [Dashboard](https://dashboard.pingxx.com)->点击管理平台右上角公司名称->开发信息-> Secret Key
  11. $api_key = 'sk_test_ibbTe5jLGCi5rzfH4OqPW9KC';
  12. // app_id 获取方式:登录 [Dashboard](https://dashboard.pingxx.com)->点击你创建的应用->应用首页->应用 ID(App ID)
  13. $app_id = 'app_1Gqj58ynP0mHeX1q';
  14. \Pingpp\Pingpp::setApiKey($api_key);
  15. try {
  16. $red = \Pingpp\RedEnvelope::create(
  17. array(
  18. 'subject' => 'Your Subject',
  19. 'body' => 'Your Body',
  20. 'amount' => 100,// 订单总金额, 人民币单位:分(如订单总金额为 1 元,此处请填 100,金额限制在 100 ~ 20000 之间,即 1 ~ 200 元)
  21. 'order_no' => date('YmdHis') . (microtime(true) % 1) * 1000 . mt_rand(0, 9999),// 红包使用的商户订单号。wx(新渠道)、wx_pub 规定为 1 ~ 28 位不能重复的数字
  22. 'currency' => 'cny',
  23. 'extra' => array(
  24. 'send_name' => 'Send Name'// 商户名称,最多 32 个字节
  25. ),
  26. 'recipient' => 'Openid',// 接收者 id, 为用户在 wx(新渠道)、wx_pub 下的 open_id
  27. 'channel' => 'wx_pub',// 目前支持 wx(新渠道)、 wx_pub
  28. 'app' => array('id' => $app_id),
  29. 'description' => 'Your Description'
  30. )
  31. );
  32. echo $red;// 输出 Ping++ 返回的红包对象 Red_envelope
  33. } catch (\Pingpp\Error\Base $e) {
  34. header('Status: ' . $e->getHttpStatus());
  35. echo($e->getHttpBody());
  36. }