海外支付RolePay对接

RolePay只接受人民币和澳元两个币种的支付订单:AUG-澳元,CNY-人民币。所有和金额相关的数字均以货币最小面值为单位,以AUD为例,100表示AUD 1.00。

RolePay支持项目种类(现有市面上的大部分支付途径都是支持的):
1、微信QrCode
2、微信JsAPI
3、微信小程序
接入微信小程序需完成海外主体认证,并且认证主体需要与在RoyalPay开通的商户主体一致4、支付宝QrCode
5、支付宝H5
6、支付宝JsApi
7、线下支付订单
包含收银终端和QrCode两种,支持支付宝和微信线下QRCode支付用于对接无扫码设备的收银终端,下单后得到二维码地址,自行生成二维码图片后展示在收银终端屏幕上,并由用户使用对应支付客户端进行扫码支付。
8、京东Web订单
用于PC端京东支付,创建订单后跳转到返回的pay_url(需附加签名参数和redirect参数),随后进入京东支付页面完成支付 该接口仅现仅支持京东支付。
9、汇付Web订单
用于PC端汇付通支付,创建订单后跳转到返回的pay_url,随后进入汇付通支付页面完成支付 该接口仅现仅支持汇付通支付。

小程序代码参考:

<?php


/**
 * 发起支付调用类
 * Class RoyalPay
 */
class RoyalPay
{
    /**
     * 海外支付
     * RoyalPay constructor.
     * @param $openid
     * @param $money
     * @param $order_number
     * @param string $description
     * @param string $notify_url
     */
    public function royalPay($openid, $money, $order_number, $description = '订单', $notify_url = '')
    {
        $wx_config = [
            'appid' => ''   //小程序APPID
        ];

        $partner_code =   wxlibRoyalPayConfig::PARTNER_CODE; //必填,商户编码,由4位大写字母或数字构成
        $credential_code =   wxlibRoyalPayConfig::CREDENTIAL_CODE;//系统为商户分配的开发校验码
        $time = $this->msectime();


        $json_data = [
            'description' => $description, //必填,订单标题(最大长度128字符,超出自动截取)
            'price' => $money * 100,//必填,金额,单位为货币最小单位,例如使用100表示AUD1.00
            'currency' => 'AUD',//币种代码 默认值: AUD 允许值: AUD, CNY
            'notify_url' => $notify_url,//支付通知url,详见支付通知api,不填则不会推送支付通知
            'operator' => '', //操作人员标识
            'appid' => $wx_config['appid'],//小程序appid
            'customer_id' => $openid,//小程序openid
        ];
        $nonce_str = $this->wxCreateNonceStr(24);

        $sign = strtolower(hash('sha256', $partner_code . '&' . $time . '&' . $nonce_str . '&' . $credential_code, false));
        $url = 'https://mpay.royalpay.com.au/api/v1.0/gateway/partners/' . $partner_code . '/microapp_orders/' . $order_number .
            '?time=' . $time . '&nonce_str=' . $nonce_str . '&sign=' . $sign;
        $result = $this->curl_put($url, $json_data);
        if ($result['return_code'] != 'SUCCESS') {
            return sendArray($result, $result['return_code'], $result['return_msg']);
        }
//      ["partner_order_id"] => string(20) "MO201903261533033482" //商户订单ID
//      ["full_name"] => string(18) "COURSECUBE PTY LTD" //商户注册全名
//      ["partner_name"] => string(10) "CourseCube" //商户名称
//      ["channel"] => string(6) "Wechat" 支付渠道 Alipay|支付宝、Wechat|微信、Bestpay|翼支付
//      ["sdk_params"] => array(6) { //小程序支付所需参数(Json字符串)
//          ["timeStamp"] => string(10) "1553585585"
//          ["package"] => string(46) "prepay_id=wx261533051265542be3aed3e42896290556"
//          ["paySign"] => string(32) "D56CB67BEE3D03D39E2906C5FDEBED80"
//          ["appId"] => string(18) "wxddd0b735201c1bd2"
//          ["signType"] => string(3) "MD5"
//          ["nonceStr"] => string(30) "WIqnDBWGvcAa2er6xsMEevCwbMeITS"
//      }
//      ["result_code"] => string(7) "SUCCESS" //SUCCESS表示创建订单成功,EXISTS表示订单已存在
//      ["partner_code"] => string(4) "CCC1" //商户编码
//      ["order_id"] => string(32) "CCC1-20190326173304752-8WBFCYDN0" //RoyalPay订单ID,同时也是微信订单ID,最终支付成功的订单ID可能不同
//      ["return_code"] => string(7) "SUCCESS" //执行结果
        if ($result['result_code'] == 'EXISTS') {
            return sendArray($result, $result['result_code'], '订单已存在');
        }
        return sendArray($result);
    }

    private function curl_put($url, $data)
    {
        $data = json_encode($data);
        $ch = curl_init(); //初始化CURL句柄
        curl_setopt($ch, CURLOPT_URL, $url); //设置请求的URL
        curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-type:application/json;Accept:application/json'));
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE); //设为TRUE把curl_exec()结果转化为字串,而不是直接输出
        curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "PUT"); //设置请求方式
        curl_setopt($ch, CURLOPT_POSTFIELDS, $data);//设置提交的字符串
        $output = curl_exec($ch);
        if (curl_error($ch)) {
            return ['return_code' => 'curl error', 'return_msg' => curl_error($ch)];
        }
        curl_close($ch);
        return json_decode($output, true);
    }

    /**
     * 随机字符串生成
     * @param int $length
     * @return string
     */
    private function wxCreateNonceStr($length = 16)
    {
        $string = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
        $result = "";
        for ($i = 0; $i < $length; $i++) {
            $result .= substr($string, mt_rand(0, strlen($string) - 1), 1);
        }
        return $result;
    }

    /**
     * 获取毫秒级别时间戳
     * @return float
     */
    private function msectime()
    {
        list($msec, $sec) = explode(' ', microtime());
        return (float)sprintf('%.0f', (floatval($msec) + floatval($sec)) * 1000);
    }

}

/**
 * 支付回调处理
 * Class RoyalPayNotifyHandel
 */
class RoyalPayNotifyHandel
{
    /**
     * 海外支付回调
     * @throws 	hinkexceptionPDOException
     */
    public function indepNotify()
    {
        $uid = input('param.uid', 2);
//        $response = json_decode($GLOBALS['HTTP_RAW_POST_DATA'], true);

        $response = json_decode(file_get_contents('php://input'), true);
        $input =  new wxlibRoyalPayDataBase();
        $input->setNonceStr($response['nonce_str']);
        $input->setTime($response['time']);
        $input->setSign();
        if ($input->getSign() == $response['sign']) { //验证成功(Validate success)
            //商户订单号(order id of the merchant)
            $order_id = $response['partner_order_id'];
            //RoyalPay订单号 (order id of Royalpay)
            $royal_order_id = $response['order_id'];
            //订单金额,单位是最小货币单位(order ammount,the unit is cents)
            $order_amt = $response['total_fee'];
            //支付金额,单位是最小货币单位(pay ammount,the unit is cents)
            $pay_amt = $response['real_fee'];
            //币种(Currency)
            $currency = $response['currency'];
            //订单创建时间,格式为'yyyy-MM-dd HH:mm:ss',澳洲东部时间(Order creation time in the format yyyy-MM-dd HH: mm: ss', Eastern Australia)
            $create_time = $response['create_time'];
            //订单支付时间,格式为'yyyy-MM-dd HH:mm:ss',澳洲东部时间(Order payment time in the format yyyy-MM-dd HH: mm: ss', Eastern Australia)
            $pay_time = $response['pay_time'];

            // todo 业务处理

            $result = array('return_code' => 'SUCCESS');
            echo json_encode($result);
        } else {////验证失败(Validate failed)
            echo "fail";
        }
    }
}

文章转载自 https://www.juchengvi.com/looknews/70



原文地址:https://www.cnblogs.com/jucheng/p/12703191.html