BitpayX.php 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. <?php
  2. namespace App\Http\Controllers\Gateway;
  3. use App\Models\Payment;
  4. use Auth;
  5. use GuzzleHttp\Client;
  6. use Illuminate\Http\JsonResponse;
  7. use Log;
  8. use Response;
  9. class BitpayX extends AbstractPayment {
  10. private $bitpayGatewayUrl = 'https://api.mugglepay.com/v1/';
  11. public function purchase($request): JsonResponse {
  12. $payment = $this->creatNewPayment(Auth::id(), $request->input('oid'), $request->input('amount'));
  13. $data = [
  14. 'merchant_order_id' => $payment->trade_no,
  15. 'price_amount' => (float) $request->input('amount'),
  16. 'price_currency' => 'CNY',
  17. 'pay_currency' => $request->input('type') == 1? 'ALIPAY' : 'WECHAT',
  18. 'title' => '支付单号:'.$payment->trade_no,
  19. 'description' => parent::$systemConfig['subject_name']?: parent::$systemConfig['website_name'],
  20. 'callback_url' => (parent::$systemConfig['website_callback_url']?: parent::$systemConfig['website_url']).'/callback/notify?method=bitpayx',
  21. 'success_url' => parent::$systemConfig['website_url'].'/invoices',
  22. 'cancel_url' => parent::$systemConfig['website_url'],
  23. 'token' => $this->sign($this->prepareSignId($payment->trade_no)),
  24. ];
  25. $result = json_decode($this->mprequest($data), true);
  26. if($result['status'] === 200 || $result['status'] === 201){
  27. $result['payment_url'] .= '&lang=zh';
  28. Payment::whereId($payment->id)->update(['url' => $result['payment_url']]);
  29. return Response::json([
  30. 'status' => 'success',
  31. 'url' => $result['payment_url'],
  32. 'message' => '创建订单成功!'
  33. ]);
  34. }
  35. return Response::json(['status' => 'fail', 'data' => $result, 'message' => '创建订单失败!']);
  36. }
  37. private function sign($data) {
  38. return strtolower(md5(md5($data).parent::$systemConfig['bitpay_secret']));
  39. }
  40. private function prepareSignId($tradeno) {
  41. $data_sign = [
  42. 'merchant_order_id' => $tradeno,
  43. 'secret' => parent::$systemConfig['bitpay_secret'],
  44. 'type' => 'FIAT'
  45. ];
  46. return http_build_query(ksort($data_sign));
  47. }
  48. private function mprequest($data, $type = 'pay') {
  49. $client = new Client(['base_uri' => $this->bitpayGatewayUrl, 'timeout' => 10]);
  50. if($type === 'query'){
  51. $request = $client->get('orders/merchant_order_id/status?id='.$data['merchant_order_id'],
  52. ['json' => ['token' => parent::$systemConfig['bitpay_secret']]]);
  53. }else{// pay
  54. $request = $client->post('orders',
  55. ['json' => ['token' => parent::$systemConfig['bitpay_secret']], 'body' => json_encode($data)]);
  56. }
  57. if($request->getStatusCode() != 200){
  58. Log::debug('BitPayX请求支付错误:'.var_export($request, true));
  59. }
  60. return $request->getBody();
  61. }
  62. public function notify($request): void {
  63. $inputString = file_get_contents('php://input', 'r');
  64. $inputStripped = str_replace(["\r", "\n", "\t", "\v"], '', $inputString);
  65. $inputJSON = json_decode($inputStripped, true); //convert JSON into array
  66. $data = [];
  67. if($inputJSON != null){
  68. $data = [
  69. 'status' => $inputJSON['status'],
  70. 'order_id' => $inputJSON['order_id'],
  71. 'merchant_order_id' => $inputJSON['merchant_order_id'],
  72. 'price_amount' => $inputJSON['price_amount'],
  73. 'price_currency' => $inputJSON['price_currency'],
  74. 'created_at_t' => $inputJSON['created_at_t'],
  75. ];
  76. }
  77. // 准备待签名数据
  78. $str_to_sign = $this->prepareSignId($inputJSON['merchant_order_id']);
  79. $isPaid = $data != null && $data['status'] != null && $data['status'] === 'PAID';
  80. if($this->sign($str_to_sign) === $inputJSON['token'] && $isPaid){
  81. $this->postPayment($inputJSON['merchant_order_id'], 'BitPayX');
  82. echo json_encode(['status' => 200]);
  83. }else{
  84. echo json_encode(['status' => 400]);
  85. }
  86. exit();
  87. }
  88. }