WeChatClient.php 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. <?php
  2. namespace crmeb\services\easywechat\miniPayment;
  3. use EasyWeChat\Core\AbstractAPI;
  4. use EasyWeChat\Core\AccessToken;
  5. use EasyWeChat\Kernel\Support;
  6. use EasyWeChat\Kernel\Support\Collection;
  7. use EasyWeChat\Kernel\Traits\HasHttpRequests;
  8. use EasyWeChat\Payment\Application;
  9. use EasyWeChat\Payment\Kernel\BaseClient;
  10. use EasyWeChat\Payment\Merchant;
  11. class WeChatClient extends AbstractAPI
  12. {
  13. private $expire_time = 7000;
  14. /**
  15. * 创建订单 支付
  16. */
  17. const API_SET_CREATE_ORDER = 'https://api.weixin.qq.com/shop/pay/createorder';
  18. /**
  19. * 退款
  20. */
  21. const API_SET_REFUND_ORDER = 'https://api.weixin.qq.com/shop/pay/refundorder';
  22. /**
  23. * Merchant instance.
  24. *
  25. * @var \EasyWeChat\Payment\Merchant
  26. */
  27. protected $merchant;
  28. /**
  29. * ProgramSubscribeService constructor.
  30. * @param AccessToken $accessToken
  31. */
  32. public function __construct(AccessToken $accessToken, Merchant $merchant)
  33. {
  34. parent::__construct($accessToken);
  35. $this->merchant = $merchant;
  36. }
  37. /**
  38. * 支付
  39. * @param array $params [
  40. * 'openid'=>'支付者的openid',
  41. * 'out_trade_no'=>'商家合单支付总交易单号',
  42. * 'total_fee'=>'支付金额',
  43. * 'wx_out_trade_no'=>'商家交易单号',
  44. * 'body'=>'商品描述',
  45. * 'attach'=>'支付类型', //product 产品 member 会员
  46. * ]
  47. * @param $isContract
  48. * @return mixed
  49. * @throws \GuzzleHttp\Exception\GuzzleException
  50. */
  51. public function createorder($order)
  52. {
  53. $params = [
  54. 'openid' => $order['openid'], // 支付者的openid
  55. 'combine_trade_no' => $order['out_trade_no'], // 商家合单支付总交易单号
  56. 'expire_time' => time() + $this->expire_time,
  57. 'sub_orders' => [
  58. [
  59. 'mchid' => $this->merchant->merchant_id,
  60. 'amount' => (int)$order['total_fee'],
  61. 'trade_no' => $order['out_trade_no'],
  62. 'description' => $order['body']
  63. ]
  64. ]
  65. ];
  66. return $this->parseJSON('post', [self::API_SET_CREATE_ORDER, json_encode($params)]);
  67. }
  68. /**
  69. * 退款
  70. * @param array $params [
  71. * 'openid'=>'退款者的openid',
  72. * 'trade_no'=>'商家交易单号',
  73. * 'transaction_id'=>'支付单号',
  74. * 'refund_no'=>'商家退款单号',
  75. * 'total_amount'=>'订单总金额',
  76. * 'refund_amount'=>'退款金额', //product 产品 member 会员
  77. * ]
  78. * @return mixed
  79. * @throws \GuzzleHttp\Exception\GuzzleException
  80. */
  81. public function refundorder(array $order)
  82. {
  83. $params = [
  84. 'openid' => $order['openid'],
  85. 'mchid' => $this->merchant->merchant_id,
  86. 'trade_no' => $order['trade_no'],
  87. 'transaction_id' => $order['transaction_id'],
  88. 'refund_no' => $order['refund_no'],
  89. 'total_amount' => $order['total_amount'],
  90. 'refund_amount' => $order['refund_amount'],
  91. ];
  92. return $this->parseJSON('post', [self::API_SET_REFUND_ORDER, json_encode($params)]);
  93. }
  94. }