BitpayX.php 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  1. <?php
  2. namespace App\Http\Controllers\Gateway;
  3. use App\Http\Models\Payment;
  4. use Auth;
  5. use Illuminate\Http\Request;
  6. use Response;
  7. class BitpayX extends AbstractPayment
  8. {
  9. private $bitpayGatewayUri = 'https://api.mugglepay.com/v1/';
  10. /**
  11. * @param Request $request
  12. *
  13. * @return mixed
  14. */
  15. public function purchase(Request $request)
  16. {
  17. $payment = new Payment();
  18. $payment->sn = self::generateGuid();
  19. $payment->user_id = Auth::user()->id;
  20. $payment->oid = $request->input('oid');
  21. $payment->amount = $request->input('amount');
  22. $payment->save();
  23. $data = [
  24. 'merchant_order_id' => $payment->sn,
  25. 'price_amount' => (float)$request->input('amount'),
  26. 'price_currency' => 'CNY',
  27. 'pay_currency' => $request->input('type') == 1? 'ALIPAY' : 'WECHAT',
  28. 'title' => '支付单号:'.$payment->sn,
  29. 'description' => parent::$systemConfig['subject_name']? : parent::$systemConfig['website_name'],
  30. 'callback_url' => (parent::$systemConfig['website_callback_url']? : parent::$systemConfig['website_url']).'/callback/notify?method=bitpayx',
  31. 'success_url' => parent::$systemConfig['website_url'].'/invoices',
  32. 'cancel_url' => parent::$systemConfig['website_url'],
  33. 'token' => $this->sign($this->prepareSignId($payment->sn)),
  34. ];
  35. $result = json_decode($this->mprequest($data), TRUE);
  36. if($result['status'] === 200 || $result['status'] === 201){
  37. $result['payment_url'] .= '&lang=zh';
  38. return Response::json(['status' => 'success', 'url' => $result['payment_url'] .= '&lang=zh', 'message' => '创建订单成功!']);
  39. }
  40. return Response::json(['status' => 'fail', 'data' => $result, 'message' => '创建订单失败!']);
  41. }
  42. public function sign($data)
  43. {
  44. return strtolower(md5(md5($data).parent::$systemConfig['bitpay_secret']));
  45. }
  46. public function prepareSignId($tradeno)
  47. {
  48. $data_sign = [
  49. 'merchant_order_id' => $tradeno,
  50. 'secret' => parent::$systemConfig['bitpay_secret'],
  51. 'type' => 'FIAT',
  52. ];
  53. ksort($data_sign);
  54. return http_build_query($data_sign);
  55. }
  56. public function mprequest($data, $type = 'pay')
  57. {
  58. $headers = ['content-type: application/json', 'token: '.parent::$systemConfig['bitpay_secret']];
  59. $curl = curl_init();
  60. if($type === 'pay'){
  61. $this->bitpayGatewayUri .= 'orders';
  62. curl_setopt($curl, CURLOPT_URL, $this->bitpayGatewayUri);
  63. curl_setopt($curl, CURLOPT_POST, 1);
  64. $data_string = json_encode($data);
  65. curl_setopt($curl, CURLOPT_POSTFIELDS, $data_string);
  66. }elseif($type === 'query'){
  67. $this->bitpayGatewayUri .= 'orders/merchant_order_id/status?id='.$data['merchant_order_id'];
  68. curl_setopt($curl, CURLOPT_URL, $this->bitpayGatewayUri);
  69. curl_setopt($curl, CURLOPT_HTTPGET, 1);
  70. }
  71. curl_setopt($curl, CURLOPT_HTTPHEADER, $headers);
  72. curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
  73. curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, FALSE);
  74. curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, FALSE);
  75. $data = curl_exec($curl);
  76. curl_close($curl);
  77. return $data;
  78. }
  79. public function notify(Request $request)
  80. {
  81. $inputString = file_get_contents('php://input', 'r');
  82. $inputStripped = str_replace(["\r", "\n", "\t", "\v"], '', $inputString);
  83. $inputJSON = json_decode($inputStripped, TRUE); //convert JSON into array
  84. $data = [];
  85. if($inputJSON !== NULL){
  86. $data = [
  87. 'status' => $inputJSON['status'],
  88. 'order_id' => $inputJSON['order_id'],
  89. 'merchant_order_id' => $inputJSON['merchant_order_id'],
  90. 'price_amount' => $inputJSON['price_amount'],
  91. 'price_currency' => $inputJSON['price_currency'],
  92. 'created_at_t' => $inputJSON['created_at_t'],
  93. ];
  94. }
  95. // 准备待签名数据
  96. $str_to_sign = $this->prepareSignId($inputJSON['merchant_order_id']);
  97. $resultVerify = $this->verify($str_to_sign, $inputJSON['token']);
  98. $isPaid = $data !== NULL && $data['status'] !== NULL && $data['status'] === 'PAID';
  99. if($resultVerify && $isPaid){
  100. $this->postPayment($inputJSON['merchant_order_id'], 'BitPayX');
  101. $return = [];
  102. $return['status'] = 200;
  103. echo json_encode($return);
  104. }else{
  105. $return = [];
  106. $return['status'] = 400;
  107. echo json_encode($return);
  108. }
  109. exit();
  110. }
  111. public function verify($data, $signature)
  112. {
  113. $mySign = $this->sign($data);
  114. return $mySign === $signature;
  115. }
  116. public function getReturnHTML(Request $request)
  117. {
  118. // TODO: Implement getReturnHTML() method.
  119. }
  120. public function getPurchaseHTML()
  121. {
  122. // TODO: Implement getPurchaseHTML() method.
  123. }
  124. }