Client.php 3.2 KB

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