diff --git a/app/Services/Gateway/MalioPay.php b/app/Services/Gateway/MalioPay.php index 5a529585..624cd9f8 100644 --- a/app/Services/Gateway/MalioPay.php +++ b/app/Services/Gateway/MalioPay.php @@ -8,8 +8,8 @@ use App\Services\Config; use App\Models\Paylist; use App\Services\MalioConfig; use Omnipay\Omnipay; - -use App\Models\DetectRule; +use Stripe\Stripe; +use Stripe\Charge; class MalioPay extends AbstractPayment { @@ -84,6 +84,23 @@ class MalioPay extends AbstractPayment ); } return json_encode($return); + case ('stripe'): + $stripe = new StripePay(); + $result = $stripe->purchase_maliopay($type, $price); + if ($result['errcode'] == 0) { + $return = array( + 'ret' => 1, + 'type' => 'url', + 'tradeno' => $result['tradeno'], + 'url' => $result['url'] + ); + } else { + $return = array( + 'ret' => 0, + 'msg' => $result['errmsg'] + ); + } + return json_encode($return); } } else if ($type == 'wechat') { $payment_system = MalioConfig::get('mups_wechat'); @@ -122,6 +139,23 @@ class MalioPay extends AbstractPayment ); } return json_encode($return); + case ('stripe'): + $stripe = new StripePay(); + $result = $stripe->purchase_maliopay($type, $price); + if ($result['errcode'] == 0) { + $return = array( + 'ret' => 1, + 'type' => 'qrcode', + 'tradeno' => $result['tradeno'], + 'url' => $result['url'] + ); + } else { + $return = array( + 'ret' => 0, + 'msg' => $result['errmsg'] + ); + } + return json_encode($return); } } } @@ -170,7 +204,7 @@ class MalioPay extends AbstractPayment } return $return; case ('tomatopay'): - $type = $args['type']; + $type = 'alipay'; $settings = Config::get("tomatopay")[$type]; $order_data = $_REQUEST; $transid = $order_data['trade_no']; //转账交易号 @@ -225,6 +259,57 @@ class MalioPay extends AbstractPayment die('success'); //The response should be 'success' only } return 'f2fpay'; + case ('stripe'): + Stripe::setApiKey(Config::get('stripe_key')); + $endpoint_secret = Config::get('stripe_webhook_endpoint_secret'); + $payload = @file_get_contents('php://input'); + $sig_header = $_SERVER['HTTP_STRIPE_SIGNATURE']; + $event = null; + + try { + $event = \Stripe\Webhook::constructEvent( + $payload, + $sig_header, + $endpoint_secret + ); + } catch (\UnexpectedValueException $e) { + http_response_code(400); + exit(); + } catch (\Stripe\Error\SignatureVerification $e) { + http_response_code(400); + exit(); + } + + switch ($event->type) { + case 'source.chargeable': + $source = $event->data->object; + $charge = Charge::create([ + 'amount' => $source['amount'], + 'currency' => $source['currency'], + 'source' => $source['id'], + ]); + if ($charge['status'] == 'succeeded') { + $type = null; + if ($source['type'] == 'alipay') { + $type = '支付宝支付'; + } else if ($source['type'] == 'wechat') { + $type = '微信支付'; + } + $order = Paylist::where('tradeno', '=', $source['id'])->first(); + if ($order->status != 1) { + $this->postPayment($source['id'], 'Stripe '.$type); + echo 'SUCCESS'; + } else { + echo 'ERROR'; + } + } + break; + default: + http_response_code(400); + exit(); + } + + http_response_code(200); default: return 'failed'; } @@ -238,6 +323,9 @@ class MalioPay extends AbstractPayment public function getReturnHTML($request, $response, $args) { $tradeno = $_GET['tradeno']; + if ($tradeno == '' || $tradeno == null) { + $tradeno = $_GET['source']; + } $p = Paylist::where('tradeno', '=', $tradeno)->first(); $money = $p->total; if ($p->status === 1) { diff --git a/app/Services/Gateway/StripePay.php b/app/Services/Gateway/StripePay.php index fa84a6fb..ed1d30f0 100644 --- a/app/Services/Gateway/StripePay.php +++ b/app/Services/Gateway/StripePay.php @@ -62,9 +62,57 @@ class StripePay extends AbstractPayment } else { return json_encode(array('url' => $source['wechat']['qr_code_url'], 'errcode' => 0, 'pid' => $pl->id)); } + } + + public function purchase_maliopay($type, $price) + { + $user = Auth::getUser(); + if ($type != 'alipay' and $type != 'wechat') { + return json_encode(['errcode' => -1, 'errmsg' => 'wrong payment.']); + } + $stripe_minimum_amount = MalioConfig::get('stripe_minimum_amount'); + if ($price < $stripe_minimum_amount) { + return json_encode(['errcode' => -1, 'errmsg' => '充值最低金额为'.$stripe_minimum_amount.'元']); + } + + $ch = curl_init(); + $url = 'https://api.exchangeratesapi.io/latest?symbols=CNY&base='.strtoupper(MalioConfig::get('stripe_currency')); + curl_setopt($ch, CURLOPT_URL, $url); + curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); + curl_setopt($ch, CURLOPT_HEADER, 0); + $currency = json_decode(curl_exec($ch)); + curl_close($ch); + + $price_exchanged = ((double)$price) / ($currency->rates->CNY); - return json_encode($source); - return json_encode(array('url' => $source['redirect']['url'], 'errcode' => 0, 'pid' => $pl->id)); + $source = Source::create([ + 'amount' => floor($price_exchanged * 100), + 'currency' => MalioConfig::get('stripe_currency'), + 'type' => $type, + 'redirect' => [ + 'return_url' => Config::get('baseUrl') . '/user/payment/return', + ], + ]); + + $pl = new Paylist(); + $pl->userid = $user->id; + $pl->total = $price; + $pl->tradeno = $source['id']; + $pl->save(); + + if ($type == 'alipay') { + return array( + 'url' => $source['redirect']['url'], + 'errcode' => 0, + 'tradeno' => $pl->tradeno + ); + } else { + return array( + 'url' => $source['wechat']['qr_code_url'], + 'errcode' => 0, + 'tradeno' => $pl->tradeno + ); + } } public function notify($request, $response, $args) diff --git a/config/.malio_config.example.php b/config/.malio_config.example.php index 7f09f383..19491225 100644 --- a/config/.malio_config.example.php +++ b/config/.malio_config.example.php @@ -40,9 +40,9 @@ $Malio_Config['bitpyax_alipay_type'] = 'ALIPAY'; // bitpayx 支付宝充值的 ####### 支付系统 ####### // 支付系统写 none 意味着不启用此收款方式 -// 支付宝目前支持 bitpayx、tomatopay、f2fpay +// 支付宝目前支持 bitpayx | tomatopay | f2fpay $Malio_Config['mups_alipay'] = 'bitpayx'; // Malio 聚合支付系统里面的 支付宝 要用的支付平台 -// 微信支付目前支持 bitpayx +// 微信支付目前支持 bitpayx | stripe $Malio_Config['mups_wechat'] = 'bitpayx'; // Malio 聚合支付系统里面的 微信支付 要用的支付平台 $Malio_Config['mups_minimum_amount'] = 0; // Malio 聚合支付系统限制最低的充值金额