Stripe.php 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  1. <?php
  2. namespace common\components\payment;
  3. use Yii;
  4. use yii\helpers\ArrayHelper;
  5. use Omnipay\Omnipay;
  6. /**
  7. * Stripe 支付类
  8. *
  9. * Class Stripe
  10. * @package common\components\payment
  11. */
  12. class Stripe
  13. {
  14. const DEFAULT = 'Stripe';
  15. const PI = 'Stripe_PaymentIntents';
  16. /**
  17. * 订单
  18. *
  19. * @var array
  20. */
  21. protected $order;
  22. /**
  23. * 配置
  24. *
  25. * @var
  26. */
  27. protected $config;
  28. /**
  29. * Stripe constructor.
  30. */
  31. public function __construct($config)
  32. {
  33. $this->config = $config;
  34. }
  35. /**
  36. * 实例化类
  37. *
  38. * @param $type
  39. * @return \Omnipay\Stripe\PaymentIntentsGateway
  40. */
  41. private function create($type)
  42. {
  43. /* @var $gateway \Omnipay\Stripe\PaymentIntentsGateway */
  44. $gateway = Omnipay::create($type);
  45. $gateway->initialize([
  46. 'apiKey' => $this->config['secret_key'],
  47. ]);
  48. return $gateway;
  49. }
  50. /**
  51. * @return \Omnipay\Common\Message\ResponseInterface
  52. */
  53. public function notify()
  54. {
  55. $gateway = $this->create(self::PI);
  56. $response = $gateway->completePurchase([
  57. 'request_params' => Yii::$app->request->post()
  58. ])->send();
  59. return $response;
  60. }
  61. /**
  62. * Stripe Payment Intents Gateway
  63. *
  64. * @param array $order
  65. * @param bool $debug
  66. * @return mixed
  67. */
  68. public function card($order)
  69. {
  70. $gateway = $this->create(self::PI);
  71. $transaction = $gateway->purchase(ArrayHelper::merge($this->order, $order));
  72. $response = $transaction->send();
  73. $data = $response->getData();
  74. // if ($response->isSuccessful()) {
  75. // $data = $response->getData();
  76. // } else if($response->isRedirect()) {
  77. // $response->redirect();
  78. // } else {
  79. // // The payment has failed. Use $response->getMessage() to figure out why and return to step (1).
  80. // }
  81. return empty($data['status']) ? 'fail' : $data['status'];
  82. }
  83. /**
  84. * 关闭订单
  85. *
  86. * @param $out_trade_no
  87. * @return mixed
  88. */
  89. public function close($out_trade_no)
  90. {
  91. /** @var $gateway */
  92. $gateway = $this->create(self::DEFAULT);
  93. $response = $gateway->close([
  94. 'out_trade_no' => $out_trade_no, //The merchant trade no
  95. ])->send();
  96. return $response->getData();
  97. }
  98. /**
  99. * 查询订单
  100. *
  101. * @param $transaction_id
  102. */
  103. public function query($transaction_id)
  104. {
  105. $gateway = $this->create(self::DEFAULT);
  106. $response = $gateway->query([
  107. 'transaction_id' => $transaction_id, //The wechat trade no
  108. ])->send();
  109. return $response->getData();
  110. }
  111. /**
  112. * 退款
  113. *
  114. * 订单类型
  115. *
  116. * @param $info
  117. * [
  118. * 'transaction_id' => $transaction_id, //The wechat trade no
  119. * 'out_refund_no' => $outRefundNo,
  120. * 'total_fee' => 1, //=0.01
  121. * 'refund_fee' => 1, //=0.01
  122. * ]
  123. */
  124. public function refund($info)
  125. {
  126. $gateway = $this->create(self::DEFAULT);
  127. $response = $gateway->refund($info)->send();
  128. return $response->getData();
  129. }
  130. }
粤ICP备19079148号