StripeWepay.php 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. <?php
  2. /**
  3. * 自己写别抄,抄NMB抄
  4. */
  5. namespace App\Payments;
  6. use Stripe\Source;
  7. use Stripe\Stripe;
  8. class StripeWepay {
  9. public function __construct($config)
  10. {
  11. $this->config = $config;
  12. }
  13. public function form()
  14. {
  15. return [
  16. 'currency' => [
  17. 'label' => '货币单位',
  18. 'description' => '',
  19. 'type' => 'input',
  20. ],
  21. 'stripe_sk_live' => [
  22. 'label' => 'SK_LIVE',
  23. 'description' => '',
  24. 'type' => 'input',
  25. ],
  26. 'stripe_webhook_key' => [
  27. 'label' => 'WebHook密钥签名',
  28. 'description' => '',
  29. 'type' => 'input',
  30. ]
  31. ];
  32. }
  33. public function pay($order)
  34. {
  35. $currency = $this->config['currency'];
  36. $exchange = $this->exchange('CNY', strtoupper($currency));
  37. if (!$exchange) {
  38. abort(500, __('user.order.stripeAlipay.currency_convert_timeout'));
  39. }
  40. Stripe::setApiKey($this->config['stripe_sk_live']);
  41. $source = Source::create([
  42. 'amount' => floor($order['total_amount'] * $exchange),
  43. 'currency' => $currency,
  44. 'type' => 'wechat',
  45. 'statement_descriptor' => $order['trade_no'],
  46. 'metadata' => [
  47. 'user_id' => $order['user_id'],
  48. 'out_trade_no' => $order['trade_no'],
  49. 'identifier' => ''
  50. ],
  51. 'redirect' => [
  52. 'return_url' => $order['return_url']
  53. ]
  54. ]);
  55. if (!$source['wechat']['qr_code_url']) {
  56. abort(500, __('user.order.stripeWepay.gateway_request_failed'));
  57. }
  58. return [
  59. 'type' => 0,
  60. 'data' => $source['wechat']['qr_code_url']
  61. ];
  62. }
  63. public function notify($params)
  64. {
  65. \Stripe\Stripe::setApiKey($this->config['stripe_sk_live']);
  66. try {
  67. $event = \Stripe\Webhook::constructEvent(
  68. file_get_contents('php://input'),
  69. $_SERVER['HTTP_STRIPE_SIGNATURE'],
  70. $this->config['stripe_webhook_key']
  71. );
  72. } catch (\Stripe\Error\SignatureVerification $e) {
  73. abort(400);
  74. }
  75. switch ($event->type) {
  76. case 'source.chargeable':
  77. $object = $event->data->object;
  78. \Stripe\Charge::create([
  79. 'amount' => $object->amount,
  80. 'currency' => $object->currency,
  81. 'source' => $object->id,
  82. 'metadata' => json_decode($object->metadata, true)
  83. ]);
  84. break;
  85. case 'charge.succeeded':
  86. $object = $event->data->object;
  87. if ($object->status === 'succeeded') {
  88. $metaData = isset($object->metadata->out_trade_no) ? $object->metadata : $object->source->metadata;
  89. $tradeNo = $metaData->out_trade_no;
  90. return [
  91. 'trade_no' => $tradeNo,
  92. 'callback_no' => $object->balance_transaction
  93. ];
  94. }
  95. break;
  96. default:
  97. abort(500, 'event is not support');
  98. }
  99. die('success');
  100. }
  101. private function exchange($from, $to)
  102. {
  103. $result = file_get_contents('https://api.exchangerate.host/latest?symbols=' . $to . '&base=' . $from);
  104. $result = json_decode($result, true);
  105. return $result['rates'][$to];
  106. }
  107. }