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. public function mprequest($data, $type = 'pay')
  11. {
  12. $headers = ['content-type: application/json', 'token: '.parent::$systemConfig['bitpay_secret']];
  13. $curl = curl_init();
  14. if($type === 'pay'){
  15. $this->bitpayGatewayUri .= 'orders';
  16. curl_setopt($curl, CURLOPT_URL, $this->bitpayGatewayUri);
  17. curl_setopt($curl, CURLOPT_POST, 1);
  18. $data_string = json_encode($data);
  19. curl_setopt($curl, CURLOPT_POSTFIELDS, $data_string);
  20. }elseif($type === 'query'){
  21. $this->bitpayGatewayUri .= 'orders/merchant_order_id/status?id='.$data['merchant_order_id'];
  22. curl_setopt($curl, CURLOPT_URL, $this->bitpayGatewayUri);
  23. curl_setopt($curl, CURLOPT_HTTPGET, 1);
  24. }
  25. curl_setopt($curl, CURLOPT_HTTPHEADER, $headers);
  26. curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
  27. curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, FALSE);
  28. curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, FALSE);
  29. $data = curl_exec($curl);
  30. curl_close($curl);
  31. return $data;
  32. }
  33. /**
  34. * @param Request $request
  35. *
  36. * @return mixed
  37. */
  38. public function purchase(Request $request)
  39. {
  40. $payment = new Payment();
  41. $payment->sn = self::generateGuid();
  42. $payment->user_id = Auth::user()->id;
  43. $payment->oid = $request->input('oid');
  44. $payment->amount = $request->input('amount');
  45. $payment->save();
  46. $data = [
  47. 'merchant_order_id' => $payment->sn,
  48. 'price_amount' => (float)$request->input('amount'),
  49. 'price_currency' => 'CNY',
  50. 'pay_currency' => $request->input('type') == 1? 'ALIPAY' : 'WECHAT',
  51. 'title' => '支付单号:'.$payment->sn,
  52. 'description' => parent::$systemConfig['subject_name']? : parent::$systemConfig['website_name'],
  53. 'callback_url' => (parent::$systemConfig['website_callback_url']? : parent::$systemConfig['website_url']).'/payment/notify',
  54. 'success_url' => parent::$systemConfig['website_url'].'/invoices',
  55. 'cancel_url' => parent::$systemConfig['website_url'],
  56. 'token' => $this->sign($this->prepareSignId($payment->sn)),
  57. ];
  58. $result = json_decode($this->mprequest($data), TRUE);
  59. if($result['status'] === 200 || $result['status'] === 201){
  60. $result['payment_url'] .= '&lang=zh';
  61. return Response::json(['status' => 'success', 'url' => $result['payment_url'] .= '&lang=zh', 'message' => '创建订单成功!']);
  62. }
  63. return Response::json(['status' => 'fail', 'data' => $result, 'message' => '创建订单失败!']);
  64. }
  65. public function sign($data)
  66. {
  67. return strtolower(md5(md5($data).parent::$systemConfig['bitpay_secret']));
  68. }
  69. public function prepareSignId($tradeno)
  70. {
  71. $data_sign = [
  72. 'merchant_order_id' => $tradeno,
  73. 'secret' => parent::$systemConfig['bitpay_secret'],
  74. 'type' => 'FIAT',
  75. ];
  76. ksort($data_sign);
  77. return http_build_query($data_sign);
  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. }