Code Monkey home page Code Monkey logo

wechat_pay's Introduction

WechatPay

WechatPay API wrapper in Elixir.

⚠️ Be careful, some of the Wechat's API docs are inconsistent with the real API endpoints. I only test those APIs on a Wechat MP App.

Travis Hex.pm codebeat badge Inline docs

Installation

If available in Hex, the package can be installed as:

  1. Add wechat_pay to your list of dependencies in mix.exs:
```elixir
def deps do
  [{:wechat_pay, "~> 0.1.0"}]
end
```
  1. Ensure wechat_pay is started before your application:
```elixir
def application do
  [applications: [:wechat_pay]]
end
```

Usage

Config :wechat_pay

use Mix.Config

config :wechat_pay,
  env: :sandbox, # or :production
  appid: "wx8888888888888888",
  mch_id: "1900000109",
  apikey: "192006250b4c09247ec02edce69f6a2d",
  ssl_cacertfile: "certs/ca.cert",
  ssl_certfile: "certs/client.crt",
  ssl_keyfile: "certs/client.key",
  ssl_password: "test"

⚠️ If the env is :sandbox, you should fill the real appid, mch_id, apikey here, then call WechatPay.API.Client.get_sandbox_signkey() to get the sandbox apikey, then replace the apikey with this one. It's verbose, but it's a workaround for using in the sandbox env.

APIs

https://pay.weixin.qq.com/wiki/doc/api/index.html

JSAPI(公众号支付)

  • WechatPay.API.place_order(params)
  • WechatPay.API.query_order(params)
  • WechatPay.API.close_order(params)
  • WechatPay.API.refund(params)
  • WechatPay.API.query_refund(params)
  • WechatPay.API.download_bill(params)
  • WechatPay.API.report(params)
  • WechatPay.HTML.generate_pay_request(prepay_id)

Native(扫码支付)

  • WechatPay.API.place_order(params)
  • WechatPay.API.query_order(params)
  • WechatPay.API.close_order(params)
  • WechatPay.API.refund(params)
  • WechatPay.API.query_refund(params)
  • WechatPay.API.download_bill(params)
  • WechatPay.API.report(params)
  • WechatPay.API.shorten_url(url)

APP(APP支付)

  • WechatPay.API.place_order(params)
  • WechatPay.API.query_order(params)
  • WechatPay.API.close_order(params)
  • WechatPay.API.refund(params)
  • WechatPay.API.query_refund(params)
  • WechatPay.API.report(params)
  • WechatPay.API.download_bill(params)

Example

params = %{
  device_info: "WEB",
  body: "时习-#{event.title}",
  attach: nil,
  out_trade_no: order.order_number,
  fee_type: "CNY",
  total_fee: order.fee,
  spbill_create_ip: Keyword.get(opts, :remote_ip),
  notify_url: @notify_url,
  time_start: now |> format_datetime,
  time_expire: now |> Timex.shift(hours: 1) |> format_datetime,
  trade_type: "JSAPI",
  openid: User.openid(user),
}

case WechatPay.API.place_order(params) do
  {:ok, order} ->
    order
  {:error, reason} ->
    IO.inspect reason
end

Plug

There's a plug WechatPay.Plug.Callback to handle callback from Wechat's server

defmodule MyApp.Web.WechatPayController do
  use MyApp.Web, :controller

  @behaviour WechatPay.Plug.Callback.Handler

  plug WechatPay.Plug.Callback, handler: MyApp.Web.WechatPayController

  def handle_data(conn, data) do
    IO.inspect data
    # %{
    #   appid: "wx2421b1c4370ec43b",
    #   attach: "支付测试",
    #   bank_type: "CFT",
    #   fee_type: "CNY",
    #   is_subscribe: "Y",
    #   mch_id: "10000100",
    #   nonce_str: "5d2b6c2a8db53831f7eda20af46e531c",
    #   openid: "oUpF8uMEb4qRXf22hE3X68TekukE",
    #   out_trade_no: "1409811653",
    #   result_code: "SUCCESS",
    #   return_code: "SUCCESS",
    #   sign: "594B6D97F089D24B55156CE09A5FF412",
    #   sub_mch_id: "10000100",
    #   time_end: "20140903131540",
    #   total_fee: "1",
    #   trade_type: "JSAPI",
    #   transaction_id: "1004400740201409030005092168"
    # }
  end

  def handle_error(conn, reason, data) do
    reason == "签名失败"
    data.return_code == "FAIL"
  end
end

wechat_pay's People

Contributors

linjunpop avatar

Watchers

刘敬坤 avatar James Cloos avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.