| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105 |
- <?php
- namespace common\components\payment;
- use addons\SandPay\common\expand\sdk\PCCashier;
- use common\helpers\FormatHelper;
- use common\traits\ErrorTrait;
- use yii\helpers\Json;
- class SandPayFast
- {
- use ErrorTrait;
- public $config = [];
- public $url = 'https://api.joinpay.com/fastpay';
- public $refund_url = 'https://api.joinpay.com/refund';
- /**
- * @var $client PCCashier
- */
- public $client;
- public function __construct(array $config)
- {
- $this->config = $config;
- $this->client = new PCCashier();
- }
- public function pay($order)
- {
- try {
- // 参数
- $this->client->body = $order ;
- $apiMap=$this->client->apiMap();
- $form = '';
- $postData = $this->client->postData($this->config, $apiMap['index']['method']);
- $url = $this->config['apiUrl'] . $apiMap['index']['url'];
- $form = '<form action="' . $url . '" method="post">';
- foreach ($postData as $k => $v) {
- $form .= "{$k} <p><input type='text' name='{$k}' value='{$v}'></p>";
- }
- $form .= '<input type="submit" value="提交"></form>';
- return $form;
- } catch (\Exception $e) {
- \Yii::error(FormatHelper::exception($e, __METHOD__));
- $this->error = $e->getMessage();
- return false;
- }
- }
- public function notify($post)
- {
- try{
- $this->client->verify($this->config, $post['data'], $post['sign']);
- return [
- 'out_trade_no' => $post['data']['orderCode'], //商户订单号
- 'trade_no' => $post['data']['tradeNo'], //汇聚平台流水号
- 'pay_fee' => bcmul(intval($post['data']['totalAmount']), 0.01, 2), // 000000000001
- ];
- }catch(\Exception $e){
- \Yii::error(FormatHelper::exception($e, __METHOD__));
- $this->error = $e->getMessage();
- return false;
- }
- }
- public function refund($info)
- {
- $amount = str_pad((string) intval(bcmul($info['refund_amount'], 100)), 12, '0', STR_PAD_LEFT);
- $this->client->body = [
- "orderCode" => $info['out_trade_no'],
- "oriOrderCode" => $info['refund_order_no'],
- "refundAmount" => $amount,
- "notifyUrl" => \Yii::getAlias('@apiUrl').'/notify/sandpay-fast-refund',
- "refundReason" => mb_substr($info['refund_reason'],0,60),
- "refundMarketAmount" => "",
- "extend" => ""
- ];
- try {
- $method = 'orderRefund';
- $ret = $this->client->request($this->config, $method);
- // $apiMap =$this->client->apiMap();
- $this->client->verify($this->config, $ret['data'], $ret['sign']);
- // $postData = $this->client->postData($this->config, $apiMap[$method]['method']);
- // $postData['data']= Json::decode($postData['data']);
- $data = Json::decode($ret['data']);
- if($data["respCode"] == '000000'){
- //退款失败
- throw new \Exception($data['respMsg'] ?? $ret);
- }
- return true;
- } catch (\Exception $e) {
- throw new \Exception($e->getMessage().'请稍后重新发起交易,或联系汇聚客服');
- }
- }
- }
|